使用$()将多个命令输出到变量Bash脚本中 [英] Multiple command output into variable Bash script with $()

查看:75
本文介绍了使用$()将多个命令输出到变量Bash脚本中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我检查了多个线程,仍然遇到问题,这是我要执行的命令.没有$()的命令可以毫无问题地将所需的输出输出到控制台,我似乎无法将该值放入一个变量中以备后用.

I reviewed the multiple threads on this and am still having issues, here is the command I'm trying to execute. Commands without the $() print the desired output to the console without issue, I just can't seem to put that value into a variable to be used later on.

MODEL3= $(/usr/sbin/getSystemId | grep "Product Name" | awk '{print $4}')

但是

MODEL3= /usr/sbin/getSystemId | grep "Product Name" | awk '{print $4}'  

-将输出到控制台. 非常感谢!

-will output to the console. Thanks so much!

推荐答案

那是正确的:

MODEL3=$(/usr/sbin/getSystemId | grep "Product Name" | awk '{print $4}')

但是您可以在没有grep的情况下写相同的内容:

But you can write the same without grep:

MODEL3=$(/usr/sbin/getSystemId | awk '/Product Name/{print $4}')

现在您将结果保存在MODEL3变量中,并且可以将其用作$MODEL3:

Now you have the result in the MODEL3 variable and you can use it further as $MODEL3:

echo "$MODEL3"

这篇关于使用$()将多个命令输出到变量Bash脚本中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆