bug-bash
[Top][All Lists]
Advanced

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

Re: Spaces trimmed from $* when assigned while IFS is unset [was: Unexpe


From: Chet Ramey
Subject: Re: Spaces trimmed from $* when assigned while IFS is unset [was: Unexpected word splitting on $* ...]
Date: Thu, 5 Oct 2017 10:48:53 -0400
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:52.0) Gecko/20100101 Thunderbird/52.3.0

On 10/4/17 6:10 PM, Martijn Dekker wrote:
> Op 04-10-17 om 17:52 schreef Chet Ramey:
>> It's interesting that other shells treat ${a:=b} as kind of like an
>> assignment statement (word splitting) but not quite (tilde expansion).
> 
> Hmm...
> 
>       v=~/bla
>       printf '%s\n' "$v"
> 
> outputs /Users/martijn/bla on all shells, so tilde expansion applies for
> a simple assignment anyway.
> 
> And
> 
>       unset -v v
>       v=${v:=~/bla}
>       printf '%s\n' "$v"
> 
> acts exactly the same.
> 
> This is all POSIXly correct, IIRC.

That's not quite what I meant. The difference between an assignment context
and a non-assignment word is that Posix specifies that tilde expansion is
performed following `:'.

"In an assignment (see XBD Variable Assignment), multiple tilde-prefixes
can be used: at the beginning of the word (that is, following the
<equals-sign> of the assignment), following any unquoted <colon>, or both."

Given the following script:

HOME=/usr/xyz
XPATH=/bin:/usr/bin:.

ADDPATH=${ADDPATH:-~/bin:~/bin2:$XPATH}
echo "$ADDPATH"
unset ADDPATH

: ${ADDPATH:=~/bin:~/bin2:$XPATH}
echo "$ADDPATH"
unset ADDPATH

Different shells will treat the `:~' tilde prefix differently between the
two statements, since they don't consider the := expansion a variable
assignment.

It would be nice if the shell were consistent in this regard.

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    address@hidden    http://cnswww.cns.cwru.edu/~chet/



reply via email to

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