daixiang0
daixiang0

Reputation: 323

Difference between set IFS as $'\n' and $' \n'

#!/bin/bash

show="ls -al /"
IFS=$'\n'
$show

The result is like bash: ls -al /: No such file or directory.

The shell cannot return expected result.

If I change IFS as $' \n' (notice I added a space), it is ok.

I do not have much knowledge about IFS, could someone explain it?

Upvotes: 2

Views: 252

Answers (1)

codeforester
codeforester

Reputation: 42999

In the first case:

show="ls -al /"
IFS=$'\n'
$show

The whole string ls -al / is being treated as the command name by shell, since IFS doesn't have a space in it and spaces in your variable don't induce word splitting. It is as good as writing the command as "$show" which completely suppresses word splitting.

In the second case, word splitting does happen since space is a part of IFS.


See also:

Upvotes: 3

Related Questions