As you start getting advanced with Linux, you will want to start creating your own automated tasks and programs. These are essentially Bash Shell scripts and those scripts work almost in the same way as any other programming language.
A Bash Script is just a series of commands that Linux will run through to complete a certain task. The script can be simple or complex to understand, completely depending on the situation. Let’s jump into it.
You must have some basic knowledge of Terminal commands and any text editor can be used.
1. Before we begin, run the following command for the customary update check.
$ sudo apt-get update && sudo apt-get upgrade
2. Before we start creating any scripts, we create a folder where we can put all our scripts into using the following commands.
$ mkdir scripts
$ cd scripts
3. The bash script files are given (.sh) extension.
4. Let’s create our first simple script to output something in the terminal itself. First, open a script file using any text editor. I am using Leafpad. Add the following line : #!/bin/bash, then go to the next line and add : echo Hello Etechwall !.
5. The #!/bin/bash tells the system that you are going to use the Bash shell. The (#) denotes a commented line that is ignored by the system. The (!) exclamation mark will force the script to execute the line as a command. This (#!/bin/bash) is known as Hash-Bang.
6. Save the file and go back to the terminal. Entering the “ls” command will show you all the files. Our file for now is not executable, to make it executable. Use “chmod +x etechwall.sh“. Now the script is executable, to run the script – enter “./etechwall.sh“.
7. The output is displayed in the terminal. The echo command is responsible for this outputting. You can add any number of words after echo in the same line only.
8. Try this example using echo : “echo Hello Etechwall ! Today is $(date +%A)“. The $(date +%A) will call the system variable which stores the current day of the week.
This is enough for this section. We will be back with our next part. Stay Tuned !
More on Related Topics :