Hi there !
I'm playing with the following in an interactive bash as well as in a
ksh script:
$ n=1
$ x1=" ok "
$ x2=" not ok "
$ eval echo \$x$n
ok
$ eval echo "\$x$n"
ok
$ n=2
$ eval echo "\$x$n"
not ok
$
So the variable var name seems to work but why are the blanks "deleted" >here? I also tried some variants with "printf", but also with no success.
So the variable var name seems to work but why are the blanks "deleted" >here?
-2One last note: echo itself will mangle leading spaces,|
|When you input "echo $x", the shell substitutes "$x" by the value of
|x with leading and trailing spaces stripped. The shell, not echo!
$ n=1
$ x1=" ok "
$ x2=" not ok "
$ eval echo \$x$n
ok
$ eval echo "\$x$n"
ok
$ n=2
$ eval echo "\$x$n"
not ok
$
So the variable var name seems to work but why are the blanks "deleted" here?
Just out of curiosity, I tried this:
$ IFS="" eval echo ".\$x$n."
. not ok .
$
To my surprise, it works but why is IFS relevant here?
And to my even bigger surprise, it looks like everything seems
to behave as expected in the bash session but in the ksh script,
IFS is not just changed for this single command but globally -
. So, the behavior of IFS probably is due to the dots "." used.No, it's not. I just added the dots here to clarify that the blanks get >lost.
I'm playing with the following in an interactive bash as well as in a
ksh script:
$ n=1
$ x1=" ok "
$ x2=" not ok "
$ eval echo \$x$n
ok
$ eval echo "\$x$n"
ok
$ n=2
$ eval echo "\$x$n"
not ok
$
So the variable var name seems to work but why are the blanks "deleted" here?
Just out of curiosity, I tried this:
$ IFS="" eval echo ".\$x$n."
. not ok .
$
To my surprise, it works but why is IFS relevant here?
And to my even
bigger surprise, it looks like everything seems to behave as expected in
the bash session but in the ksh script, IFS is not just changed for this single command but globally - so I had to save and restore it to prevent
the whole script from exploding.
POSIXLY_CORRECT.
Hi there !
I'm playing with the following in an interactive bash as well as in a
ksh script:
$ n=1
$ x1="-a ok-a "
$ x2="-a not ok-a "
$ eval echo \$x$n
ok
$ eval echo "\$x$n"
ok
$ n=2
$ eval echo "\$x$n"
not ok
$
So the variable var name seems to work but why are the blanks "deleted" here? I also tried some variants with "printf", but also with no success.
Just out of curiosity, I tried this:
$ IFS="" eval echo ".\$x$n."
.-a not ok-a .
$
To my surprise, it works but why is IFS relevant here? And to my even
bigger surprise, it looks like everything seems to behave as expected in
the bash session but in the ksh script, IFS is not just changed for this single command but globally - so I had to save and restore it to prevent
the whole script from exploding. This makes the interesting part much
longer and more complicated, eating up the potential advantage and
coolness superiority over just doing something like
-a [ $n -eq 1 ] && echo "$x1"
-a [ $n -eq 2 ] && echo "$x2"
Any hints from the experts?
TIA
-a-a-a-aFrank
On 2026-06-12 09:30, Frank Winkler wrote:
Hi there !
You've already got a couple useful hints and explanations. So just
some additional hints...
Below, consider using arrays instead of manipulating variable names.
(Thereby you avoid the 'eval' that you've already been suggested to
avoid in the first place, if not really necessary.)
-a x=( "" "-a ok-a " "-a not ok-a " )
-a echo ${x[1]}
-a echo ${x[2]}
Standard arrays are counted from 0 so the first array element has
a dummy element above. (Modern shells also support associative
arrays, in case you want more flexibility.)
If you want for (some reason) to use 'eval' note that you can, as
suggested, use two layers of quotes (one escaped), or just use two
different nested quotes ( " and ' ), but also note the effects of
expansions inside the double-quotes; you can take that for your
advantage (if you know the expansion rules).
The effect of IFS changes on _internal_ shell commands like 'eval'
have been explained already, and that this is standard behavior.
Note that you don't need to save/restore the IFS in standard code;
you can also embed it in a subshell-environment with parenthesis
to keep the IFS change local within the parenthesis, as in
-a ( IFS="" eval echo ".\$x$n." )
In ksh you won't create a real subshell that way if there's only
shell built-ins like 'eval' and 'echo' used, so it's efficient.
If you intend to use mainly "modern" shells like bash, ksh, zsh,
you should consider using (instead of [ ... ]) [[ ... ]], or in
case of arithmetic (as in your code below) the shell's arithmetic
statement
-a (( n == 1 )) && ..
For many values you may also use 'case',
-a case $n in
-a (1) ... ;;
-a (2) ... ;;
-a (*) ... ;;-a # default branch for errors
-a esac
And of course use 'printf' (instead of 'echo').
Janis
I'm playing with the following in an interactive bash as well as in a
ksh script:
$ n=1
$ x1="-a ok-a "
$ x2="-a not ok-a "
$ eval echo \$x$n
ok
$ eval echo "\$x$n"
ok
$ n=2
$ eval echo "\$x$n"
not ok
$
So the variable var name seems to work but why are the blanks
"deleted" here? I also tried some variants with "printf", but also
with no success.
Just out of curiosity, I tried this:
$ IFS="" eval echo ".\$x$n."
.-a not ok-a .
$
To my surprise, it works but why is IFS relevant here? And to my even
bigger surprise, it looks like everything seems to behave as expected
in the bash session but in the ksh script, IFS is not just changed for
this single command but globally - so I had to save and restore it to
prevent the whole script from exploding. This makes the interesting
part much longer and more complicated, eating up the potential
advantage and coolness superiority over just doing something like
-a-a [ $n -eq 1 ] && echo "$x1"
-a-a [ $n -eq 2 ] && echo "$x2"
Any hints from the experts?
TIA
-a-a-a-a-aFrank
To my surprise, it works but why is IFS relevant here? And to my
even bigger surprise, it looks like everything seems to behave as
expected in the bash session but in the ksh script, IFS is not just
changed for this single command but globally - so I had to save and
restore it to prevent the whole script from exploding.
Word splitting - The Bash Hackers Wiki
The latter is actually the POSIX-mandated behavior: "If the command
name is a special built-in utility, variable assignments shall
affect the current execution environment before the utility is
executed and remain in effect when the command completes".
Bash chooses to violate this, unless invoked with --posix or
POSIXLY_CORRECT.
On 12.06.2026 11:02, Kenny McCormack wrote:
I don't know much about ksh, but in bash, you can (and, IMHO, you should)
use the "nameref" functionality.
I'm still trying to figure out how exactly that works ... but thanks for
the hint!
On 12.06.2026 11:02, Kenny McCormack wrote:
I don't know much about ksh, but in bash, you can (and, IMHO, you should)
use the "nameref" functionality.
I'm still trying to figure out how exactly that works ... ut thanks for
the hint!
Shell scripting is still a cool science of its own :) ...
Hi there !
I'm playing with the following in an interactive bash as well as in a
ksh script:
$ n=1
$ x1=" ok "
$ x2=" not ok "
$ eval echo \$x$n
ok
$ eval echo "\$x$n"
ok
$ n=2
$ eval echo "\$x$n"
not ok
$
So the variable var name seems to work but why are the blanks "deleted" here? I also tried some variants with "printf", but also with no success.
Just out of curiosity, I tried this:
$ IFS="" eval echo ".\$x$n."
. not ok .
$
To my surprise, it works but why is IFS relevant here? And to my even
bigger surprise, it looks like everything seems to behave as expected in
the bash session but in the ksh script, IFS is not just changed for this single command but globally - so I had to save and restore it to prevent
the whole script from exploding.
It is also written that "If the command name is a special
built-in utility, variable assignments shall affect the current
execution environment before the utility is executed and remain
in effect when the command completes". So in fact ksh is
conforming, even though it is arguably a dumb behavior that Bash
disagrees with (even in --posix mode; just tested with 4.4 and
5.1).
It is also written that "If the command name is a special built-in
utility, variable assignments shall affect the current execution
environment before the utility is executed and remain in effect when the command completes". So in fact ksh is conforming, even though it
is arguably a dumb behavior that Bash disagrees with (even in --posix
mode; just tested with 4.4 and 5.1).
| Sysop: | Amessyroom |
|---|---|
| Location: | Fayetteville, NC |
| Users: | 74 |
| Nodes: | 6 (0 / 6) |
| Uptime: | 50:07:17 |
| Calls: | 1,100 |
| Files: | 1,339 |
| Messages: | 275,859 |