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?

1

1 Answer 1

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:

Sign up to request clarification or add additional context in comments.

1 Comment

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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.