List of Contents:
- Bash Script Definition
- Advantages of the Bash Script
- Disadvantages of the Bash Script
- Run the Script from the Terminal
- Create and Run the Script from the Bash File
- Purpose of Using the Bash Alias
- Advantages of Using the Bash Alias
- Create a Bash Alias
- Make the Bash Alias Permanent
- Common Examples of Bash Alias
- Get the List of Bash Alias
- Delete the Bash Alias
Bash Script Definition
Bash script is one of the shell scripting languages and it is the default shell scripting language of Linux. Different Bash commands can be executed from the terminal. When the Bash commands are written in a file with the “.sh” or “.bash” extension, it is called a Bash script. Bash is an interpreted and weakly typed language. So, the script of the Bash file is executed line by line and the data type declaration of the variable is not necessary. The “bash” command is required to execute the Bash file. The methods of executing the Bash script from the terminal and Bash file are shown in the next part of this tutorial.
Advantages of the Bash Script
There are many advantages of using the Bash script in Linux. Some of them are discussed as follows:
- It is easier to learn and use than the other shell scripting languages.
- Many administrative tasks are required to perform on a regular basis on Linux. These types of tasks can be done automatically by writing appropriate Bash scripts.
- Multiple commands can be executed as a single command using the Bash script.
- Different file related operations can be done easily using the Bash script.
Disadvantages of the Bash Script
Some disadvantages of the Bash scripting language are mentioned as follows:
- It has no built-in functions for different tasks like the other standard programming languages.
- It has no better debugging facilities like other languages. So, it is difficult to fix the errors in the Bash script.
- It can be used for structured programming only.
- It is not compatible for all types of operating system like other languages, such as Python, PHP, etc.
- In many cases, it is slower than the other programming languages.
Run the Script from the Terminal
If you have to execute some lines of script instantly, you can run the script from the terminal. Some examples are mentioned as follows:
Run the following script from the terminal to print a line of text using the “echo” command:
The following output appears after executing the script:
Run the following scripts from the terminal that contains multiple lines. Here, an infinite “while” loop is used that prints a line of text and exits from the loop using a break statement. When the Bash script contains many lines, it is better to write the script in a Bash file and execute the Bash file from the terminal.
The following output appears after executing the script:
Create and Run the Script from the Bash File
Create a Bash file named “largest.bash” with the following script that takes two numbers from the user and find out the largest number between them.
#Enter two numbers from the user
read -p "Enter the first number: " n1
read -p "Enter the second number: " n2
#Find out the largest number
if [ $n1 -gt $n2 ]
then
echo "The largest number is $n1"
else
echo "The largest number is $n2"
fi
Now, the Bash file can be executed using the “bash” command or by creating the executable file of the Bash file.
Run the following command to execute the Bash file using the “bash” command:
The following output appears after executing the script for the input values of 56 and 78. Here, 78 is the largest number:
Run the following command to make the “largest.bash” file executable:
Run the executable Bash file:
The following output appears after executing the previous command with the input values of 6 and 3. Here, 6 is the largest number:
Purpose of Using the Bash Alias
Sometimes, it requires running long commands that may contain one or more commands. If these types of long commands need to be executed multiple times, it is better to create a short command of the long command. The Bash “alias” command is used to create the short command of the long command and it gives an alternative name for that command. The short command can be created temporarily or permanently. The benefits of using the Bash “alias” command and the method of using the Bash “alias” command are explained in the next part of the tutorial.
Advantages of Using the Bash Alias
The Bash “alias” command has many benefits that are mentioned as follows:
- The short command is created by the user through the “alias” command. The user can set the name of the short command that he/she wants to set and to easily remember. So, the short command that is created by the “alias” command is more memorable for the user and is easier to use.
- It saves the time of the user by executing the short command.
- It can contain the argument or flag to create a customized command of a long command.
- It helps to create an organized command that optimizes the Bash workflow.
Create a Bash Alias
The following command prints the current system date:
The following commands create the short command of the “date” command and execute the short command:
$ dt
The following output shows that the output of the “date” command and the “dt” short command are the same. This type of alias command works temporarily. If the system restarts, the alias command will not work.
Make the Bash Alias Permanent
You have to edit the “~/.bashrc” file to add the “alias” command to make the shortcut of any command and run the “source” command with the “~/.bashrc” file to make the shortcut command permanent.
Run the following command to edit the “~/.bashrc” file by the nano editor:
Add the following line at the end of the file. Press “Ctrl+x” to save the file and exit from the editor:
Then, run the following command to confirm the modification of the “~/.bashrc” file by adding the line of the “alias” command:
Now, if you restart the system and run the “dt” shortcut command from the terminal, it will work.
Common Examples of the Bash Alias
Many types of shortcut commands can be created using the “alias” command. The method of creating the shortcut commands of some commonly used Bash commands using the “alias” command is shown in this part of the tutorial.
1. Get the List of Hidden Folders
The Bash “ls” command is mainly used to get the list of the files and folders of the current working directory. Different options can be used with this command to get the output in different ways. The “ls -d .*” command is used to print the list of all hidden folders.
The following similar output appears after executing the command:
Run the following commands to create the shortcut of the “ls -d .*” command with the shortcut name, “l”. The output of “l” is the same as the output of the “ls -d .*” command:
$ l
The following output appears after executing the previous commands:
2. Get the Result of the Arithmetic Operation with Fractional Value
The “bc” command is used to perform the arithmetic operations properly in Bash. The “bc –l” command is used to calculate the result of the arithmetic operation that may contain the fractional value. After executing the following “alias” command, the “cal” shortcut command is created. Then, the “cal” command can be used inside the “echo” command to calculate the result of 5/2 with the fractional value that contains two digits after the decimal point.
$ echo "scale=2; 5/2" | cal
The following output appears after executing the previous commands:
3. Get the Current Date with Weekday
The “date” command is used in Linux to get the date and time values in different formats. The “alias” command creates the shortcut of a “date +”%A, %B %d, %Y”” command that contains the weekday name and month name of the current date with the year value. Then, the “dtf” shortcut command is executed to check the output.
$ dtf
The following output appears after executing the previous commands:
4. Login as Root User
The “sudo” command is used in Linux to provide the root privilege. The “sudo –i” command is used to change the current user to the root user.
Run the following commands. The “alias” command creates the shortcut of the “sudo –i” command with the “admin” shortcut name. Then, the “admin” command is executed to check the output of the command.
$ admin
The following output appears after executing the “admin” command:
5. Count the Number of Lines of a File
The “find . -type f | wc –l” command is used to count the total number of lines of any file.
Run the following command. The “alias” command creates the shortcut of the command with the name, “cl”. Then, the “cl” command counts the total number of lines of the “temp.txt” file. The “cat” command is used here to check the total lines of the “temp.txt” file.
$ cl temp.txt
$ cat temp.txt
The following output appears after executing the previous commands based on the content of the “temp.txt” file. The “temp.txt” file has three lines and the output that is returned by the “cl” command is 3:
6. Find the Executed Last 5 Commands
The “history” command is used to get the list of the executed commands in Linux. The “history 5” command is used to get the list of the last five executed commands.
Run the following commands from the terminal. The “alias” command creates the shortcut command of the “history 5” command with the name, “h5”. Then, the “h5” command prints the output of the “history 5” command.
$ h5
The following output appears after executing the previous commands:
Get the List of Bash Alias
The “alias” command without any alias name is used to get the list of all existing shortcut commands in details that were created using the “alias” command. The following command displays the list of all previously created alias commands of the system:
The following output shows the list of all alias commands that are previously created:
Delete the Bash Alias
You can remove the shortcut command that is created by the “alias” command using the “unalias” command. The “dt” command was created earlier. The “unalias dt” command removes the “dt” command from the system. So, the following commands display the output of the “dt” command first. After executing the “unalias” command, the “dt” command will not work.
$ unalias dt
$ dt
The following output appears after executing the previous commands:
Conclusion
The basic concept of using the Bash script and Bash “alias” command for different purposes is explained here. The new Bash users will be able to know the purposes of learning the Bash script and Bash alias after reading this tutorial.