在 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/