On Tue, 15 Nov 2011, Peng Yu wrote:

Hi,

I find that I have to make a program quotearg.sh to convert an array
to a string by quoting each element. So that it be used for eval.

I'm not sure if there is a way that I can do eval in bash without
having to use quotearg.sh. If there is no such a way, should
quotearg.sh be added in bash (if it is not available in bash yet), as
it provides a fundamental functionality?


~/linux/bin/src/bash/quotearg/main$ cat ./main.sh
#!/usr/bin/env bash

../quotearg.sh a b c
../quotearg.sh "'" ' ' '"'
../quotearg.sh 'a' 'a b'

echo ================

args=('a' 'a b')
cmd="printf 'x%sx\n' ${args[@]}"
eval "$cmd"

   Why not use the array instead of making it into a single string?

$cmd "${args[@]}"

   Why are you using eval or quotearg.sh? It sounds as if you are
   making the process more complicated than it need be.

   You should explain why you want it.

echo ================#the following is what I want, the above is not.

args=('a' 'a b')
arg_string=`../quotearg.sh "${args[@]}"`
cmd="printf 'x%sx\n' $arg_string"
eval "$cmd"

~/linux/bin/src/bash/quotearg/main$  ./main.sh
'a' 'b' 'c'
''\''' ' ' '"'
'a' 'a b'
================
xax
xax
xbx
================
xax
xa bx

--
   Chris F.A. Johnson, <http://cfajohnson.com/>
   Author:
   Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)
   Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)

Reply via email to