voedger / kb

Knowledge base
0 stars 0 forks source link

howto: bash #14

Open maxim-ge opened 1 year ago

maxim-ge commented 1 year ago

Nesting "" and ()

In other words, "..." and $(...) can nest within each other. Command substitution, $(...), can contain one or more complete double-quoted strings. Also, double-quoted strings may contain one or more complete command substitutions. But, they do not interlace. Thus, a double-quoted string that starts inside a command substitution will never extend outside of it or vice versa.

https://unix.stackexchange.com/questions/289574/nested-double-quotes-in-assignment-with-command-substitution

Example:

cur_dir=$(dirname "$(readlink -f "$0")")

RHS and double quotes

There are no (good) reasons to double quote the RHS of a variable assignment when used as a statement on its own.

The RHS of an assignment statement is not subject to word splitting (or brace expansion), etc. so cannot need quotes to assign correctly. All other expansions (as far as I'm aware) do occur on the RHS but also occur in double quotes so the quoting serves no purpose.

That being said there are reasons not to quote the RHS. Namely how to address error "bash: !d': event not found" in Bash command substitution (specifically see my answer and rici's answer).

https://stackoverflow.com/questions/3958681/quoting-vs-not-quoting-the-variable-on-the-rhs-of-a-variable-assignment

So there are no (good) reasons to do:

cur_dir="$(dirname "$(readlink -f "$0")")"

Use just:

cur_dir=$(dirname "$(readlink -f "$0")")