How to get script name, script path within the bash script in Linux


Written By - admin
Advertisement

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

Advertisement
#!/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.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

3 thoughts on “How to get script name, script path within the bash script in Linux”

    • $(dirname "$(readlink -f "$BASH_SOURCE")") seems to work.

      [root@server ~]# . /tmp/dir1/dir2/dir3/dir4/script.sh
      Script Path 3: /tmp/dir1/dir2/dir3/dir4

      Or a better way would be to invoke using sh

      ~]# sh /tmp/dir1/dir2/dir3/dir4/script.sh
      Reply
  1. $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

    Reply

Leave a Comment