-2

test_script.sh:

#!/bin/sh
D1="$(dirname $0)"
echo $D1
D0="(dirname $0)"
echo $D0

What I type on the command line:

$ ./test_script.sh

Output:

.
(dirname ./test_script.sh)

What significance does the first dollar sign in D1's value have s.t. the command gets executed while D0's value is interpreted as a string?

2

A trivial search for $( in the shell man page gives the answer.

As $name causes parameter expansion $(command) causes command substitution i.e. it is replaced by the output of the command (with trailing newline(s) removed). $(command) causes word splitting after the expansion, "$(command)" avoids it (like $name vs. "$name").

"(dirname)" on the other hand is just a literal string to the shell.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.