[Top][All Lists]

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

Re: Spaces in args, escapes, and command substitution

From: Eric Blake
Subject: Re: Spaces in args, escapes, and command substitution
Date: Sun, 29 Oct 2006 17:55:41 -0700
User-agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv: Gecko/20060909 Thunderbird/ Mnenhy/

Hash: SHA1

According to address@hidden on 10/29/2006 5:29 PM:
> But dont variables set on command line apply to everything on
> that line?

Nope.  According to 2.9.1 Simple Commands, variable assignments are
deferred until after expansions, which include command substitution.  In
order to have IFS affect a command substitution, it must occur in an
earlier simple command.  For a one-liner, try:

IFS=$'\n'; vi $(grep -l PATTERN *)

The semicolon separates the variable assignment from the command.  And if
you don't want IFS to remain changed after the vi invocation, either use a
subshell, or save and restore IFS after you are done modifying it.  For

(IFS=$'\n'; vi $(grep -l PATTERN *))

And note that you had a typo - it should be IFS=$'\n' if you want a
newline, because IFS='\n' sets IFS to the two characters backslash and n
rather than the one character newline.

> Or is IFS not really an IFS?

IFS affects ONLY the results of variable and command substitution.  If it
were to affect the grammar, it would be MUCH harder to write a shell.
Reread POSIX 2.5.3 Shell Variables:

"IFS (Input Field Separators.) A string treated as a list of characters
that is used for field splitting and to split lines into fields with the
read command. If IFS is not set, the shell shall behave as if the value of
IFS is <space>, <tab>, and <newline>; see Field Splitting. Implementations
may ignore the value of IFS in the environment at the time the shell is
invoked, treating IFS as if it were not set."

and 2.6.5 Field Splitting

"After parameter expansion ( Parameter Expansion), command substitution (
Command Substitution), and arithmetic expansion ( Arithmetic Expansion),
the shell shall scan the results of expansions and substitutions that did
not occur in double-quotes for field splitting and multiple fields can

In other words, IFS only affects the results of expansions, not literal
text; words, in the sense of the shell grammar, are always delineated by
unquoted space, tab, and newline, regardless of the IFS setting.

- --
Life is short - so eat dessert first!

Eric Blake             address@hidden
Version: GnuPG v1.4.2.1 (Cygwin)
Comment: Public key at home.comcast.net/~ericblake/eblake.gpg
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org


reply via email to

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