如何在Bash中将变量设置为命令的输出? - How do I set a variable to the output of a command in Bash?

问题:

I have a pretty simple script that is something like the following: 我有一个很是简单的脚本,以下所示: bash

#!/bin/bash

VAR1="$1"
MOREF='sudo run command against $VAR1 | grep name | cut -c7-'

echo $MOREF

When I run this script from the command line and pass it the arguments, I am not getting any output. 当我从命令行运行此脚本并将参数传递给它时,我没有获得任何输出。 However, when I run the commands contained within the $MOREF variable, I am able to get output. 可是,当我运行$MOREF变量中包含的命令时,我能够获取输出。 this

How can one take the results of a command that needs to be run within a script, save it to a variable, and then output that variable on the screen? 如何获取须要在脚本中运行的命令的结果,将其保存到变量,而后在屏幕上输出该变量? spa


解决方案:

参考一: https://stackoom.com/question/JW3B/如何在Bash中将变量设置为命令的输出
参考二: https://oldbug.net/q/JW3B/How-do-I-set-a-variable-to-the-output-of-a-command-in-Bash
相关文章
相关标签/搜索