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

相关推荐
想唱rap1 天前
表的约束条件
linux·数据库·mysql·ubuntu·bash
倔强的石头1061 天前
【Linux指南】进程控制系列(四)进程替换 ——exec 系列函数全解析与应用
linux·运维·bash
tianyuanwo2 天前
Bash与Sh的诞生背景、底层原理及Linux多Shell解释器兼容性解析
linux·开发语言·bash
木风小助理4 天前
`mapfile`命令详解:Bash中高效的文本至数组转换工具
开发语言·chrome·bash
liliangcsdn6 天前
bash中awk如何切分输出
开发语言·bash
ASEpochs7 天前
Vsocde中‘sh’不是内部或外部命令,也不是可运行的程序或批量处理文件--已解决
git·vscode·bash
叠叠乐7 天前
bash sh为什么终端不能tab补全
开发语言·bash
Ken_11157 天前
CentOS7升级GNU Bash
bash
我是koten10 天前
K8s启动pod失败,日志报非法的Jar包排查思路(Invalid or corrupt jarfile /app/xxxx,jar)
java·docker·容器·kubernetes·bash·jar·shell
西京刀客11 天前
Bash 脚本中的 ((i++)) || true 表达式详解( set -e 表达式陷阱)
bash·set·表达式