I’m pretty sure people working in the tech domain are aware of something called Shell Scripting. Were you ever working on a project and felt that some tasks were repeating over and over again? Did you feel that there should be an easy way to automate these things and you don’t have to worry about it every single time you want to run your project? People who have heard about shell scripting and don’t know what it is, you are missing out on a cool weapon in your arsenal. People who haven’t heard about shell scripting, well it’s time to move you into the other category. So what exactly is shell scripting?
What the shell!
To understand shell scripting, we need to know what a shell is. Basically, a shell is an interface between the user and the operating system. On any Unix based machine (Unix, Linux or Mac), you can just open the terminal and you can see the command-line shell. You can parse the directories, move or remove files, display files etc with specific commands. There are GUI shells as well (like the Windows shell), but they don’t give as much freedom to automate as we want. So when we talk about shells, it usually refers to the command-line interface.
Script me up
Now that we know what a shell is, let’s see what scripting means. Scripts are used to automate the execution of tasks that would otherwise need humans to manually do them. For example, let’s say you want to run your project. Every time you want to do it, you have to navigate to the directory, check for redundant files, delete them and run your code. If you don’t want to do it manually everytime, you can always write a script and it will do all these things for you. Scripting refers to the process of writing such scripts.
The sum of its parts.
Combining the above two things, shell scripting means that you are writing code to automate various tasks on the shell. It’s just like another programming language. Why do you need that? Well, a lot of times, you have to parse through huge number of files stored in many subdirectories and collect information, or run a lot of different executables located in different directories whose inputs are dependent on previous outputs from other executables, and so on. When you have to do this every time you change your code, it becomes very tedious and difficult to keep track of it manually.
It starts with a bang!
All shell scripts on *nix machines start with something called hashbang. It’s pound sign followed by the exclamation mark: ‘#!’. The filename has a ‘.sh’ extension. When you run this, the program loader parses the rest of the line and considers that path to be the interpreter program. The remaining lines look pretty much like the commands on the command-line interface. You can use conditional statements and loops as well. The Windows equivalent of this would be a batch file. These files have ‘.bat’ extension and they usually contain a series of commands to be executed on the command line.
A language like Python can do everything you just said. Why do I need shell scripting?
Python is a general-purpose programming language which can be used for both scripting and non-scripting. Shell scripting, on the other hand, is meant exclusively for this purpose. It means you have very close control of what’s happening and it’s more native. It depends on what exactly you want to do. If you have Python code and you just want to parse through a few files, you should do it within your Python code. But if you want to do a lot of file manipulation and interaction with your OS and executables, then shell scripting would be the way to go.
2 thoughts on “Inside The Shell of Shell Scripting”
you can use python -c within a shell script too
Just like you can use “os.system” within Python.