kristopolous
kristopolous

Reputation: 1848

In Bash's builtin "read" command, is there a way for it to accept whitespace as input

My Google-fu is failing me here.

If I have a token of "hello world " and then use it as stdin for bash's builtin read, I will get the trailing whitespace truncated. Is there a way to preserve that?

Upvotes: 1

Views: 384

Answers (1)

0x1F602
0x1F602

Reputation: 865

Turns out that you have to set the variable $IFS to newline. This is what I did, and it worked

$ read x
hello world 
$ echo $x"testing"
hello worldtesting
$ IFS='\n'
$ read x
hello world 
$ echo $x"testing"
hello world testing

The manual says, and I quote, ahem:

If IFS has a value other than the default, then sequences of the whitespace characters "space" and "Tab" are ignored at the beginning and end of the word, as long as the whitespace character is in the value of IFS (an IFS whitespace character).

Sources: info on read itself & info on word splitting

Upvotes: 6

Related Questions