0

On Bash, I first define a variable CMD for a command line bash instruction, then I run it. An error occurs. Where does it go wrong?

$ CMD="VERBOSE=1 ./myscript"
$ $CMD
bash: VERBOSE=1: command not found
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
zell
  • 9,830
  • 10
  • 62
  • 115
  • 2
    See [BashFAQ #50: I'm trying to put a command in a variable, but the complex cases always fail!](http://mywiki.wooledge.org/BashFAQ/050) – Gordon Davisson Mar 17 '21 at 23:46

1 Answers1

2

Don't store commands in variables. Variables aren't smart enough to hold commands. They will let you down time and time again like your roommate that never washes the dishes.

Use a function. They're the right tool for the job. You can use them like they were regular executables; they can take arguments; there are no quoting/backslash/whitespace issues.

$ cmd() {
>     VERBOSE=1 ./myscript
> }
$ cmd

Functions are where it's at.

See also:

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • May i suggest you to precise that If you use parameters in function, the eval operator should be appropriate for the call ? $ cmd() { > ls "$1" > } $ cmd ls: cannot access '': No such file or directory $ cmd "spa ced" 'spa ced' $ cmd2() { > ls $1 > } $ cmd2 > ls of . directory < $ cmd2 "spa ced" ls: cannot access 'spa': No such file or directory ls: cannot access 'ced': No such file or directory – Zilog80 Mar 17 '21 at 22:27
  • Stay away from `eval`. If the argument were required I would write `cmd() { ls "$1"; }`. If it were optional, `cmd() { ls "$@"; }`. – John Kugelman Mar 17 '21 at 22:34
  • I agree that eval is not safe. At least it require to quote every parameters to prevents wrong doing. I'm not sure however that it should'nt be known, even if it should be avoided. – Zilog80 Mar 17 '21 at 22:40
  • Going this way, this should be done [there](https://stackoverflow.com/questions/5615717/how-to-store-a-command-in-a-variable-in-a-shell-script) too. There is a [smart answer there too](https://stackoverflow.com/a/44055875/3641635), taking account of POSIX shell. – Zilog80 Mar 17 '21 at 23:08
  • 1
    @Zilog80 The problem is that `eval` is a very tempting trap. It looks simple and powerful, but using it properly requires a deep understanding of the shell's parsing process... and people almost always use it as a *substitute* for understanding shell parsing (people who understand shell parsing well almost always also know about better ways to do things). As a result, it seems to cause more trouble than anything else, and showing it to beginning scripters is a bad idea. – Gordon Davisson Mar 17 '21 at 23:44
  • @GordonDavisson I understand. I'll keep that in mind in the future. – Zilog80 Mar 17 '21 at 23:52
  • @GordonDavisson I deleted my answer. It bothers me however that in this way there is no direct explanation of why what OP was doing don't work. – Zilog80 Mar 18 '21 at 00:05
  • 1
    @Zilog80 I've added a [link](https://stackoverflow.com/questions/61898254/how-can-i-create-a-bash-environment-variable-that-prefixes-an-environment-variab/61898307#61898307) to a longer answer where I explain in excruciating detail how Bash parses variable expansions and why storing an assignment in them doesn't work. Hopefully that answers why what they tried didn't work. – John Kugelman Mar 18 '21 at 01:47
  • There is also the [BashFAQ link](http://mywiki.wooledge.org/BashFAQ/050) @GordonDavisson posted above. – John Kugelman Mar 18 '21 at 01:47