0

I have script code that I think they look the same, but they must work differently.

BASEDIR=$(basename $0)
    SN="$(basename $0)"

Following the reference, double quotes are

How to suppress most of the interpretation of a sequence of characters.

In bash, I typed both, but the results are the same. What is really a difference between these two?

Cyrus
  • 84,225
  • 14
  • 89
  • 153
jayko03
  • 2,329
  • 7
  • 28
  • 51
  • 3
    These assignments are exactly the same, and both wrong. To fix the bugs, it would need to be `result=$(basename "$0")` (or `result="$(basename "$0")"`, which has the advantage that `"$(basename "$0")"` works correctly even in a non-assignment context). – Charles Duffy Aug 15 '19 at 14:02
  • ...try giving your script a name that contains whitespace to make those bugs more visible. – Charles Duffy Aug 15 '19 at 14:03
  • 1
    BTW, http://shellcheck.net/ is a good resource to go to first; it flags the issue in question with a link to [SC2086](https://github.com/koalaman/shellcheck/wiki/SC2086). – Charles Duffy Aug 15 '19 at 14:06
  • 2
    ...as another aside, it's bad form to use all-caps names for your script's own variables, as these names are used for variables that modify behavior of the shell and OS; see the relevant POSIX documentation @ https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html, keeping in mind that setting a local shell variable will overwrite any like-named environment variable. – Charles Duffy Aug 15 '19 at 14:07
  • BTW, given the name `BASEDIR`, I'll bet the author meant to run `dirname`, not `basename`. – Charles Duffy Aug 15 '19 at 23:47

0 Answers0