Bash argument not expanding in script

I pass an argument to bash as run . The first command in green executes as expected, however the second in blue fails as the $run does not expand. I tried to escape the variable with \ thinking the quotes were making the literal translation and also "${run}" but both did not work to expand the variable run . What am I missing? Thank you :).

bash script <run>
run=$1

printf -v cmd_q '(cd path/to/dir/%q*/*/out && exec sshpass -f out.txt scp -- *.txt* user@xxx.xx.xxx.xx:/path/to/dentination/*/folder)\n' "${array[@]}"
sshpass -f file.txt ssh -o strictHostKeyChecking=no -t xxx@xxx.xxx.xxx "$cmd_q"

printf -v cmd_q '(cd path/to/dir/%q*/*/out && exec sshpass -f out.txt scp -- *.txt* user@xxx.xx.xxx.xx:/path/to/dentination/"$run"/folder)\n' "${array[@]}"
sshpass -f file.txt ssh -o strictHostKeyChecking=no -t xxx@xxx.xxx.xxx "$cmd_q"

Try export run=$1 to make the variable visible in child processes. I can't see a mistake in the syntax.

1 Like

Shells don't expand variables within single quotes.

2 Likes

Thank you very much, the below worked :slight_smile:

run=$1
printf -v cmd_q '(cd path/to/dir/%q*/*/out && exec sshpass -f out.txt scp -- *.txt* user@xxx.xx.xxx.xx:'"$run"'/folder)\n' "${array[@]}"
sshpass -f file.txt ssh -o strictHostKeyChecking=no -t xxx@xxx.xxx.xxx "$cmd_q"