繁体   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