Christian Sippel
Christian Sippel

Reputation: 5

bash: execute a semicolon separated list of commands as variable

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...

Upvotes: 0

Views: 496

Answers (1)

Saboteur
Saboteur

Reputation: 1428

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

eval $RESTARTCOMMAND

Upvotes: 2

Related Questions