Home > Software design >  bash: execute a semicolon separated list of commands as variable
bash: execute a semicolon separated list of commands as variable

Time:10-06

In a bash script I'm putting several commands as a semicolon separated list in a variable. When trying to execute the commands by calling the variable, the semicolons seem not to be treated as separators of different commands but as arguments of the kill command:

# RESTARTCOMMAND="kill `ps -ef | awk '/[s]omebinary/ {print $2}'`; sleep 3; /bath/to/somebinary"

# echo $RESTARTCOMMAND
kill 23396566; sleep 3; /bath/to/somebinary

# $RESTARTCOMMAND
bash: kill: 23396566;: arguments must be process or job IDs
bash: kill: sleep: arguments must be process or job IDs
bash: kill: 3;: arguments must be process or job IDs
bash: kill: /bath/to/somebinary: arguments must be process or job IDs

Why is this happening and how can I avoid it? My attempts with quotes or escapes did not work...

CodePudding user response:

In this case, you should use eval command. Try the following:

eval $RESTARTCOMMAND
  •  Tags:  
  • bash
  • Related