简体   繁体   中英

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

I am creating a bash file.sh which will locate the path of a file and store the output of that command to use it later in another command. This is what I've done so far:

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

when I'm running it I'm getting this: "locate: invalid option -- 'p'" Can anyone help me on this please?

You have to store the first variable as a command. See below example which highlights what you are doing and how it is to be done.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM