If you need to store a command in a variable, don't use a string, that just can't work. See Bash FAQ #50. Use a function or an array.
Your line is parsed as the assignment var="bash createGnuploat.sh "${exp[j]}" ../Result/ 0"
followed by the two words ${arr[i]}
(which will be taken as a command name) and ${exp[j]}
(which will be the first and only argument to the command). Check the syntax highlighting in your question or in your editor, it shows what is inside the quoted string.
Always use double quotes around variable substitutions, e.g. "$foo"
. Otherwise the value of the variable is split into words that are interpreted as glob patterns. (Omit the double quotes in the 0.01% of the cases where this is desired behavior.) For an array, use "${foo[@]}"
to have each element of the array in a separate word ("${foo[*]}"
is a single word with the elements of the array separated by spaces; if you leave out the quotes, then each element is broken into separate words that are interpreted as glob patterns).
Here's your snippet rewritten using a function:
create_plot () {
bash createGnuploat.sh "${exp[$2]}" ../Result/ 0 "${arr[$1]}" "${exp[$2]}"
}
for j in "${!exp[@]}"
do
arr=([1]=mem [2]=gen [3]=usr)
for i in "${!arr[@]}"
do
create_plot "$i" "$j"
done
done
For the rare cases where it makes sense to use an array variable instead of a function:
for j in "${!exp[@]}"
do
arr=([1]=mem [2]=gen [3]=usr)
for i in "${!arr[@]}"
do
var=(bash createGnuploat.sh "${exp[j]}" ../Result/ 0 "${arr[i]}" "${exp[j]}")
"${var[@]}"
done
done
$
for variables you assign to:var="..."
. – choroba Sep 17 at 15:12bash
scripts.sh
extensions - it's misleading. – jw013 Sep 17 at 15:13bash
script", in the name of the file,.bash
works. Either way is better than.sh
which implies/bin/sh
. – jw013 Sep 17 at 15:25