In standard sh
syntax, that'd be:
j=jjj
k=kkk
l=lll
for i in j k l
do
eval 'printf "%s\n" "$i=${'"$i"'}"'
done
That is ask the shell to interpret as shell code the concatenation of printf "%s\n" "$i=${
with the expansion of $i
and }"
, so for instance for i=j
, it interprets:
printf "%s\n" "$i=${j}"
Which once eval
uated calls the printf
command with %s\n
and j=jjj
as arguments.
The curly braces are necessary in some shells for values of i
such as 10
where $10
would be interpreted as ${1}0
in some shells instead of ${10}
.
Beware that with j='foo; bar'
, you'd see i=foo; bar
as output. If you wanted to output the shell code that would be used assign the same value to j
(like typeset -m j
does in zsh), POSIXly, you could do:
show_var() (
for _var do
eval 'set -- "$@" "$_var" "${'"$_var"'}"'
shift
done
LC_ALL=C awk -v q="'" -- '
function shquote(s) {
gsub(q, "&\\\\&&", s)
return q s q
}
BEGIN {
for (i = 1; i < ARGC; i++) print ARGV[i]"="shquote(ARGV[++i])
}' "$@"
)
And then:
show_var j k l