I have a command that is constructed within a variable in a bash script, like so:
CMD="cowsay "
CMD+="-n "
CMD+="foo-moo"
I want to run this command, then exit the script with the return value of the command. I can do:
${CMD}
exit $?
But is there some way of doing this as a single action (i.e. without using $? or similar)? I tried variations on the following but could not achieve the desired result myself.
exit "$(${CMD})"
echo "Command finished"or somethingresult=$?followed byecho "Command finished"thenexit $resultmaybe?