I am trying to run multiple similar commands using a for loop and string formatting.
I am able to create and then print the strings of the commands I would like to run:
$foo=( "bar" "baz" )
$for ((j=0;j<2;++j)); do printf "touch %s_file.txt; " "${foo[j]}"; done
touch bar_file.txt; touch baz_file.txt;
But I want to enter these strings as a command. Based on other questions I was thinking that eval would do what I need:
$for ((j=0;j<2;++j)); do eval "touch %s_file.txt; " "${foo[j]}"; done
-bash: bar: command not found
-bash: baz: command not found
I was expecting(or hoping) the output would be equivalent to the output of:
$touch bar_file.txt; touch baz_file.txt;
The touch here is just an example. I'm more interested in how to format a string and then run it as a command. Thanks.
for prefix in "${foo[@]}"; do? Shouldn't be any need to havejat all.j. Really I am using string formatting because it is something I know how to do. It's good to know that is not the correct way to perform this operation, I will investigate alternatives. Thanks for your suggestions!