1

Here is my property files

xxx.properties

SCRIPT_NAME="AAAAA"
ENVIRON="BBB"

Here is my sh file

yyy.sh

. xxx.properties
LOG_PREFIX="$SCRIPT_NAME(${ENVIRON})"
echo $SCRIPT_NAME
echo $ENVIRON
echo $LOG_PREFIX

If I run yyy.sh, it displays

AAAAA
BBB
ABBBA    <--- weird

But if I don't use property file and put variables in sh file it works

yyy.sh

SCRIPT_NAME="AAAAA"
ENVIRON="BBB"
LOG_PREFIX="$SCRIPT_NAME(${ENVIRON})"
echo $SCRIPT_NAME
echo $ENVIRON
echo $LOG_PREFIX

Displays

AAAAA
BBB
AAAAA(BBB)   <-- correct

why it happens, and how to solve the issue?

Thanks!

soworl
  • 5
  • 2

1 Answers1

2

If I run your script, I get the desired result:

$ bash yyy.sh
AAAAA
BBB
AAAAA(BBB)

If I convert xxx.properties to DOS format:

$ sed -i 's/$/\r/' xxx.properties

And run the script again, I get results that look somewhat similar to yours:

$ bash yyy.sh
AAAAA
BBB
)BBBA

This is because both SCRIPT_NAME and ENVIRON now end with a carriage return (\r) character.

The solution is to run dos2unix or similar utility on xxx.properties to remove DOS line-endings.

Community
  • 1
  • 1
John1024
  • 109,961
  • 14
  • 137
  • 171