4

I am writing (my first) shell script, and, for flexibility would like to use variables. Here is a part of script I wrote following different examples searched:

#!/bin/sh
#
jar_path="/sbclocal/apps/blablabla"
echo $jar_path            # <- OK
echo ${jar_path}          # <- OK
echo $jar_path/jar        # <- /jarlocal/apps/blablabla
echo "$jar_path/jar"      # <- /jarlocal/apps/blablabla
echo ${jar_path}/jar      # <- /jarlocal/apps/blablabla
echo "${jar_path}/jar"    # <- /jarlocal/apps/blablabla

In the last line, I expect to get "/sbclocal/apps/blablabla/jar", but what I get is "/jarlocal/apps/blablabla"! So instead of concatenation, starting of the variable string is replaced with the constant part, which I expect in the end!

There is also more complex concatenation inside of the jar parameter, which gets broken in a way I can't figure out for sure, but I suppose the issue is the same.

What is wrong here please?

UPD: Edited to show different approaches I tried, all having the same result.

UPD: I have also tried with 2 variables, strange thing is that the first one is not shown, like it's empty, when I write something like

echo $var1$var2
echo ${var1}${var2}
echo ${var1}+${var2}

This is crazy, I think I will give up and use old plain copy-paste, as this will be much quicker.

Fredrik Pihl
  • 44,604
  • 7
  • 83
  • 130
Kurtevich
  • 345
  • 8
  • 18
  • possible duplicate of http://stackoverflow.com/questions/4181703/how-can-i-concatenate-string-variables-in-bash – Fredrik Pihl Feb 21 '13 at 12:22
  • Fredrik, this question is one of the sources I used, I don't see other way there, except the one that is stated to be specifically for Bash, with +. I use SH. – Kurtevich Feb 21 '13 at 12:24
  • all your examples gives the correct result in `bash` as well as `dash`. On my machine /bin/sh is a symlink to bash. check with `ls -l /bin/sh`. What does it say on you machine and what version of the shell are you running? – Fredrik Pihl Feb 21 '13 at 12:33
  • ls shows: lrwxrwxrwx 1 root root 4 Feb 7 2012 /bin/sh -> bash* So it's bash also. – Kurtevich Feb 21 '13 at 12:40

1 Answers1

4

To long to put as a comment...

$ ./var.sh 
/sbclocal/apps/blablabla
/sbclocal/apps/blablabla
/sbclocal/apps/blablabla/jar
/sbclocal/apps/blablabla/jar
/sbclocal/apps/blablabla/jar
/sbclocal/apps/blablabla/jar

As I said, your scrip works perfectly for me. Somethings to check:

First, change the shebang to #!/bin/sh -x to enable debugging and look at the output.

Secondly, run file var.sh to check the line endings. Expected result should be:

$ file var.sh 
var.sh: POSIX shell script text executable

Convert to unix-format using dos2unix or fromdos.

Fredrik Pihl
  • 44,604
  • 7
  • 83
  • 130
  • 2
    Fabulous! It really was because I created the file in Windows! What I did is just resaved it as "unix script file" (from Notepad++) and it works now! – Kurtevich Feb 21 '13 at 13:14
  • @Kurtevich outch :-) Glad I could help. Different line-endings causes subtle or really weird errors as in this case... – Fredrik Pihl Feb 21 '13 at 13:16
  • Usually they cause an unknown command error or somesuch, but echo was happy to include the carriage return in the Windows EOL in the argument list it was displaying. – William Feb 21 '13 at 15:26