[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: converting array to string by quoting each element for eval
From: |
Chris F.A. Johnson |
Subject: |
Re: converting array to string by quoting each element for eval |
Date: |
Tue, 15 Nov 2011 17:30:40 -0500 (EST) |
User-agent: |
Alpine 2.00 (LMD 1167 2008-08-23) |
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)
- converting array to string by quoting each element for eval, Peng Yu, 2011/11/15
- Re: converting array to string by quoting each element for eval,
Chris F.A. Johnson <=
- Re: converting array to string by quoting each element for eval, Peng Yu, 2011/11/15
- Re: converting array to string by quoting each element for eval, Chris F.A. Johnson, 2011/11/15
- Re: converting array to string by quoting each element for eval, Peng Yu, 2011/11/15
- Re: converting array to string by quoting each element for eval, Chris F.A. Johnson, 2011/11/15
- Re: converting array to string by quoting each element for eval, Peng Yu, 2011/11/15
- Re: converting array to string by quoting each element for eval, Chris F.A. Johnson, 2011/11/15
- Re: converting array to string by quoting each element for eval, Peng Yu, 2011/11/15
- Re: converting array to string by quoting each element for eval, Greg Wooledge, 2011/11/16
- Re: converting array to string by quoting each element for eval, Peng Yu, 2011/11/16
- Re: converting array to string by quoting each element for eval, Greg Wooledge, 2011/11/16