1

I want to separate a variable from my main script file. This variable contains a path to another script:

PATH_TO_SCRIPT="/tmp/script_folder/"

I put this variable into a separate file called "config.sh".

In my main script, I source this file to get the path and use it this way:

source "config.sh"
echo $PATH_TO_SCRIPT
result=$($PATH_TO_SCRIPT/script.bash)

But I get the following result:

/tmp/script_folder/
/tmp/script_folder/script.bash: No such file or directory

If I don't source the file and explicitly put the variable into the main script this way, it is working:

#source "config.sh"
PATH_TO_SCRIPT="/tmp/script_folder/"
echo $PATH_TO_SCRIPT
result=$($PATH_TO_SCRIPT/script.bash)

I don't get it.. I can see that the path when sourced is well retrieved (the echo prouves it) but then I cannot use the path. I imagine my problem comes from the using of $(command) but I don't get why. Maybe a story with subshell etc. ? I don't get how to go over this error.

Pozinux
  • 964
  • 2
  • 10
  • 22
  • 2
    I can't reproduce this. Check if `config.sh` contains invisible characters, `/tmp/script_folder/script.bash: No such file or directory` doesn't make any sense if that file exists. – oguz ismail Dec 24 '20 at 10:46
  • 1
    Instead of `echo $PATH_TO_SCRIPT`, which can be misleading for a number of reasons (see [here](https://stackoverflow.com/questions/29378566/i-just-assigned-a-variable-but-echo-variable-shows-something-else)), use `printf '%s' "$PATH_TO_SCRIPT" | xxd` to get a detailed dump of the variable's *exact* contents. I'll bet there's something invisible/weird hiding in there. – Gordon Davisson Dec 24 '20 at 10:53
  • 1
    I agree you shoud check `config.sh` for special symbols. The easiest way to begin with is simple `cat -A` or `cat -v`. – Olesya Bolobova Dec 24 '20 at 11:23
  • 1
    Thanks all for your comments, you were so right! My problem comes from \r\n in my config.sh file (because I created it with notepad++ this morning and didn't think about converting it in Unix format.... grrrr). How should I do now with this SO post? @oguzismail was the first one to brought a comment with a right theory and Gordon Davisson brought an action to check oguz theory. Some of you can post an answer or should I do it? I checked using "od -c config.sh" and resolved with sed to convert to unix format. – Pozinux Dec 24 '20 at 11:52
  • @oguzismail I have no problem with that if the community think it is the best approach to have here. Do I have any actions to make for that? – Pozinux Dec 24 '20 at 12:10
  • 1
    @oguzismail I didn't know I could mark my own question as duplicate so it is what I did with the link you provided. Thanks. I was worried that a duplicate question is necessarily bad but I enjoyed the foolowing post :) https://stackoverflow.blog/2010/11/16/dr-strangedupe-or-how-i-learned-to-stop-worrying-and-love-duplication/ – Pozinux Dec 24 '20 at 12:22

0 Answers0