gpt4 book ai didi

linux - 没有变量的 Bash 字符串操作

转载 作者:IT王子 更新时间:2023-10-29 00:53:27 25 4
gpt4 key购买 nike

在 Bash 中,可以对变量进行字符串操作,例如获取 Linux 机器上的当前运行级别:

current_runlevel=$(runlevel) #sample output: 'N 2'
current_runlevel=${current_runlevel#* }
echo $current_runlevel #sample output: '2'

但是,是否可以将两条线结合起来,这样就不需要中间变量了?使用相同的示例,我希望它看起来像:

current_runlevel=${$(runlevel)#* }

这个不行,报错

${$(runlevel)#* }: bad substitution

关于如何在 Bash 字符串操作表达式中使用文字字符串有什么想法吗?

最佳答案

嗯,不是真的。您可以使用 sed 或类似的东西,例如:

current_runlevel=$( runlevel | cut -d' ' -f2 )

不过使用中间变量会快一点。

关于linux - 没有变量的 Bash 字符串操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17364982/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com