Search code examples
bashvariable-assignmentdouble-quotes

Using double quotes in a command assigning it in a variable with double quotes bash


dir="$(find -L "${1:-.}" -mindepth 1 -type d 2>/dev/null|fzf -0)"

why it works even if quoting end is unspecified in bash. This should've show error but worked perfectly.WHY?

Previously I tried dir="$(find -L \"${1:-.}\" -mindepth 1 -type d 2>/dev/null|fzf -0)" but it failed.


Solution

  • $(...) establishes a new quoting context. After the $(, the next " is an opening quote, not the closing quote paired with your opening quote before the $(.

    As the entire right-hand side is a single command substitution, you don't need the outer quotes at all, as the RHS is not subject to word-splitting or pathname expansion, the prevention of which are the two reasons you would otherwise quote it.

    dir=$(find -L "${1:-.}" -mindepth 1 -type d 2>/dev/null | fzf -0)
    

    is sufficient.