在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字符串操作表达式中使用文字字符串,有什么想法吗?
发布于 2013-06-28 20:58:23
嗯,不是这样的。不过,您可以使用sed或类似的东西,例如:
current_runlevel=$( runlevel | cut -d' ' -f2 )不过,使用中间变量会更快一些。
https://stackoverflow.com/questions/17364982
复制相似问题