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

相关推荐
顾安r8 小时前
11.5 脚本 本地网站收藏(解封归来)
linux·服务器·c语言·python·bash
鸠摩智首席音效师4 天前
如何在 Bash 命令中执行命令 (嵌套命令) ?
linux·bash
Bella的成长园地4 天前
Linux 中sudo bash -i 和 su root 有什么区别?
linux·运维·bash
顾安r7 天前
10.25 脚本 整理2024全年 GITHUB每周热门项目
linux·c语言·python·github·bash
百步送剑7 天前
在 Ubuntu 环境下 sh 脚本运行因为 Bash 改用 Dash 语法报错问题
ubuntu·bash·dash
Hali_Botebie8 天前
服务器上用Slurm 管理训练bash 脚本任务
运维·服务器·bash
潇I洒8 天前
Linux写sh开机启动脚本-bash报错的两种解决方法
java·linux·bash
Hali_Botebie8 天前
服务器上用Slurm 管理训练bash 脚本任务,申明使用GPU
服务器·chrome·bash
gfdgd xi8 天前
deepin 终端,但是版本是 deepin 15 的
linux·python·架构·ssh·bash·shell·deepin
我爱钱因此会努力9 天前
shell实战-跳板机和测试主机是否在线
服务器·tcp/ip·bash