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

相关推荐
小哈龙5 小时前
裸仓库 + Git Bash 搭建 本地 Git 服务端与客户端
开发语言·git·bash
阿巴~阿巴~1 天前
Linux基本命令篇 —— alias命令
linux·服务器·bash
Ven%13 天前
掌握Bash脚本编写:从服务启动脚本到语法精要
linux·服务器·开发语言·chrome·bash
阿乾之铭14 天前
推送项目到GitHub
ssh·github·bash
张紫娃15 天前
bash 命令行 |
开发语言·bash
先做个垃圾出来………18 天前
Bash (Bourne Again SHell)
开发语言·chrome·bash
Eric.Lee202120 天前
ubuntu 系统 多条命令通过 bash 脚本执行
linux·ubuntu·bash
bingbingyihao20 天前
服务自动添加实例工具
linux·运维·bash
等风来不如迎风去20 天前
【python】bash: !‘: event not found
chrome·python·bash
芯语新源21 天前
在 Linux 系统中通过 yum 安装 Sublime Text
linux·bash·sublime text