Aaryn
Aaryn

Reputation: 1643

Shell "source" command always has "command not found" on the first line of the imported file

So this works, but it's weird. I have a shell script reads a config file. Here they are:

Main Script:

#!/usr/bin/env bash

BASE="`dirname $0`"
BASE="`cd ${BASE} && pwd`"
CONFIG="${BASE}/sync.config"
source $CONFIG

echo "$SERVER_HOSTNAME"

Config (sync.config)

SERVER_HOSTNAME="test-1"
SERVER_DOCROOT="/opt/dir/dir/"

When I did this I noticed I would get the following warning when running the script.

/sync.config: line 1: SERVER_HOSTNAME=test-1: command not found

The first variable was empty, but the second was fine. Just for fun I added a rubbish comment on the first line:

#blah
SERVER_HOSTNAME="test-1"
SERVER_DOCROOT="/opt/dir/dir/"

The same thing happened, just on line 1. And this time, both vars were available to the script and it ran fine. It just complained about that very first line.

sync.config: line 1: #blah: command not found

Always the first line. And the rest of the file is fine. Why?

Upvotes: 1

Views: 1542

Answers (2)

Firat
Firat

Reputation: 1

There is a function which is overriding source.this is how i solved this problem:

unset -f source $CONFIG

Upvotes: 0

rici
rici

Reputation: 241701

You probably have an invisible character at the beginning of sync.config. Try:

 $ head -n1 sync.config | hd 

For example, you might see:

00000000  ef bb bf 23 62 6c 61 68  0a                       |...#blah.|
00000009

Which would indicate that the file starts with a UTF-8 BOM (byte order mark).

Upvotes: 5

Related Questions