The first step into linux programming generally follows the footprints of a generic roadmap, which suggests learning shell scripting.
But why?
Now that’s what an open-source enthusiast like me would be willing to find out.
So, why should you waste your time learning a whole new language when you already know a bunch of them?
Things that can be done with just a couple of commands typed in the terminal or shell are quite fascinating to a new Linux user, such as the fact that you don’t need to open your file to delete that particular line number 56; you can do it all from your shell.
Well, that Linux user is living in a delusion.
In reality, these commands are layered with the GUI to perform tasks, making things accessible for a normal user. For us developers, we don’t need that layer; we just need to learn shell scripting!
Now that you’ve got your answer, let’s proceed.
But what exactly is a shell?
A shell is a program, or rather a management system, whose task is to exchange the execution instructions between the kernel and a program.
Suppose I want to play a Taylor Swift song that is saved as an mp3 audio file in my :/music directory. I would just open a music player, navigate to the music directory, and play the song.
But there’s more that goes on behind the scenes.
Firstly, the shell receives the command to navigate to the music directory, and then the kernel receives the command to execute the mp3 file through the shell. And Success.
In this tutorial, I’ll be focusing on BASH scripting, as it is probably the most popular and feature-rich scripting language out there.
Running your first shell script
Make a file with the extension sh.The extension is not necessary, but it helps text editors with syntax formatting. Now open the file in any text editor of your choice.
Add #! /usr/bin/bash known as shebang in the starting of the file. The path there is the location of bash shell present in your OS.
This helps the kernel identify it as a shell file.
Before writing anything else, let’s get familiar with the shell commands:
echo :similar to print
‘read’: Accepts input.
These two basic commands would be sufficient for the starter.
But you also need to know how to create a variable. In shell language, we don’t initialise the variables with any type or value. Just the name of the variable is what goes into its use. As for the usecase, a dollor sign is appended at the beginning of the variable name. And that’s it.
We’ve covered every aspect of writing a basic shell program.
Now, let’s write the program.
#! /usr/bin/bash
read name #creating a name variable to take input from the user
echo "my name is $name" #printing the name
This programme should read your name and return the string, “My name is —–.”
But it seems like the programme ran into the error “permission denied.”
No need to curse the code or your programming skills, the code is fine and so are your skills. It’s just that your script file is in no mood to be executed. You’ll have to change its mood. and that can be done by using chmod commmand.
use chmod +x to add eXecutable permission to the file and then execute the file.