Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > gnu.bash.bug > #14376

Re: Empty ""s in ARG in ${x:+ARG} expand to no words instead of the empty word if prepended/appended with space

Path csiph.com!goblin1!goblin.stu.neva.ru!usenet.stanford.edu!not-for-mail
From Ilkka Virta <itvirta@iki.fi>
Newsgroups gnu.bash.bug
Subject Re: Empty ""s in ARG in ${x:+ARG} expand to no words instead of the empty word if prepended/appended with space
Date Sat, 21 Jul 2018 13:28:40 +0300
Lines 94
Approved bug-bash@gnu.org
Message-ID <mailman.3956.1532168951.1292.bug-bash@gnu.org> (permalink)
References <3d5b8d4b-b77e-2486-b45b-ca733e4d0ca0@redhat.com> <20180720223753294160030@bob.proulx.com>
NNTP-Posting-Host lists.gnu.org
Mime-Version 1.0
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding 7bit
X-Trace usenet.stanford.edu 1532168951 24139 208.118.235.17 (21 Jul 2018 10:29:11 GMT)
X-Complaints-To action@cs.stanford.edu
To bug-bash@gnu.org, Bob Proulx <bob@proulx.com>
Envelope-to bug-bash@gnu.org
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:52.0) Gecko/20100101 Thunderbird/52.9.1
In-Reply-To <20180720223753294160030@bob.proulx.com>
Content-Language en-US
X-SASI-RCODE 200
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=iki.fi; h=subject:to:references:from:message-id:date:mime-version:in-reply-to:content-type:content-transfer-encoding; s=smtp; bh=rmVjZlRMzxVG1CMaQ9xGLc+YVoFjXFjFObITQX0meqY=; b=a62nk1AaN3jFOomzMT0bdk5uo/78G3sb1Ux0Kw2wY13qdisKqUUqtx4r8lFphVjPNi9SmBVRQjtOWezsYpbI31ctLTDk3aTjK3X8CgT6qV0QIGCnKxaeqnRUpnfKUZiLX0z/+fIFCbPBJEPlt1b0v6TO42SHWSCJs2PRTUS/ti6vYIhJSFrcKb4dG0wmH96ewJzPDKwAlY+h6IWSP4YJarmEEmxe9n0G49sXv294x8NDMYz22dOe6dgpdWxsy97pBptrMXCL6QhA5YPr+xx96XzeC4F3rVhHxKGXz8z63os2c4iCjS+wSDq2JXmO8J60kX7FwDJmipKnhEqnE7rPDA==
X-detected-operating-system by eggs.gnu.org: FreeBSD 8.x [fuzzy]
X-Received-From 157.24.2.104
X-BeenThere bug-bash@gnu.org
X-Mailman-Version 2.1.21
Precedence list
List-Id Bug reports for the GNU Bourne Again SHell <bug-bash.gnu.org>
List-Unsubscribe <https://lists.gnu.org/mailman/options/bug-bash>, <mailto:bug-bash-request@gnu.org?subject=unsubscribe>
List-Archive <http://lists.gnu.org/archive/html/bug-bash/>
List-Post <mailto:bug-bash@gnu.org>
List-Help <mailto:bug-bash-request@gnu.org?subject=help>
List-Subscribe <https://lists.gnu.org/mailman/listinfo/bug-bash>, <mailto:bug-bash-request@gnu.org?subject=subscribe>
Xref csiph.com gnu.bash.bug:14376

Show key headers only | View raw


On 21.7. 07:44, Bob Proulx wrote:
> Denys Vlasenko wrote:
>> $ f() { for i; do echo "|$i|"; done; }
>> $ x=x
>> $ e=
>> $ f ${x:+ ""}
>> ^^^^^^^^^^^ prints nothing, bug?
>>
>> $  ${x:+"" }
>> ^^^^^^^^^^^ prints nothing, bug?
> 
> Insufficient quoting.  That argument should be quoted to avoid the
> whitespace getting stripped.  (Is that during word splitting phase
> using the IFS?  I think so.)
> 
> Try this:
> 
>    f "${x:+ ""}"
>    f "${x:+"" }"

That's not the same at all. With outer quotes, the result will always be 
a single word. Without them, having an empty 'x' would result in no word:


Without outer quotes:

$ for cond in "" "1" ; do for value in "" "*" ; do printf "<%s>\t" 
"$cond" "$value" ${cond:+"$value"}; echo; done; done
<>      <>
<>      <*>
<1>     <>      <>
<1>     <*>     <*>


With outer quotes:

$ for cond in "" "1" ; do for value in "" "*" ; do printf "<%s>\t" 
"$cond" "$value" "${cond:+"$value"}"; echo; done; done
<>      <>      <>
<>      <*>     <>
<1>     <>      <>
<1>     <*>     <*>


I suppose that could be used to pass optional arguments to some command.

Though different shells do behave a bit differently here, and I'm not 
sure which behaviour is the correct one. With the values from the third 
line in the above test (the other three seem consistent), different shells:


No extra spaces in ${cond:+"$value"}:

$ for shell in bash dash ksh "zsh -y" ; do $shell -c 'cond=1; value=""; 
printf "<%s> " "$0" ${cond:+"$value"}; echo;' ; done
<bash> <>
<dash> <>
<ksh>
<zsh> <>


Extra spaces in ${cond:+ "$value" }:

$ for shell in bash dash ksh "zsh -y" ; do $shell -c 'cond=1; value=""; 
printf "<%s> " "$0" ${cond:+ "$value" }; echo;' ; done
<bash>
<dash> <>
<ksh> <>
<zsh> <>


Or with multiple words inside:

$ for shell in bash dash ksh "zsh -y" ; do $shell -c 'cond=1; printf 
"<%s> " "$0" ${cond:+"" "x" ""}; echo;' ; done
<bash> <x>
<dash> <> <x> <>
<ksh> <> <x>
<zsh> <> <x> <>


It doesn't seem like a very good idea to rely on this, arrays would of 
course work better.


Bash: GNU bash, version 4.4.12(1)-release (x86_64-pc-linux-gnu)
ksh:    version         sh (AT&T Research) 93u+ 2012-08-01
zsh:  zsh 5.3.1 (x86_64-debian-linux-gnu)
dash: Debian's 0.5.8-2.4


-- 
Ilkka Virta / itvirta@iki.fi

Back to gnu.bash.bug | Previous | Next | Find similar | Unroll thread


Thread

Re: Empty ""s in ARG in ${x:+ARG} expand to no words instead of the empty word if prepended/appended with space Ilkka Virta <itvirta@iki.fi> - 2018-07-21 13:28 +0300

csiph-web