0

I am trying to create a variable called $SPIM that holds the result of which spim but it instead runs the spim which is not my intention.

➜  10 git:(main) ✗ echo "$(which spim)"              
/usr/bin/spim
➜  10 git:(main) ✗ $SPIM=$(echo "$(which spim)")     
SPIM Version 8.0 of January 8, 2010
Copyright 1990-2010, James R. Larus.
All Rights Reserved.
See the file README for a full copyright notice.
Loaded: /usr/lib/spim/exceptions.s
(spim) exit
Node.JS
  • 1,042
  • 6
  • 44
  • 114
  • Why is there an `echo` included in your command _anywhere at all_? What purpose does it serve? `result=$(echo $(somecommand))` is almost _always_ better replaced with just `result=$(somecommand)`. – Charles Duffy Apr 03 '22 at 17:36
  • Beyond that, using `which` in bash scripts is not particularly good form. bash caches binary locations internally; you're making your script _slower_ by using an external command like `which` to do the lookup instead of relying on the built-in cache). `spim=$(command -v spim)` would be somewhat less bad, but again, there's no real reason to do it at all. – Charles Duffy Apr 03 '22 at 17:38
  • `$SPIM` is always replaced with the current value of `SPIM`. If you want to *set* the variable you need `SPIM=`...something with no `$` at the beginning – Chris Dodd Apr 03 '22 at 17:38
  • (an extra `echo` doesn't just make your code slower, it can also make the output wrong; see [I just assigned a variable, but `echo $variable` shows something different](https://stackoverflow.com/questions/29378566/i-just-assigned-a-variable-but-echo-variable-shows-something-else), and [Why is printf better than echo?](https://unix.stackexchange.com/questions/65803/why-is-printf-better-than-echo) -- both questions' answers have aspects that apply here). – Charles Duffy Apr 03 '22 at 17:45

1 Answers1

1

Runs here with bash without problem (I do not have spim installed). No need for echo again.

$ SPIM=$(which ls)
$ echo $SPIM
/usr/bin/ls
PeterMmm
  • 24,152
  • 13
  • 73
  • 111