Introduction to Bash Scripting
- rajababukarmakar
- Jul 22, 2024
- 3 min read
Updated: Jul 22, 2024
In this post, we will talk about how to run bash scripts in the terminal. The shell is the main component of Linux, which allows users to interact with the Linux kernel by using various commands like ls, cd, pwd, rm etc. Bash, one of the most popular shells, is the default shell for most Linux distributions. There are many types of shells available for Linux. This will be covered in another blog post.
Let's see how to run a basic bash command in the terminal.
Print text on the screen
In this example, we would try to print a text message on the terminal screen using the bash scripting command.
$ echo "Hello World! My name is Sun."
Hello World! My name is Sun.
To make the print message more fun and engaging, we can make a custom message for each one of you. Just copy and paste the command shown below. It is okay if you do not understand it now, we will cover advanced topics like string interpolation in bash, but we will come back to it after a few posts.
$ echo "Hello World! My name is $(whoami)."
Hello World! My name is SureshK.
Now, you should be able to see your user name printed on the screen along with the text.
Print today's date on screen
In this example, we would try to print today's date on the terminal screen using the bash scripting command.
$ date
Mon Jul 22 02:40:15 IST 2024
Now, you should be able to see the current time and date on the terminal.
Create a file and execute it
In this example, we would create a bash file and try to execute it. The script should print the message with the user's name as input by the user.
$ touch run.sh
The touch command will create an empty file named run.sh . The file needs to start with '#!/bin/bash' being the first line. We will use the nano text editor to write to the bash file.
$ nano run.sh
#!/bin/bash
echo "Hello World! My name is Sun." ;
To close and save the file, press Ctrl ^X and enter. Now, we can run the bash file using the bash command.
$ bash run.sh
Hello World! My name is Sun.
or similarly, the file needs to be made executable using chmod. Once the file is ready, we can execute it.
$ chmod +x run.sh
$ ./run.sh
Hello World! My name is Sun.
You should be able to see the message printed on the screen.
Why bash scripting when you can just write Linux commands directly on the terminal
In this example, we would dig deep into why should we even care to write bash scripts if we can just do the same thing directly on terminals.
The bash scripts make code readable and easier to understand for other users. Let's try to write a small code to print a message if the user is root, and a separate message if the user is not root. Firstly, let's try to do that in the terminal directly.
$ if [ $(whoami) = 'root' ]; then echo "root"; else echo "not root"; fi
not root
Great, we have received a message which says 'not root'. Now we will write the same logic in the bash script, which is easier to understand and reuse effortlessly.
#!/bin/bash
if [$(whoami) == 'root'] ;
then echo "Hello Boss! I hope you are doing well." ;
else echo "Please wait until boss allows you in the party!";
fi
$ bash run.sh
Please wait until boss allows you in the party!
As the code/logic gets more complicated, it will be more difficult to read and edit the code. Thus, it is much nicer to write bash scripts to be able to reuse them and write/edit more efficiently.
If you are interested in learning more about bash scripting, check out some more topics in our blog section. If you are interested in acquiring your own computing space, don't hesitate to contact Uni Datacenter at info@uni-datacenter.com or visit www.uni-datacenter.com .