bug-bash
[Top][All Lists]
Advanced

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

Re: eval a=b\ c


From: Linda Walsh
Subject: Re: eval a=b\ c
Date: Mon, 25 May 2015 15:49:24 -0700
User-agent: Thunderbird



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
----
You need to put \\ there:

echodo () {
echo "[ $a ]"
eval $@
}
Ishtar:law> echodo a=b\\ c
[ b\ c ]                    <<still quoted here in echo
Ishtar:law> echo $a
b c

Your first BackSlash was removed on the calling command line,
that means (as the 'echo' shows, that the BS is no longer there.

so you are telling eval to evaluate both expressions a=b & 'c'

But if you use \\, one goes away on call, and the other is till there for eval.



  [ 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 ]
Ishtar:law> echo $a
b c
---
Same ... You are using 2 levels of dequoting, so you need 2 BS's.
Make sense?


Alternatively, you can pass 1 BS if you quote the initial expression:

echodo 'export a=b\ c'
[ export a=b\ c ]
Ishtar:law> typeset -p a declare -x a="b c"

(I used typeset (or declare) -p to also show the "-x" (export) flag was set).

Alternatively you could also do it this way:


echodo () { printf -v v -- "%q " "$@"
echo "$v"; eval $v
}
Ishtar:law> echodo export a=b\ c
export a=b\ c Ishtar:law> typeset -p a declare -x a="b c"

You can do what you want in bash, you just have to be clear about
what you are telling it (same goes for most any program! ;-))....




reply via email to

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