Denebe
Denebe

Reputation: 1

What's the difference between "$IFS" and $(echo "$IFS)?

I use hexdump for two expressions. There is a small difference, I would like more to know about it…

thierry@toto-PC:~> hexdump -C <<< "$IFS"
00000000  20 09 0a 0a                                       | ...|
00000004

and

thierry@toto-PC:~> hexdump -C <<< $(echo "$IFS")
00000000  20 09 0a                                          | ..|
00000003

I know that 0a (line feed) means end of line.

I try an explanation.

Case_1) The shell-variable IFS contains per default 20 09 0a 0a. That means three values: a white-space 20, a tabulation 09 and a line feed 0a. Everything is expanded, but because of the double quoting, another line feed 0a is append at the end. Therefor the first stream is 20 09 0a 0a and is considered as line for the here string…

Case_2) $(echo "$IFS").

ONLY echo "$IFS" produces 20 09 0a 0a, it is like the Case_1 or like this:

thierry@toto-PC:~> echo "$IFS" | hexdump -C
00000000  20 09 0a 0a                                       | ...|
00000004

Then the command substitution eliminates the first 0a at the end of this stream. Why ? The command substitution looks the value of the IFS shell-variable and find 0a. Therefor the command substitution considers the first 0a (in 20 09 0a 0a) like a delimiter, but it is the same value as the last character 0a, and thus eliminates the first 0a.

At the end in the Case_2 reads logically: 20 09 0a.

Is this correct?

Comment: in this situation, the value in the case_2 is false, I mean this is not the expected value. (I don't say it is a bug. It works well !)

Upvotes: 0

Views: 125

Answers (1)

Armali
Armali

Reputation: 19415

Your explanation is partially correct. The shell variable IFS does contain space, tab, newline (20 09 0a). Not because of the double quoting, another line feed 0a is append at the end, but rather because of the <<<, see Here Strings:

The result is supplied as a single string, with a newline appended, to the command …

It's true that echo "$IFS" produces 20 09 0a 0a. It isn't true that the command substitution eliminates the first 0a at the end of this stream only, but rather therewith are any trailing newlines deleted, see Command Substitution. After this, one newline is appended again by <<<.

Upvotes: 0

Related Questions