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

相关推荐
vortex52 天前
反弹Shell场景中bash -c与直接bash -i的适用差异解析
c语言·chrome·bash
nono牛2 天前
30天Shell脚本编程实战(14天逆袭)
前端·chrome·bash
鸠摩智首席音效师3 天前
如何在 Bash 中循环遍历文件内容 ?
bash
ChrylZzz4 天前
bash和zsh
bash·shell·zsh
喵霓5 天前
mac系统的环境变量-bash_profile
开发语言·macos·bash
xuanloyer6 天前
linux基础学习--学习bash
linux·学习·bash
vortex57 天前
Windows 下 Git Bash 终端高效配置指南
windows·git·bash
vortex57 天前
Bash One-Liners 学习精要指南
开发语言·chrome·bash
蒋士峰DBA修行之路7 天前
红帽练习环境介绍
linux·开发语言·bash
张志翔的博客13 天前
如何在 macOS 上切换 Shell:从 Bash 到 Zsh 或其他 Shell
开发语言·macos·bash