help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: printf '%s\n' "$@" versus <<< redirection


From: Kerin Millar
Subject: Re: printf '%s\n' "$@" versus <<< redirection
Date: Sat, 18 Feb 2023 12:52:05 +0000

On Sat, 18 Feb 2023 13:21:48 +0100
alex xmb ratchev <fxmbsw7@gmail.com> wrote:

> may i ask for what the IFS= does in a single var read ment ?
> i know its been manywhere , but , i couldnt keep it

(It wasn't easy to pick out your response)

The default value of IFS is $' \t\n'. This affects how word splitting works and 
how read splits a line into fields (it follows the same rules as for word 
splitting). The manual has this to say:-

"If IFS is unset, or its value is exactly <space><tab><newline>, the default, 
then sequences of <space>, <tab>, and <newline> at the beginning and end of the 
results of the previous expansions are ignored, and any sequence of IFS 
characters not at the beginning or end serves to delimit words."

Pay particular attention to the observation that a consecutive sequence of any 
of those three characters at the beginning and end of the results are ignored. 
We can see exactly that happening in the following example.

$ printf ' \t \t this is\tmy line \t \t ' | { read -r line; echo "${line@Q}"; }
$'this is\tmy line'

The manual goes on to say, "If the value of IFS is null, no word splitting 
occurs." We can prove this by putting it to the test.

$ printf ' \t \t this is\tmy line \t \t ' | { IFS= read -r line; echo 
"${line@Q}"; }
' \t \t this is\tmy line \t \t '

In summary, if reading to one variable name, the default value of IFS causes 
leading and trailing sequences of <space> and <tab> to be removed. Setting IFS 
as empty prevents splitting and causes read to assign to the _first_ specified 
variable name, with the original content of the line not being modified in any 
way. In the latter case, if you specify more than one variable name, the second 
onwards will only end up being empty.

-- 
Kerin Millar



reply via email to

[Prev in Thread] Current Thread [Next in Thread]