bash 中 ${-#*i} 是什么意思?


author: hjjdebug

date: 2024年 12月 25日 星期三 17:43:45 CST

description: bash 中 ${-#*i} 是什么意思?


在centos 的 /etc/profile 中有这样的语句

for i in /etc/profile.d/*.sh /etc/profile.d/sh.local ; do

if [ -r "$i" ]; then

if [ "{-#\*i}" != "-" ]; then

. "$i" #是交互式shell, 直接source

else

. "$i" >/dev/null #非交互式shell, 将输出重定向到空

fi

fi

done

其中难理解的是:${-#*i}

${-#*i}, 是一个变量置换.

$-: 是一个内置变量,是外壳shell的标志集合,是一个字符串, 我的是himBHs

#是元字符,*也是元字符,i不是元字符,就是字符"i"

是从左侧开始查找并删除匹配的意思,最小匹配 ${parameter#[word]} :

是最大匹配 ${parameter##[word]} :

* 是匹配任何字符

i 是字面意义的i字符

if [ "{-#\*i}" != "-" ] 语句含义:

前部分是变量替换,最小匹配删除从开始到i字符为止得到新变量,与原变量相比

不等就是字符串带i, 等于就是字符串不带i

带i, 则说明shell 是interactive 的, 是交互式shell

相关推荐
dys_Codemonkey11 小时前
ROS 2 环境配置与 Shell 配置文件详解(zsh/bash)ROS 2 多工作空间规范配置
开发语言·chrome·bash
zhangfeng11331 天前
Windows 的 Git Bash 中使用 md5sum 命令非常简单 md5做文件完整性检测 WinRAR 可以计算文件的 MD5 值
人工智能·windows·git·bash
佳木逢钺2 天前
ROS Noetic 中自定义编译和替换 cv_bridge 的完整指南
人工智能·bash·ros
IMPYLH4 天前
Linux 的 basename 命令
linux·运维·服务器·ssh·bash
IMPYLH5 天前
Linux 的 b2sum 命令
linux·运维·服务器·bash
波特率1152006 天前
bash命令进阶学习(Shell 元字符)
linux·bash·shell
knighthood20017 天前
ROS1中source xxx.bash失效
开发语言·bash
IMPYLH7 天前
Bash 的 basenc 命令
linux·运维·服务器·bash·shell
IMPYLH7 天前
Linux 的 arch 命令
linux·运维·服务器·bash
Be for thing7 天前
Bash 编程语法
学习·bash