X
X
xhimik2015-10-04 00:31:57
linux
xhimik, 2015-10-04 00:31:57

How to execute a command from a variable, for #!/bin/sh (not bash)?

[email protected] ~ $ komand7="echo 555"; for i in 1 2 3 4 5 6 7 8 9; do a="\$komand$i"; $a; done
$_komand1: команда не найдена
$_komand2: команда не найдена
$_komand3: команда не найдена
$_komand4: команда не найдена
$_komand5: команда не найдена
$_komand6: команда не найдена
$_komand7: команда не найдена
$_komand8: команда не найдена
$_komand9: команда не найдена
[email protected] ~ $ $_komand7
555
[email protected] ~ $

Why is the variable not converted to a command in the program?
But with manual input it works fine?
-------------------------------------------------- -------------------------------------------------- ----------------------
Found the answer for myself:
komand7="echo 555";komand9="ls -l"; for i in 1 2 3 4 5 6 7 8 9; do a="\$komand$i";echo $i; eval $a; done

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
DevMan, 2015-10-04
@xhimik

komand7="echo 555"
for i in 1 2 3 4 5 6 7 8 9; do
  a="komand$i"
  ${!a}
done
ideone.com/ShIYWg

V
Victor, 2015-10-04
@only-victor

bash: x='echo "ok"'
bash: $x
result:
"ok"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question