As we had discussed in previous chapters/topics now we are going to create our first bash script.
We had seen while learning various languages, the first program we write is the old “Hello World” program. Generally, I don’t like that so, I’m going to do some modification in that simple program and write a new code.
Step 1: Open your shell of terminal and find the folder or directory where you want to save your script, using the “cd” command.
cd folder_name
Step 2: Now we will generate an empty file with .sh extension using touch command.
touch file_name.sh
Step 3: Now we will open the recently generated file using any text editor. If you are using your default text editor, then you can open it using your terminal
Just type: gedit a.sh/
Step 4: Now we are going to write the code.
#! /bin/bash
# Now we are going to write a basic bash shell script
# For now we will only use the echo command
echo "Bash shell scripts can be very handy if we use it nicely :) "
Here, Line 1 signifies #! (shebang) and denotes the bash shell location.
Line 2 symbolizes the commented line.
Line 3 represents echo command to print the output.
Step 5: So, we have written the code for our 1st bash shell script so are going to execute the a.sh file, to run the a.sh file we have to use a command: ./a.sh .
So in the above image you can see the output.
But when you try to execute the file it throw an error, which is permission denied , so fix that error you can simply add:
chmod +x file_name.sh
then you will be able see the output according to your code.