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

相关推荐
zh路西法15 小时前
【tmux入门】终端分屏、SSH远程守护与一键启动脚本
linux·运维·ssh·bash
承渊政道17 小时前
【MySQL数据库学习】(MySQL内置函数)
数据库·学习·mysql·ubuntu·bash·数据库开发·数据库系统
allway21 天前
How to Echo Multiline to a File in Bash [3 Methods]
开发语言·chrome·bash
Dontla1 天前
git bash打开Claude code报错:Claude Code on Windows requires git-bash.(别把git装其他位置,严格按照默认安装)找不到claude code
windows·git·bash
weixin_462446231 天前
手把手教你用 Bash 脚本自动更新 /etc/hosts —— 自动绑定网卡 IP 与节点名
开发语言·tcp/ip·bash
一个梦醒了1 天前
安装git bash选项推荐
开发语言·git·bash
Drone_xjw1 天前
Kylin 系统下使用 Shell 脚本定期清理历史文件
bash·kylin
技术小结-李爽2 天前
【工具】Shell之Bash、Zsh配置文件的使用
开发语言·bash
烁3473 天前
shell简单脚本语法
bash
承渊政道4 天前
【MySQL数据库学习】MySQL基本查询(下)
数据库·学习·mysql·leetcode·bash·数据库开发·数据库系统