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.
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"
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.
After 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"
#!/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.
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"
${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. See 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 |
|
|
Method 2 |
|
|
Method 3 |
|
|
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
- How to Get Date in Bash [2 Methods with Examples]
- How to Print Time in Bash [2 Quick Methods]
- How to List Users in Bash [2 Easy Ways]
- How to Get Current Time in Bash [4 Practical Cases]
- How to Use Date Format in Bash [5 Examples]
- How to Get Timestamp in Bash [2 Practical Cases]
- How to Copy and Paste in Bash [2 Methods & Cases]
- How to Read Password in Bash [3 Practical Cases]
- How to Send Email in Bash [2 Easy Methods]
- Bash Script to Send Email with Attachment [Step-by-Step Guide]
- How to Get IP Address in Bash [3 Methods]
- How to Find and Replace String in Bash [5 Methods]
- How to Call Another Script in Bash [2 Methods]
- How to Generate UUID in Bash [3 Simple Methods]
- 3 Easy Ways to Write to a File in Bash Script
- How to Write the Output to a File in Bash Script [5 Practical Cases]
- How to Create a List in Bash Scripts? [2 Easy Methods]
- How to Clear History in Bash [2 Practical Cases]
- How to Clear Screen Using Bash Script? [2 Effective Methods]
- How to Check Ubuntu Version Using Bash Scripts? [2 Methods]
<< Go Back to Bash Script Examples | Bash Scripting Basics | Bash Scripting Tutorial
FUNDAMENTALS A Complete Guide for Beginners