2
#!/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?

codeforester
  • 39,467
  • 16
  • 112
  • 140
daixiang0
  • 193
  • 2
  • 10

1 Answers1

3

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:

codeforester
  • 39,467
  • 16
  • 112
  • 140
  • i change IFS to read file each line as below: `a:1 \n b:2` I do not know a good way to read it with bash, do you have a good idea? – daixiang0 Oct 25 '17 at 07:37