What is Linux Shell?
Computer only understand the language of 0’s and 1’s which called binary language. In early days of computing, instruction are provided using binary language, which is difficult to understand. So in OS there is special program called Shell. Shell accepts our instruction or commands in English (mostly) and if the commands are valid, it is pass to Kernel.
Several shell comes with Linux Operating System, They are:
Shell Name | Developed by | Where | Remark |
BASH(Bourne-Again SHell) | Brian Fox and Chet Ramey | Free Software Foundation | Most common shell in Linux. It’s Freeware shell. |
CSH (C SHell) | Bill Joy | University of California (For BSD) | The C shell’s syntax and usage are very similar to the C programming language. |
KSH (Korn SHell) | David Korn | AT & T Bell Labs | — |
TCSH | See the man page. Type $ man tcsh |
— | TCSH is an enhanced but completely compatible version of the Berkeley UNIX C shell (CSH). |
This Command will help you to find all available shells in your system:
$ cat /etc/shells
This Command will help you to find your current shell
$ echo $SHELL
What is Shell Script?
Normally shells script is nothing, but it’s everything. If you use command one by one, then you can only see only the single command at a time. But if you use Script, then you can create many commands written in plain text file, which is known as shell script. Shell script is just like batch file is MS-DOS but have more power than the MS-DOS batch file.
Why need to Write Shell Script?
- Shell script can take more than one input from the user and give output on the screen.
- Save lots of time.
- To automate some task of day today life. (i.e. poweroff, sleep, reboot, update and upgrades etc)
- System Administration part can be also automated.
Objective of this Tutorial
- Try to understand the basics of Linux shell.
- Try to learn the Linux shell programming.
Getting started with Shell Programming
In this article you can understand shell programming, write scripts and execute them.
How to write shell script
In these way you can write a shell script:
- Open any editor like vi,nano or gedit to write script.
- After writing shell script you need to set execute permission for run the script-
Syntax: chmod permission your-script-name
To execute your script file
Syntax:
$ chmod +x your-script-name $ ./your-script-name
Examples:
$ chmod +x HelloWorld
NOTE:-
In the last syntax ./ means current directory, But only . (dot) means execute given command file in current shell without starting the new copy of shell.
After saving your script, you can run the script using this command:
$ ./HelloWorld
Script Command(s) |
Meaning |
$ vi script-name/file-name |
Start vi editor |
clear |
clear the screen |
echo “Hello World, Welcome to FOSS Lovers “ |
To print message or value of variables on screen, we use echo command, general form of echo command is as follows |
You can also create a simple script like this:
1. Open any editor like vi,nano or gedit to write script. I’m use vi editor to write this script.
2. Now you need to set execute permission for run the script file.
# chmod +x d.sh
3. After set the permission, you can run this script using this command:
$ ./d.sh
But Some of the above settings can be different in your Linux System.
$USER = Show the Current User Name Who Login in the system.
Sleep = Break between two commands.
echo = Use echo command to display text or value of variable.
read = Use for user-created variables is storing information that a user enters in response to a prompt.
Other Important Commands
If you want to print your home directory location then you give command:
a) $ echo $HOME
More about Quotes & Commands
Quotes |
Name | Meaning |
“ | Double Quotes | “Double Quotes” – Anything enclose in double quotes removed meaning of that characters (except \ and $). |
‘ | Single quotes | ‘Single quotes’ – Enclosed in single quotes remains unchanged. |
; | Semi colon | Semi colon (;) – To makes it possible to run, several commands in a single go and the execution of command occurs sequentially. |
& | Ampersand | The function of ‘&’ is to make the command run in background. Just type the command followed with a white space and ‘&’. User can execute more than one command in the background, in a single go. |
sleep | Sleep | To pause for some time in users bash shell script. |
read | Read Variable | Use for user-created variables is storing information that a user enters in response to a prompt. |
| | Pipe |
A pipe is a way to connect the output of one program to the input of another program without any temporary file. |
Pipes:
A pipe is a way to connect the output of one program to the input of another program without any temporary file.
The Pipe command work looks like:-
Syntax:
command1 | command2
Examles:
Command using Pipes |
Meaning or Use of Pipes |
$ ls | more |
Output of ls command is given as input to more command So that output is printed one screen full page at a time. |
$ who | sort |
Output of who command is given as input to sort command So that it will print sorted list of users |
$ who | sort >user_list |
Same as above except output of sort is send to (redirected) user_list file |
$ who | wc -l |
Output of who command is given as input to wc command So that it will number of user who logon to system |
$ ls -l | wc -l |
Output of ls command is given as input to wc command So that it will print number of files in current directory. |
$ who | grepraju |
Output of who command is given as input to grep command So that it will print if particular user name if he is logon or nothing is printed (To see particular user is logon or not) |
Conclusion
I think, by this article, you should have a Basic Idea About Linux Shell Scripting. You should also know some basic important commands that will help you to create shell script. Finally, you should be comfortable to create Shell Script Files in Linux Distribution. Thank You.
Kool Arpan is a tech enthusiastic who loves blogging and writing articles on technology. He loves sharing knowledge to the others by any means and medium.
Wow! Nice Article bro. Hat’s off you.