[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: One IFS non-whitespace is stripped at the end of a word during word
From: |
Lawrence Velázquez |
Subject: |
Re: One IFS non-whitespace is stripped at the end of a word during word splitting |
Date: |
Sun, 08 Dec 2024 03:49:55 -0500 |
On Sun, Dec 8, 2024, at 3:27 AM, Yuri Kanivetsky wrote:
> $ bash -c 'IFS=x; a=ax; f() { for arg; do echo "($arg)"; done; }; f $a'
> (a)
>
> $ bash --version
> GNU bash, version 5.2.37(1)-release (x86_64-pc-linux-gnu)
>
> I.e. IFS non-whitespaces are not stripped at the beginning of a word,
> but if there's one such non-whitespace at the end, it is stripped.
It's not being stripped. IFS characters *terminate* fields, so
"ax" is split to the single field "a".
The shell treats each character of $IFS as a delimiter, and
splits the results of the other expansions into words using
these characters as field terminators.
(https://www.gnu.org/software/bash/manual/html_node/Word-Splitting.html)
> This looks like a bug, unless I'm missing something.
It's not a bug. It is how most (but not all) other shells behave:
$ cat /tmp/ifs.sh
IFS=x
a=ax
f() {
for arg
do
echo "($arg)"
done
}
f $a
$ bash /tmp/ifs.sh
(a)
$ dash /tmp/ifs.sh
(a)
$ ksh /tmp/ifs.sh
(a)
$ mksh /tmp/ifs.sh
(a)
$ oksh /tmp/ifs.sh
(a)
$ yash /tmp/ifs.sh
(a)
$ zsh --emulate sh /tmp/ifs.sh
(a)
()
--
vq