如何在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
相關文章
相關標籤/搜索