bug-bash
[Top][All Lists]
Advanced

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

converting array to string by quoting each element for eval


From: Peng Yu
Subject: converting array to string by quoting each element for eval
Date: Tue, 15 Nov 2011 16:13:24 -0600

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"

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



-- 
Regards,
Peng



reply via email to

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