Bash shell scripting is a very cool concept, disguised by a very prosaic title.
The concept is that anything that goes on in the command line can go on in a shell script.
So, why would anyone want to do anything in a shell script, that they could already do in the command line?
I’m glad you asked; because I made a list. 😀
The Benefits of Shell Scripts
Very briefly, some of the most obvious benefits of using (and writing) shell scripts are their ability to:
– Automate Tasks
– Combine Commands
– Save Time/Typing
– Perform Commands Sequentially
– Save Long/Complex Commands
– Perform Advanced Piping
– Test for Conditions
– Schedule Tasks (With Cron)
– Allow User Interaction/Input
In case these points are too brief to make much sense just now, there are also some long-winded explanations available:
– Shell scripts can save commands and/or a series of commands that you would otherwise have to type out more than once.
– Shell scripts allow commands to be combined and carried out in sequential order.
– Shell scripts can automate tasks so that you do not have to continually perform them. (Cron is helpful with this; we’ll explore it later.)
– Shell scripts can handle input and output of commands in a more advanced — or organized — manner than piping.
– Shell scripts allow for tests to be made, so that various results can be achieved based on conditions set in place.
Your First Shell Script
Before we get into the actual shell scripting part, let’s take a look at an example script, just to prove that’s it not too scary.
And please note that I’m going to deliberately forego the whole “Hello world!” thing that you will find just about anywhere that you go to learn about scripting, programming, or related topics… it gets old fast!
#!/bin/bash # My First Bash Shell Script clear ls -l echo "Can you see me?" echo "Yes? Ok, let's move on." exit 0
In coming lessons we’ll not only break down exactly what you are seeing here and how to execute it, but also how to make it so much better.
As I discovered when I wrote and successfully executed my very first shell script, it’s surprisingly easy to learn, because anyone who knows their way around the command line has learned most of it already!