0

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!

8
  • can't reproduce that Commented Aug 17, 2017 at 8:43
  • Please provide output of :- echo "#$THE_VARIABLE#" Commented Aug 17, 2017 at 8:48
  • 1
    Could be the \r for the carriage return causing the problem. Commented Aug 17, 2017 at 8:48
  • Yes, this cannot be fully reproduced without having all the code and the csv. I know only basics of bash and I am wondering why its cuts the string assigned to variable when trying to run it (echo is ok). Maybe some Bash parameter? Commented Aug 17, 2017 at 8:50
  • @grail echo "#$THE_VARIABLE#" ----> #/fakepath/scripts/test.sh Commented Aug 17, 2017 at 8:53

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect! I just run dos2unix on the csv file and everything works. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.