0

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?

Etan Reisner
  • 77,877
  • 8
  • 106
  • 148
kensuke1984
  • 949
  • 1
  • 11
  • 21
  • possible duplicate of [Bash/shell scripting: What is the exact meaning of IFS=$'\n'?](http://stackoverflow.com/questions/4128235/bash-shell-scripting-what-is-the-exact-meaning-of-ifs-n) – Carl Norum Mar 13 '15 at 02:23
  • 1
    Note that [ANSI C Quoting](http://www.gnu.org/software/bash/manual/bash.html#ANSI_002dC-Quoting) is different again from single quoting and double quoting and back-quoting. – Jonathan Leffler Mar 13 '15 at 02:50

1 Answers1

3

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=""
Etan Reisner
  • 77,877
  • 8
  • 106
  • 148
  • I saw the difference! I am not sure difference caused from it.. but I comprehend. (I mean 'in practical use') But I may remember what you taught me someday when I face something Thank you very much. – kensuke1984 Mar 13 '15 at 02:31