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

相关推荐
坏小虎1 天前
~/.zshrc 和 ~/.bash_profile 详细介绍与区别
开发语言·bash
想唱rap1 天前
计算机网络基础
linux·计算机网络·mysql·ubuntu·bash
要记得喝水1 天前
适用于 Git Bash 的脚本,批量提交和推送多个仓库的修改
git·elasticsearch·bash
IMPYLH1 天前
Linux 的 mv 命令
linux·运维·服务器·bash
阿常呓语2 天前
Bash `readonly` 详解:只读变量、数组与函数
bash·linux command
IMPYLH3 天前
Linux 的 mktemp 命令
linux·运维·服务器·bash
拾贰_C3 天前
【Claude Code | bash | install】安装Claude Code
开发语言·bash
程序员小崔日记4 天前
一个命令救命:GitHub 爆火项目 thefuck,真把我笑服了
github·bash·开发者·宝藏项目
IMPYLH4 天前
Linux 的 mkdir 命令
linux·运维·服务器·bash
zhaoshuzhaoshu4 天前
Bash 与 Dash 的区别与联系
开发语言·bash·dash