Echo result of command preserving echo

Viewed 33

I have a code

#!/bin/bash
case "$*" in
  "info" ) echo "INFO * * *";;
  "copy" ) echo `$0 info`;;
esac

that prints this

$ ./print.sh info
INFO * * *
$ ./print.sh copy
INFO print.sh print.sh print.sh

how do I make it to print

$ ./print.sh info
INFO * * *
$ ./print.sh copy
INFO * * *

preserving echo in the copy case:

  "copy" ) echo ???;;
1 Answers

Quote the backticks so that the asterisks that are substituted in aren't subsequently interpreted as globs and expanded. $0 should also be quoted for safe measure.

"copy" ) echo "`"$0" info`";;

Also it's better to use $(...) in place of `...` these days. And no need to quote "info" or "copy".

copy) echo "$("$0" info)";;

Note that echo "$(cmd)" is redundant. You can just call the command directly and skip the $(...) and echo.

copy) "$0" info;;
Related