Table of Contents
Get script full path, bash get script directory, bash get script path, bash get script name, shell get script name from inside the script, shell script get absolute path, Linux find script location.
In my last script I shared the steps to run shell scripts in parallel and collect their individual exit status. Now in this article I will share some tips to get the script name in shell script, get script path in bash script with examples. You can collect these values from within the script.
Get script name in shell script
Use the below variable within the script to get the script name from within the shell script
#!/bin/bash script_name1=`basename $0` script_name2=`basename "$(realpath $0)"` echo $script_name1 echo $script_name2
Let us execute the script
[root@node2 deepak]# ./testscript.sh Script Name1: testscript.sh Script Name2: testscript.sh
So both our variables are giving us the script name of the executed script.
Get script path in shell script
Now there can be two posible situation where the script is located in some directory under a symlink or it is located under physical path. Now for both situation the variable will vary
Get script path under symlink
To get a relative path of the script we need a different variable, Here I have created a symlink for my script under /tmp
[root@node2 deepak]# ls -l /tmp/testscript.sh lrwxrwxrwx 1 root root 26 May 5 17:26 /tmp/testscript.sh -> /home/deepak/testscript.sh
Add the below variable
#!/bin/bash script_relative_path1=`dirname $0` script_relative_path2=`dirname "$BASH_SOURCE"` echo "Script-Dir-Relative : $script_relative_path1" echo "Script-Dir-Relative : $script_relative_path1"
Execute the script
[root@node2 deepak]# /home/deepak/testscript.sh Script-Dir-Relative : /home/deepak Script-Dir-Relative : /home/deepak
[root@node2 deepak]# /tmp/testscript.sh Script-Dir-Relative : /tmp Script-Dir-Relative : /tmp
Script path under physical location
Here I will create a variable to get the location of the physical path of my script
#!/bin/bash script_relative_path1=`dirname $0` script_relative_path2=`dirname "$BASH_SOURCE"` script_path1=$(dirname $(readlink -f $0)) script_path2=`dirname $(realpath $0)` script_path3=$(dirname "$(readlink -f "$BASH_SOURCE")") script_path4=`pwd` echo "Script-Dir-Relative : $script_relative_path1" echo "Script-Dir-Relative : $script_relative_path1" echo "Script Path 1: $script_path1" echo "Script Path 2: $script_path2" echo "Script Path 3: $script_path3" echo "Script Path 4: $script_path4"
Here we can use four different variables to get the script path
[root@node2 deepak]# /home/deepak/testscript.sh Script-Dir-Relative : /home/deepak Script-Dir-Relative : /home/deepak Script Path 1: /home/deepak Script Path 2: /home/deepak Script Path 3: /home/deepak Script Path 4: /home/deepak
If I execute the same script from it's softlink
[root@node2 deepak]# /tmp/testscript.sh Script-Dir-Relative : /tmp Script-Dir-Relative : /tmp Script Path 1: /home/deepak Script Path 2: /home/deepak Script Path 3: /home/deepak Script Path 4: /home/deepak
Get script path with script name
To get the script path and script name in the same variable. here you can combine above both the variables to get one single variable
#!/bin/bash script_name1=`basename $0` script_path1=$(dirname $(readlink -f $0)) script_path_with_name="$script_path1/$script_name1" echo "Script path with name: $script_path_with_name"
Execute the script
[root@node2 deepak]# /tmp/testscript.sh
Script path with name: /home/deepak/testscript.sh
Lastly I hope the steps from the article to get script name and script path from within the shell script on Linux was helpful. So, let me know your suggestions and feedback using the comment section.
Will not work when invoked as
. /home/deepak/testscript.sh
which is common for scripts that are setting env variables in the current
shell.
$(dirname "$(readlink -f "$BASH_SOURCE")")
seems to work.Or a better way would be to invoke using
sh
$BASH_SOURCE is not portable (if the shell is /bin/sh, /bin/ksh etc ?? ) .
So it appears there is no generic Unix shell method of getting the
dir D in which a script S resides, when invoked as : . D/S