5

I am writing a shell script which reads data from a properties file and stores in into a local variable in shell script. The problem is when i am trying to read multiple properties from the file and form a string its getting over written

#!/bin/bash
. /opt/oracle/scripts/user.properties

echo $username
echo $password
echo $service_name

conn=$username$password$service_name
echo $conn

the values of username=xxxx password=yyyy and service_name=zzzz i expect the output to be

xxxxyyyyzzzz 

but instead of that i am getting the output as

zzzz

please tell me where am i doing the mistake ?

Martin Schröder
  • 4,176
  • 7
  • 47
  • 81
charan
  • 309
  • 2
  • 4
  • 14

1 Answers1

9

I'm certain that the file /opt/oracle/scripts/user.properties contains CR+LF line endings. (Running the file command for the properties file would say ... with CRLF line terminators). Changing those to LF using dos2unix or any other utility should make it work.

Moreover, instead of saying:

conn=$username$password$service_name

you could say:

conn="${username}${password}${service_name}"
devnull
  • 118,548
  • 33
  • 236
  • 227
  • 1
    thanks devnull that worked perfectly.I never imagined that line endings would cause an issue. – charan Sep 10 '13 at 06:18
  • @devnull can you tell when we have to use the "{}" curly brackets and when not. – Indrajeet Gour Feb 23 '16 at 09:50
  • 1
    @Avenger for reference have a look at http://stackoverflow.com/questions/8748831/when-do-we-need-curly-braces-in-variables-using-bash, and more broadly http://stackoverflow.com/questions/2188199/how-to-use-double-or-single-bracket-parentheses-curly-braces – Base_v Aug 09 '16 at 11:28