user8476799 user8476799 - 5 months ago 65
Bash Question

Bash run script from script using variable

I am trying to run bash script from bash script using variable.

The variable is assigned before from parsed CSV, and when I echo it the path to another script looks ok. When I try to run the script path from the variable it fails.

Assiging the variable (2nd column from csv):

THE_VARIABLE=`echo ${CHOICE[${WHICH}]} | awk -F";" '{print $2}'`


echo $THE_VARIABLE (ok):

/fake_path/scripts/test.sh


Bash debug:

+ echo $'/fake_path/scripts/test.sh\r'


However after that when trying to run:

bash $THE_VARIABLE
: No such file or directoryripts/test.sh


It seems like taking only part of the string in variable(?).

Thanks for help!

Answer Source

The string ends with ASCII character 13 (\x0D or \015 or \r) and not the sequence '\' 'r', with echo the trailing \r can't be seen because echo adds a \n, but appending a # character for example will show at begining of line.

echo "${THE_VARIABLE}#"
#fake_path/scripts/test.sh

to remove the trailing \r:

THE_VARIABLE=${THE_VARIABLE%$'\r'}

otherwise it means that input file has dos line ending \r\n, it could be modified with dos2unix.