From: наб <nabijaczleweli@nabijaczleweli.xyz>
To: dash@vger.kernel.org
Subject: [PATCH] man: document what happens when IFS= (and when it's not)
Date: Sat, 22 Apr 2023 17:16:21 +0200 [thread overview]
Message-ID: <rgn7yvfaitifvmsx7tsu3bicrcrqjbhkai7jdj6q5ja3ccrq3o@musfjdmv7egg> (raw)
[-- Attachment #1: Type: text/plain, Size: 1864 bytes --]
A question I just got from a user was "how do I make while read -r l; do
...; done < f not strip the initial tabs?". Turns out, the manual is
silent on this, and POSIX just about implies this behaviour.
(Indeed, our read is almost verbatim POSIX, and both defer to Field
Splitting, but our Field Splitting isn't nearly as detailed,
and thank god.)
Even POSIX spends just one line describing this pivotal behaviour
(Issue 8 Draft 2.1 line 75044-75045:
"2. If the value of IFS is null, field splitting shall have no effect,
except that if the input is empty the result shall be zero fields.),"
and when I first encountered this it was also quite surprising to me.
Spell it out explicitly: IFS= means that input is preserved,
and the default value means whitespace is stripped from the front.
Drive it home with an example because it's esoteric (and I know from
that user that they first tried searching for read in the manual,
but it was not very helpful).
Reported-by: rozbrajaczpoziomow <rozbrajaczpoziomow@gmail.com>
---
src/dash.1 | 13 +++++++++++++
1 file changed, 13 insertions(+)
diff --git a/src/dash.1 b/src/dash.1
index d3893bc..bd4c662 100644
--- a/src/dash.1
+++ b/src/dash.1
@@ -1021,6 +1021,19 @@ .Ss White Space Splitting (Field Splitting)
.Ev IFS
as a delimiter and uses the delimiters to split the results of parameter
expansion and command substitution into fields.
+.Pp
+If
+.Ev IFS
+is empty, field splitting yields no fields if the input string was empty,
+and one string with the unchanged value of the input otherwise.
+For example, with the default
+.Ev IFS ,
+.Dq Ic read Fl r Ev l
+will remove any initial whitespace,
+but
+.Dq Ev IFS Ns = Ic read Fl r Ev l
+will leave the entire line in
+.Ev l .
.Ss Pathname Expansion (File Name Generation)
Unless the
.Fl f
--
2.30.2
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
next reply other threads:[~2023-04-22 15:16 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-04-22 15:16 наб [this message]
2024-04-06 9:30 ` [PATCH] man: document what happens when IFS= (and when it's not) Herbert Xu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=rgn7yvfaitifvmsx7tsu3bicrcrqjbhkai7jdj6q5ja3ccrq3o@musfjdmv7egg \
--to=nabijaczleweli@nabijaczleweli.xyz \
--cc=dash@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).