Bash stands for ‘Bourne Again Shell’. Bash is a shell or command language interpreter for the GNU operating system. (GNU is basically a project to create a free operating system, wholly consisting of free softwares. It was initiated by Richard Stallman and was publicly announced in september 27, 1983. The Linux we use today is actually GNU-Linux). It is the default login shell for linux and most of the operating systems. You can also call it a command processor as it runs in a text window and takes commands from the user, executes it and performs actions accordingly. It can also read and execute commands from a file, called shell script. Like Unix shells, Bash also supports - wildcard matching, piping, here documents, command substitution, variables and control structures.
Bash (like many other shells) also has the ability to run an entire script of commands, known as a "Bash shell script" (or "Bash script" or "shell script" or just "script"). A script might contain just a very simple list of commands — or even just a single command — or it might contain functions, loops, conditional constructs, and all the other hallmarks of imperative programming. In effect, a Bash shell script is a computer program written in the Bash programming language.
Sh shell, C Shell, Z Shell, Fish Shell, Korn Shell, etc.
All you need is a plain text editor which usually comes with each and every operating system.
Nano, Vim or Vi, emacs, gedit, kate, text-editor, notepad, etc.
Open up a text-editor. Create a new file. Name it anything you want and save it with .sh extension.
Now the first line of a shell script should always start with a sha-bang or hash-bang => ‘#!’. It is a good practice to do this. This tells the system that it is a shell script. After that we give the location of the shell we write our script for, in this case it is Bash => ‘/bin/bash’
This is how it looks like:
Note: ‘#’ without ‘!’ is used to write comments in shell scripts. Everything after ‘#’ in that line is ignored by the system.
Let's write a simple hello world shell script
#Usage: Hello World Bash Shell Script
#Author: Chaitanya Chaturvedi
echo "Hello!, World."
Now save the file. Open the terminal. Go to the locations where you script is located.
For the script to be executed by Bash, we first need to make it executable. We do that by
$chmod +x script-name.sh
And then execute it by
Before we move forward lets get familiar with some shell commands and variables.
Echo is used to print text to the screen.
echo “Hello!, World”
Read is used to get input from the user
Where n is a variable
We can declare a variable like
And then can use it anywhere like
We can create new file using touch
This will create a file named new_file
To be continued.
Thanks for reading :)