1

I am facing this issue when concatenating strings Linux shell script

apphome="`cd \`dirname $0\` && pwd && cd - >/dev/null`"
echo "***************** APP Home***************"
echo $apphome
libdir="${apphome}/lib"
echo "**********Lib  DIR ***********************"
echo ${libdir}

echo $apphome and echo $libdir gives following output

***************** APP Home***************
/product/abc/project1/cba/STADATA
**********Lib  DIR ***********************
/libduct/abc/project1/cba/STADATA

why don't it append /lib to the libdir correctly, could anybody explain me what is going wrong here

3
  • Copy just that bit you posted into its own file and run it directly using sh or bash. You're introducing error somewhere that you're not posting. Commented Oct 31, 2012 at 7:03
  • 2
    Somehow, $apphome ends with a carriage return. Are you using a Windows shell? Commented Oct 31, 2012 at 10:57
  • 1
    $() nests, backticks do not. Use: $( cd $( dirname $0 ) && pwd ) ( no need to cd back, it's a subshell) or $( dirname $( readlink -f $0 )). Backticks are evil. Commented Oct 31, 2012 at 12:38

1 Answer 1

2

Just quote the string

libdir="${apphome}/lib"

Complete script

#!/bin/sh
apphome="`cd \`dirname $0\` && pwd && cd - >/dev/null`"
echo "***************** APP Home***************"
echo $apphome
libdir="${apphome}/lib"
echo "**********Lib  DIR ***********************"
echo ${libdir}

gives

***************** APP Home***************
/product/abc/project1/cba/STADATA
**********Lib  DIR ***********************
/product/abc/project1/cba/STADATA/lib
Sign up to request clarification or add additional context in comments.

8 Comments

Ok which shell are you using? How is the script executed?
If it keeps failing you I suggest printf which is bullet-proof :)
i have tried executing script as sh scriptname.sh and ./scriptname.sh both gives same error. How can I know which shell I am using.
Run the script as follows bash -x ./scriptname.sh. That will give you a trace of what it is doing, which should lead to a solution.
Glenn Jackman's comment is the best hint
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.