In order from worst to best:

    DIRNAME="$(dirname $FILE)" will not do what you want if $FILE contains whitespace or globbing characters \[?*.
    DIRNAME=`dirname "$FILE"` is technically correct, but backticks are not recommended for command expansion because of the extra quoting issues when nesting them.
    DIRNAME=$(dirname "$FILE") is correct, but only because this is an assignment. If you use the command substitution in any other context, such as export DIRNAME=$(dirname "$FILE") or du $(dirname "$FILE"), the lack of quotes will cause trouble if the result of the expansion contain whitespace or globbing characters.
    DIRNAME="$(dirname "$FILE")" is the recommended way. You can replace DIRNAME= with a command and a space without changing anything else, and dirname receives the correct string.

To improve even further:

    DIRNAME="$(dirname -- "$FILE")" works if $FILE starts with a dash.
    DIRNAME="$(dirname -- "$FILE"; printf x)" && DIRNAME="${DIRNAME%?x}" works even if $FILE ends with a newline, since $() chops off newlines at the end of output and dirname outputs a newline after the result.

You can nest command expansions as much as you like. With $() you always create a new quoting context, so you can do things like this:

foo "$(bar "$(baz "$(ban "bla")")")"

You do not want to try that with backticks.

comments powered by Disqus
TitleTime
Carb Blocker31 minutes
Carb Blocker32 minutes
Panalean33 minutes
Carb Blocker34 minutes
Carb Blocker35 minutes
Carb Blocker36 minutes
Ultra Apex Diet1 hour

© Paste4BTC 2014 - Earn bitcoins by pasting! | My pastes | Popular pastes | New pastes | Payments | FAQ | Terms of Service | Pastes uploaded: 714 005 | Total hits: 73 917 158