Search code examples
bashshellquoting

Quoting in sh bash ... ("") ($'') ('')


I hopefully would like to say I understand quotings used in BASH and their difference , " ", ' ', $' '.

I saw many shell scripts containing

IFS=$'\n'

but NO

IFS="\n"

It looks at least to me that there is no difference. and in my environment both work correctly (for my understanding), What difference is here? Is it just a custom?


Solution

  • They aren't the same.

    IFS=$'\n' sets the value of IFS to a literal newline.

    IFS="\n" sets the value of IFS to the string \n.

    See?

    $ IFS=$'\n'
    $ declare -p IFS
    declare -- IFS="
    "
    $ IFS="\n"
    $ declare -p IFS
    declare -- IFS="\\n"
    
    $ IFS="\n" read a b c <<<$'anbncndn'
    $ declare -p a b c
    declare -- a="a"
    declare -- b="b"
    declare -- c="cndn"
    $ IFS=$'\n' read a b c <<<$'anbncndn'
    $ declare -p a b c
    declare -- a="anbncndn"
    declare -- b=""
    declare -- c=""