Skip to main content
let's try to make the title more searchable, even though someone looking for this might not know what command substitution is
Link
ilkkachu
  • 148k
  • 16
  • 268
  • 441

nested double quotes in highly-voted one-linerassignment with command substitution

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

A StackOverflow answeranswer with > 3.5K votes features this one-liner for assigning to DIR the directory of the current bash script:

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" 

I'm puzzled by the nested double-quotes. As far as I can tell, the following fragments are double-quoted:

"$( cd " "${BASH_SOURCE[0]}" " && pwd )" 

...and everything else to the right of the = (i.e. $( dirname and )) is unquoted. In other words, I assume that the 2nd, 4th, and 6th " characters "close" the 1st, 3rd, and 5th " characters, respectively.

I understand what the double-quotes in "${BASH_SOURCE[0]}" achieve, but what's the purpose of the other two pairs of double-quotes?

If, on the other hand (and the high vote score notwithstanding), the above snippet is incorrect, what's the right way to achieve its nominal intent?

(By nominal intent I mean: collect the value returned by pwd after first cd-ing to the directory returned by dirname "${BASH_SOURCE[0]}", and do the cd-ing in a sub-shell, so that the $PWD of the parent shell remains unchanged).

A StackOverflow answer with > 3.5K votes features this one-liner for assigning to DIR the directory of the current bash script:

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" 

I'm puzzled by the nested double-quotes. As far as I can tell, the following fragments are double-quoted:

"$( cd " "${BASH_SOURCE[0]}" " && pwd )" 

...and everything else to the right of the = (i.e. $( dirname and )) is unquoted. In other words, I assume that the 2nd, 4th, and 6th " characters "close" the 1st, 3rd, and 5th " characters, respectively.

I understand what the double-quotes in "${BASH_SOURCE[0]}" achieve, but what's the purpose of the other two pairs of double-quotes?

If, on the other hand (and the high vote score notwithstanding), the above snippet is incorrect, what's the right way to achieve its nominal intent?

(By nominal intent I mean: collect the value returned by pwd after first cd-ing to the directory returned by dirname "${BASH_SOURCE[0]}", and do the cd-ing in a sub-shell, so that the $PWD of the parent shell remains unchanged).

A StackOverflow answer with > 3.5K votes features this one-liner for assigning to DIR the directory of the current bash script:

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" 

I'm puzzled by the nested double-quotes. As far as I can tell, the following fragments are double-quoted:

"$( cd " "${BASH_SOURCE[0]}" " && pwd )" 

...and everything else to the right of the = (i.e. $( dirname and )) is unquoted. In other words, I assume that the 2nd, 4th, and 6th " characters "close" the 1st, 3rd, and 5th " characters, respectively.

I understand what the double-quotes in "${BASH_SOURCE[0]}" achieve, but what's the purpose of the other two pairs of double-quotes?

If, on the other hand (and the high vote score notwithstanding), the above snippet is incorrect, what's the right way to achieve its nominal intent?

(By nominal intent I mean: collect the value returned by pwd after first cd-ing to the directory returned by dirname "${BASH_SOURCE[0]}", and do the cd-ing in a sub-shell, so that the $PWD of the parent shell remains unchanged).

edited tags
Link
Gilles 'SO- stop being evil'
  • 865.9k
  • 205
  • 1.8k
  • 2.3k
deleted 2 characters in body
Source Link
kjo
  • 16.4k
  • 25
  • 76
  • 125
Loading
Tweeted twitter.com/StackUnix/status/742650150112350208
added 25 characters in body
Source Link
Jeff Schaller
  • 68.8k
  • 35
  • 122
  • 266
Loading
Source Link
kjo
  • 16.4k
  • 25
  • 76
  • 125
Loading