簡體   English   中英

如何將命令的 output 作為對 bash shell 中的變量的引用

[英]How to give the output of a command as a reference to a variable in bash shell

我正在創建一個 bash file.sh,它將定位文件的路徑並存儲該命令的 output 以便稍后在另一個命令中使用它。 這是我到目前為止所做的:

path_exe=${1:-locate binarycreator}
$path_exe -c config/config.xml -p packages $Name

當我運行它時,我得到這個:“定位:無效選項 - 'p'”有人可以幫助我嗎?

您必須將第一個變量存儲為命令。 請參閱下面的示例,該示例突出顯示了您正在做什么以及如何完成。

bash-5.0$ a=${1:-'which ls'}
bash-5.0$ echo $a
which ls
bash-5.0$ $a
/usr/bin/ls  # Notice only the command is printed but not executed.

--------------------------------------------- 

bash-5.0$ a=$(which ls)
bash-5.0$ echo $a
/usr/bin/ls
bash-5.0$ $a
aliases        bin 
# This executed the command

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM