How to Get Script Name Using Bash Script? [3 Easy Ways]

Suppose you want to get the script name currently running using a Bash script. To get the bash script name from the script itself, you can use the basename command, parameter expansion, redlink command, and even show the path of the script along with its name. In this article, I am going to demonstrate all methods to bash get the script name in simple ways.

3 Methods to Get Script Name Using Bash Scripting

In this section, you will learn how to get a script name using Bash Scripting. I have demonstrated three methods of getting the script name using the basename command, parameter expansion, and I have shown the path along with the script names.

You can read our Comparison of Methods to distinguish between these three methods and pick the best one for your needs.

1. Get the Script Name Using “basename” Command

In this method, you are going to see how to use the basename command to get the script name. This command takes a filename and prints it. So, I have used this in my script along with $0 to get the script name.

To get the script name using the basename command, check the below-mentioned script:

#!/bin/bash

#Retreive script name being executed
script_name1=`basename $0`

#Display script name
echo "The script that you’re running is:$script_name1"
EXPLANATION

The first line, #!/bin/bash is called shebang, which specifies the interpreter as Bash, which will be used to execute the script. The $0 actually returns the full name of your script, but along with the basename, it returns the filename without any path names in it. Then the file name is assigned to the script_name1 variable. Again, the echo command and the $script_name1 variable print the script name.

Getting script name using basename commandAfter running the script, I got the script name printed.

2. Get Script Name Along With Directory Path

In this method, I have printed the script name along with the path. Along with the basename command, I’ve used the realpath command to get the absolute path of the script. You can do so by using the following script:

#!/bin/bash

#Retreive the script name
script_name=`basename "$(realpath $0)"`

#Display the script name
echo "The script that you’re running is:$script_name"

#Retreive the path
script_path=$(dirname $(readlink -f $0))
path_and_name="$script_path/$script_name"

#Display script path and script name
echo "The script you’re running with path: $path_and_name"
EXPLANATION
As in the first script, this code also starts with #!/bin/bash, which specifies the Bash interpreter.

Then a variable script_name assigns the name of the script, using the basename command along with the real path command, to get the absolute path of the script. Then the echo command prints the script name.

In the next line, a variable named script_path is used to assign the script path, where I have used the dirname command along with the readlink -f command to get the absolute path of the script’s directory.

After that, a variable path_and_name is used to assign the concatenation of previous variables, forming the absolute path and name of the script. Finally, the echo command displays the file path and name of the script. 

Getting script name along with path

The script has printed the script file name along with the path of the script.

3. Get the Script Name Using Parameter Expansion

In this method, I have used parameter expansion to perform pattern matching to get the script name. To do so you can use the below script:

#!/bin/bash

# Get the script name using parameter expansion
script_name="${0##*/}"
echo "Script name using parameter expansion: $script_name"

EXPLANATION
The variable script_name is used to assign the script name. The script name is found using the parameter expansion, where ${0} represents the value of the $0 variable, which is the script’s name or path. And ##*/ is a pattern that matches and removes the longest occurrence of */. So, overall, ${0##*/} removes the path component from the script’s name, resulting in only the script name. Finally, the script name is printed using the echo command, whenever the script is run. 

Getting script name using parameter expansionSee from the image, after running the script, the script name is shown.

Comparison of Methods to Get Bash Script Name

In this section, I will give you a comparative analysis of the three methods mentioned above so you can understand which will be best for you to use:

Methods Pros Cons
Method 1
  • The syntax is quite easy to use.
  • You need to know the command and its syntax.
Method 2
  • You can get the path along with the file name.
  • If you don’t need a file path, it contains redundant information.
Method 3
  • Searches for files and gets the exact filename.
  • You have to remember the signs and their sequences to use them correctly.

Any user can use the first and third methods only to get the file name. The first method is easier, as there is no need to memorize the parameter expansion signs.  On the other hand, users who need to know the path of the file can easily go for method 2,

Conclusion

Finally, you have an overview of bash scripts to get the script name, and you can use any of these methods according to your convenience. Getting a script name seems like an easy task, but when you deal with a huge number of scripts, it becomes difficult to handle. Feel free to ask any questions or share your thoughts related to this article.

People Also Ask

How to get file name in shell script?

If you want to get the file name in a shell script, use the basename command. By using this command, you can extract the filename along with the extension from a file path and store it in a predefined variable in a shell script.

How to read bash script?

There are several techniques to read a bash command, and one of them is using the cat command. To use it, simply type cat <filename> in your command line.  And, you can see the contents of your bash file on your screen.

What is a shell script file?

A shell script file is a file that contains one or more commands that are executed by the shell in sequence.

What is bin bash for?

The /bin/bash is used to instruct the operating system to use bash as the command interpreter. This line is also called the shebang.

Related Articles


<< Go Back to Bash Script Examples | Bash Scripting Basics | Bash Scripting Tutorial

Rate this post
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
Lamisa Musharrat

Hello there. My name is Lamisa Musharat, and I'm an Linux Content Developer Executive at SOFTEKO. I earned a bachelor's degree in Naval Architecture and Marine Engineering from Bangladesh University of Engineering and Technology (BUET).I learned Linux out of my curiosity and now I find it useful as automation is easier using Linux. I take great pleasure in assisting others with Linux-related issues. I really want you to enjoy and benefit from my efforts.Read Full Bio

Leave a Comment