How to run a command after assigning it to some variable in shell scripting?
example:
command_name=echo
Now, is there a way to use “$command_name hello world” instead of “echo hello world” ?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Yes. That exact code (
$command_name hello world) will work.Make sure that quotes, if present, are placed only around the command name and each individual argument. If quotes are placed around the entire string, it will interpret the entire string as the command name, which isn’t what you want.
For example:
will be interpreted as:
(which works), whereas:
is interpreted as:
which doesn’t work because it’s trying to find a command called
echo hello worldrather than interpreting hello and world as arguments.Similarly,
fails for the same reason, whereas:
works.