bug-bash
[Top][All Lists]
Advanced

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

Re: A bug when using bash shell variable


From: Andreas Kusalananda Kähäri
Subject: Re: A bug when using bash shell variable
Date: Thu, 13 Jan 2022 12:04:16 +0100

On Thu, Jan 13, 2022 at 11:55:45AM +0100, Kusalananda Kähäri wrote:
> 
> When you want to store several separate arguments, do not combine them
> into a single string.
> 
> Compare this
> 
>       args="-a 'hello world' -b '1 * 3'"
>       printf '"%s"\n' $args
> 
> and
> 
>       args=( -a 'hello world' -b '1 * 3' )
>       printf '"%s"\n' "${args[@]}"

Portably, to shells without arrays:

        set -- -a 'hello world' -b '1 * 3'
        printf '"%s"\n' "$@"

> Using an unquoted variable causes the shell to split the variable's
> value on spaces, tabs, and newlines (by default, the values in the IFS
> variable).  The words generated from this also undergoes filename
> globbing.  This is not what you want to do.
> 
> Instead, use an array to store the quoted arguments, and then make sure
> that you properly quote the expansion of the array's elements when you
> use it on the command line.
> 
> The bug is in your code, not in bash.
> 
> See also:
> 
> https://unix.stackexchange.com/questions/68694/when-is-double-quoting-necessary
> https://unix.stackexchange.com/questions/444946/how-can-we-run-a-command-stored-in-a-variable
> 
> (and others)
> 
> 
> On Thu, Jan 13, 2022 at 10:13:05AM +0000, ju nan wrote:
[cut]

-- 
Andreas (Kusalananda) Kähäri
SciLifeLab, NBIS, ICM
Uppsala University, Sweden

.



reply via email to

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