An equivalent sh
script to what's presented in the question:
#!/bin/sh exe1='filepath' n=1 var=exe$n eval "echo \"\$$var\"" # or: eval 'echo "$'"$var"'"'
The eval
line will evaluate the given string as a command in the current environment. The given string is echo \"\$$var\"
, which, after variables have been expanded etc., would become echo "$exe1"
. This is passed to eval
for re-evaluation, and the command prints the string filepath
.
You also mention arrays. It is true that the POSIX sh
shell does not have named arrays in the same sense that e.g. bash
has, but it still has the list of positional parameters.
With it, you can do most things that you'd need to do with a list in the shell.
For example,
set -- 'filepath1' 'filepath2' 'filepath3' for name in "$@"; do # or: for name do echo "$name" done
which is the same as just
for name in 'filepath1' 'filepath2' 'filepath3' do echo "$name" done
Since I'm usure of what it is you're trying to achieve, I can't come up with a more interesting example. But here you have an array-like structure that you iterate over. Why you'd want to iterate over names of variables is still not clear.
bash
, but about indirection. If you could show a more complete example, we could show how to do the same with/bin/sh
.