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.
Key Takeaways
- Learning how to get script names using different methods.
- Getting the script name along with the path name using Bash script.
Free Downloads
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 name.
Method1: Using the basename Command to Get the Script Name
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 do the same, follow the below-mentioned steps.
➊ First, launch the Ubuntu terminal. You can use the shortcut keys CTRL+ALT+T to do so.
➋ Then create a file with the extension .sh using the nano command and open the script in the text editor like the following.
nano get_scriptname1.sh
- nano: Opens and creates a file in the nano text editor.
- get_scriptname1.sh: File name.
➌ After that, copy the following script into your text editor.
#!/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.
➍ To save and exit the text editor press CTRL+ O and CTRL+X.
➎ Now, you need to make the bash script file executable. Execute the following command in the terminal to do so.
chmod +x get_Script1.sh
- chmod: Changes the permissions of files and directories.
- +x: Argument with the chmod command to add the executable permission.
- sh: File that you want to make executable
➏ Finally, you can simply run the file from your command line by executing:
./get_scriptname1.sh
After running the script, I got the script name printed.
Method 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.
Script (get_scriptname2.sh) >
#!/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: $script_path_with_name"
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.
Method 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.
Script (get_scriptname3.sh) >
#!/bin/bash
# Get the script name using parameter expansion
script_name="${0##*/}"
echo "Script name using parameter expansion: $script_name"
After running the script, the script name was shown.
Comparative Analysis of the Methods
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
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