bug-bash
[Top][All Lists]
Advanced

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

Re: Bash $@ expansion undocumentedly ignoring '


From: Eric Blake
Subject: Re: Bash $@ expansion undocumentedly ignoring '
Date: Wed, 21 Mar 2012 16:35:40 -0600
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:10.0.1) Gecko/20120216 Thunderbird/10.0.1

On 03/21/2012 08:24 AM, Andreas Marschke wrote:
> Dear Maintainers of bash,
> 
> it has come to my attention that bash is unexpectedly removing quotes
> from parameters given to a script if read from within the
> script. See attached shell-script for a full explanation.
> 

> # This does not constitute that quotations in the passed parameters are to be 
> ignored/removed upon 
> # passing them back to the script.

Quotations are removed _before_ setting the positional parameters.  If
you want the positional parameters to contain quotes, you have to quote
those quotes.

> #
> # Execute the following script like this: 
> # $> bash ./test.sh hello' ''dadewq'da''q

That says to execute test.sh with one argument, whose contents are exactly:

hello dadewqdaq

> #
> # The output should be something equal to: 
> # hello dadewqdaq
> #
> # As a user I would expect an output like: 
> # hello' ''dadewq'da''q

Well, then you need to first pass that argument to the script, as in:

bash ./test.sh "hello' ''dadewq'da''q"

Notice how I quoted the ' characters by surrounding the overall argument
in "".

> #
> # This is non-standard/unexpected behaviour.
> 
> echo $@

Bug in your script - you generally want to use "$@", not $@, as the
former replays the positional parameters exactly, while the latter is
subject to globbing and field splitting which can alter the results
based on what is currently in the positional parameters.

-- 
Eric Blake   eblake@redhat.com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org

Attachment: signature.asc
Description: OpenPGP digital signature


reply via email to

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