absolute path for a script ran with relative path

I have a script in which i want to print absolute path of the same script irrespective of path from where i run script.

I am using
test.sh:

echo "pwd : `pwd`"
echo "script name: $0"
echo "dirname: `dirname $0`"

when i run script from /my/test/dir/struct as ../test.sh the output i get is

pwd : /my/test/dir/struct
script name: ../test.sh
dirname: ..

I want dirname to be printed as absolute path. script gives proper output if I run it with absolute path.
as /my/test/dir/test.sh the output i get is

pwd : /my/test/dir/struct
script name: /my/test/dir/test.sh
dirname: /my/test/dir

i want the same output in case when i run script using relative path

$ cat test.sh
currentpath="`pwd`/"
currentscript=`echo $0 | sed 's/^[./]*//'`
echo "pwd : `pwd`"
echo "script name: $currentpath$currentscript"
echo "dirname: `dirname $currentpath`"
$ ./test.sh
pwd : /my/test/dir/struct
script name: /my/test/dir/struct/test.sh
dirname: /my/test/dir

Hi,

not all systems have this, but if you are lucky:

 
#!/bin/bash
 
PNAME=$(readlink -f $0)

hi grepFruit,
thanks for replying, but i want the script to run on all machines, so i dont think so i'll be able to use "readlink" (i found that its not working on SunOS 5.10).

---------- Post updated at 07:18 PM ---------- Previous update was at 06:54 PM ----------

hi ygemici,
thnx for replying,
with the suggestion provided i am able to get absolute script path when i run script from the directoy where it is situated i.e. using ./test.sh.
# cat test.sh
currentpath="`pwd`/"
currentscript=`echo $0 | sed 's/^[./]*//'`
echo "currentscript : $currentscript"
echo "script name: $currentpath$currentscript"

# ./test.sh
currentscript : test.sh
script name: /my/test/dir/test.sh

but my real concern is in situation where i run script using ../ as in following case
# pwd
/my/test1
bash-3.00# ../test/dir/test.sh
currentscript : test/dir/test.sh
script name: /my/test1/test/dir/test.sh
bash-3.00#
scriptname should have been "/my/test/dir/test.sh"

Since your original code is using the obsolete command substitution syntax, I assumed it was a constraint.

#!/bin/sh

if cd "`dirname \"$0\"`"; then
    absdirpath=`pwd`
    cd "$OLDPWD" || exit 1
else
    exit 1
fi

echo "pwd: `pwd`"
echo "script name: $0"
echo "dirname: $absdirpath"

Regards,
Alister

hi alister,

got what you are doing,
"`dirname \"$0\"`" is a relative path, but still its a valid path from current pwd. so you are jumping to that path, then get new pwd in variable, which will give me what i want. and then jump back to old pwd where i wad initially.

thanks buddy.

You're very welcome. By the way, you can probably replace any `pwd` command substitutions with the with the environment's $PWD.

quite useful suggestion.(probably reducing d work shell has to do)

I was looking for that trick by long time, thanks for the post.

Just a silly question ... i thought that that command in backticks `command` was equivalent with command like this $( command ) but ..ehm.. i realized now that's not really the same. I tested the alister's trick with $( command ) instead of backticks.. but no success.

So what's the real difference between `command` and $( command ) please?

Thanks and regards.

Mau

The two command substitution syntaxes are not interchangeable; they are subject to different quoting behavior.

if cd "`dirname \"$0\"`"; then

would be translated to

if cd "$(dirname "$0")"; then

For more info, see Shell Command Language and If then else help in shell script Post: 302409782

In short, using the old style, obsolete backtick syntax can get hairy when used within double quotes and/or when you try to nest command substitutions. The newer syntax, $(...) parses commands just as if they were the top-level, without any special quoting requirements (sooooo much nicer).

Regards,
Alister

Thank You very much alister for the clear explanation and links.

Best regards,
Mau