lixinso
lixinso

Reputation: 793

Bash "source" command returns the wrong concatenated string

I have a configuration file like this:

//filename : stat.conf
LAS_SERVER="127.0.0.1"
LAS_PORT=3306
LAS_USER=root
LAS_PWD=root
LAS_DB=test
CONN_STR_LAS_DB="-h$LAS_SERVER -P$LAS_PORT -u$LAS_USER -p$LAS_PWD $LAS_DB"

Now I run the source command like this:

$ source ./stat.conf

$ echo $CONN_STR_LAS_DB
testot.0.1

You can see that the result is wrong. not in our expectation.

In contrast, I can get the right result if I run the command in the shell like this:

$ LAS_SERVER="127.0.0.1"
$ LAS_PORT=3306
$ LAS_USER=root
$ LAS_PWD=root
$ LAS_DB=test
$ CONN_STR_LAS_DB="-h$LAS_SERVER -P$LAS_PORT -u$LAS_USER -p$LAS_PWD $LAS_DB"
$ echo $CONN_STR_LAS_DB
-h127.0.0.1 -P3306 -uroot -proot test

This is the right result.

So, my question is: why I got the wrong result when using "source ./stat.conf" ?

I tested the same operation on another computer, I can get the right result. Is there anything that I missed to config on my computer?

My OS is CentOS 5.

Upvotes: 0

Views: 461

Answers (2)

Rugal
Rugal

Reputation: 2717

I know what's wrong with your shell code.

Thatis the results of environment variables,your $CONN_STR_LAS_DB declared in your shell script can be exist just within the run-time of this script,and of course you can not echo $CONN_STR_LAS_DB outside this scripts.

The way to solve this problem is to "export" your variables ,that is

export $CONN_STR_LAS_DB                

in your shell script,and that surely can be done! good luck!

Upvotes: -2

Gordon Davisson
Gordon Davisson

Reputation: 125788

Your config file has windows-style line endings (\r\n), not unix-style (just \n). You can use the dos2unix command to convert it. Then, switch to a text editor that doesn't create files with weird line endings.

Upvotes: 6

Related Questions