0

This question already has an answer here:

I want my bash script to read parameters from config file. Here is what inside the script:

#!/bin/bash
source /home/myscript/conf/config.conf

echo "$username"

and below is my config.conf:

username="jonas"

The output when I run the script:

[user@machinename bin]$ . thescript
: No such file or directoryonfig.conf
: command not found
jonas

Here I am confused, though it successfully print "jonas", why is there error "No such file or directory" and "command not found"?

Am I doing something wrong?

marked as duplicate by Gilles bash Aug 14 '14 at 21:51

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 6
    Are you sure that the error message has translated perfectly from your machine to this question? The error message No such file or directoryonfig.conf indicates that something in your script (perhaps a part not shown?) is cutting off /home/myscript/conf/c from the source command. – Warwick Aug 14 '14 at 5:51
  • 1
    In addition, the leading colons in the error messages do not look right. – John1024 Aug 14 '14 at 6:04
  • 1
    Your source file probably has Windows or Mac line endings. Fix that and the issue should go away. – Mat Aug 14 '14 at 8:02
  • Thanks everyone, it is as @Mat pointed out. It is solved now! – lacha Aug 18 '14 at 3:13
3

As @Mat pointed out, thescript probably has Windows newlines (carriage return/CR followed by line feed aka. LF). Compare:

$ echo : No such file or directoryonfig.conf
: No such file or directoryonfig.conf
$ printf '/[.................]/conf/config.conf\r: No such file or directory\n'
: No such file or directoryonfig.conf

Use dos2unix thescript to fix it.

  • Hi @l0b0 and @Mat, it is exactly as you said, when I do file config.conf, it says config.conf: ASCII text, with CRLF line terminators and when I do sed -n l config.conf it shows the CR at every line. Thanks for pointing out the link, it is very useful! – lacha Aug 18 '14 at 3:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.