[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: eval a=b\ c
From: |
Dave Yost |
Subject: |
Re: eval a=b\ c |
Date: |
Wed, 27 May 2015 00:59:53 -0700 |
OK, I can make it to work in bash if I say
echodo a=b\\ c
but then zsh tries to execute c. Can’t win.
The problem is: how do I write this function so that it can be invoked
identically in zsh and bash with identical results of setting a variable to a
value with a space in it?
> On 2015-05-26, at 1:04 AM, Andreas Schwab <schwab@suse.de> wrote:
>
> dave@yost.com writes:
>
>> eval $@
>
> You are expanding a shell parameter unquoted. Never do that unless you
> know what you are doing.
>
> eval "$@"
>
> Andreas.
>
> --
> Andreas Schwab, SUSE Labs, schwab@suse.de
> GPG Key fingerprint = 0196 BAD8 1CE9 1970 F4BE 1748 E4D4 88E3 0EEA B9D7
> "And now for something completely different."
On 2015-05-25, at 6:12 PM, Dennis Williamson <dennistwilliamson@gmail.com>
wrote:
> I'm trying to put a command in a variable, but the complex cases always fail!
> : http://mywiki.wooledge.org/BashFAQ/050
>
> Eval command and security issues : http://mywiki.wooledge.org/BashFAQ/048
>
> On Mon, May 25, 2015 at 2:33 PM, <dave@yost.com> wrote:
> Configuration Information [Automatically generated, do not change]:
> Machine: x86_64
> OS: linux-gnu
> Compiler: gcc
> Compilation CFLAGS: -DPROGRAM='bash' -DCONF_HOSTTYPE='x86_64'
> -DCONF_OSTYPE='linux-gnu' -DCONF_MACHTYPE='x86_64-unknown-linux-gnu'
> -DCONF_VENDOR='unknown' -DLOCALEDIR='/usr/local/bash/4.3.39/share/locale'
> -DPACKAGE='bash' -DSHELL -DHAVE_CONFIG_H -I. -I../bash-4.3
> -I../bash-4.3/include -I../bash-4.3/lib -g -O2
> uname output: Linux s6.millcomputing.com 2.6.32-504.16.2.el6.x86_64 #1 SMP
> Wed Apr 22 06:48:29 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
> Machine Type: x86_64-unknown-linux-gnu
>
> Bash Version: 4.3
> Patch Level: 39
> Release Status: release
>
> Description:
>
> # Echo the arguments, then execute them as a command.
> function echodo() {
> if [[ -n $ZSH_VERSION ]] ; then
> echo "[ ${(q)@} ]"
> eval ${(q)@}
> else
> echo "[ $@ ]"
> eval $@
> fi
> }
> echodo echo b\ c
> echodo a=b\ c
> echo $a
>
> The result in zsh is:
> $ echodo echo b\ c
> [ echo b\ c ]
> b c
> $ echodo a=b\ c
> [ a=b\ c ]
> $ echo $a
> b c
> $
>
> Bash gets an error:
> $ echodo echo b\ c
> [ echo b c ]
> b c
> $ echodo a=b\ c
> [ a=b c ]
> bash: c: command not found
> $ echo $a
>
> $
>
> I can't find a way to implement echodo in bash. Apparently this is because
> bash is unable to expand a variable with quoting intact, as zsh can do with
> its (q) expansion flag.
>
> Bash behaves differently in this case::
>
> $ echodo export a=b\ c
> [ export a=b c ]
> $ echo $a
> b
> $
>
>
> Repeat-By:
>
>
>
> Fix:
>
>
>
>
> --
> Visit serverfault.com to get your system administration questions answered.
>
- eval a=b\ c, dave, 2015/05/25
- Re: eval a=b\ c, Charles Daffern, 2015/05/25
- Re: eval a=b\ c, Linda Walsh, 2015/05/25
- Re: eval a=b\ c, Dennis Williamson, 2015/05/25
- Re: eval a=b\ c, Andreas Schwab, 2015/05/26
- Re: eval a=b\ c,
Dave Yost <=
- Re: eval a=b\ c, Greg Wooledge, 2015/05/26