how to get the scripts full dir path

lyang0@lyang0-OptiPlex-755:~$ ./test.sh 
.
lyang0@lyang0-OptiPlex-755:~$ cat test.sh 
#!/bin/bash
echo `dirname $0`
lyang0@lyang0-OptiPlex-755:~$ pwd
/home/lyang0

it doesn't get "/home/lyang0" and only when run /home/lyang0/test.sh it will get, but how can I do, then it can get the real full path anytime?

#!/bin/bash

echo "$(readlink -f $0)"

============

./testme 

/tmp/testme
1 Like
echo `pwd`

You assume that the script being executed is in the current working directory which it is likely not. Consider the typical case where the script (foo.sh) resides in /usr/local/bin and the user's current working directory is /tmp/wrk. Assuming that /usr/local/bin is in PATH, and the user types foo.sh at the command line, echo `pwd` returns the wrong answer as foo.sh doesn't reside in the current directory.

can we get the script's dirctory dir, here is /tmp, and only with one line to get this.

Not a one-line but it uses shell build-in functions

#!/bin/sh

n="$(readlink -f $0)"
echo "${n%/*}"

exit 0

I'm confused. I see several references to readlink(1) in the responses to this request, but I didn't see any indication that the file being executed is a symbolic link.

In practice I would expect that $0 in a shell script would be set to an absolute pathname of the script if the script was found by invoking an absolute pathname for the script or if the script was found using a directory (with an absolute pathname) found in $PATH.

Otherwise, I would expect that $PWD/$0 would be an absolute pathname of the script if the shell script was invoked by using a relative pathname on the command line or was found using a directory (with a relative pathname) found in $PATH.

So, at least with the Bourne shell, bash, and ksh, the following should give an absolute pathname of the script if the following is included in the script before it makes any changes to its current working directory:

case "$0" in
(/*)    script_path="$0" ;;
(*)     script_path="$PWD"/"$0" ;;
esac

When this uses relative paths, the resulting $script_path may include dot and dot-dot components and may also contain unresolved symbolic links. But, it should always give you an absolute path to the file containing the executing script.

Found one interesting thing... Please check...

#Script - 

$ cat Test_27_8.sh
echo "pwd-`pwd`"
echo "dir--`dirname $0`"  
echo "$(readlink -f $0)"
cd jobs/QA

echo "pwd-`pwd`"
echo "dir--`dirname $0`"
echo "$(readlink -f $0)"
cd QA

echo "pwd-`pwd`"
echo "dir--`dirname $0`"
echo "$(readlink -f $0)"


$ sh Test_27_8.sh
pwd-jobs
dir--.
jobs/Test_27_8.sh
pwd-jobs/QA
dir--.
jobs/QA/Test_27_8.sh
jobs/QA/QA
dir--.
jobs/QA/QA/Test_27_8.sh


#Run with the full path - Here we get the correct path....:)
sh /jobs/Test_27_8.sh
pwd-/jobs
dir--/jobs
/jobs/Test_27_8.sh
pwd-/jobs/QA
dre--/jobs
/jobs/Test_27_8.sh
pwd-/jobs/QA/QA
dir--/jobs
/jobs/Test_27_8.sh

@JC_1 - If you are using fix path for the script that you want it to use you can simply save it in a variable...

I still don't understand how readlink -f $0 is supposed to produce a full (or absolute) pathname for a shell script that was invoked as ./script or as bin/script . First, the readlink utility on openBSD and OS X systems doesn't have a -f option. Second, the man pages for systems I've seen that have a man page for readlink(1) that do have a -f option don't say anything about being able to turn a relative pathname into an absolute pathname (unless there is a symbolic link in the path being processed that expands to an absolute pathname). And, third, many systems (including Solaris 10 and 11) don't have a readlink utility at all.