Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sd031/548f4ad781f7a10626e397507c3b0b7e to your computer and use it in GitHub Desktop.
Save sd031/548f4ad781f7a10626e397507c3b0b7e to your computer and use it in GitHub Desktop.
This Gist contains basic shell scripting and their meaning, this gist's sole purpose is to help students learn basic file management commands.
Basic Linux Shell Scripting:
What is Shell?
The shell executes a program in response to its prompt. When you give a command, the shell searches for the program, and then executes it. For example, when you give the command ls, the shell searches for the utility/program named ls, and then runs it in the shell. The arguments and the options that you provide with the utilities can impact the result that you get. The shell is also known as a CLI, or command line interface.
Types of shells:
Primaryly there is two types of shell:
Bourne shell − If you are using a Bourne-type shell, the $ character is the default prompt.
It has sub categories: Bourne shell (sh), Korn shell (ksh), Bourne Again shell (bash), POSIX shell (sh)
C shell − If you are using a C-type shell, the % character is the default prompt.
Subcategories: C shell (csh), TENEX/TOPS C shell (tcsh)
Out of which below are widely used:
BASH (Bourne Again SHell) – It is most widely used shell in Linux systems. It is used as default login shell in Linux systems and in macOS. It can also be installed on Windows OS.
CSH (C SHell) – The C shell’s syntax and usage are very similar to the C programming language.
KSH (Korn SHell) – The Korn Shell also was the base for the POSIX Shell standard specifications etc.
To check which shell you are using run below command:
echo $SHELL
What is Shell Scripting?
I liked this defination: "A shell script is a text file that contains a sequence of commands for a UNIX-based operating system. It's called a shell script because it combines into a "script" in a single file a sequence of commands that would otherwise have to be presented to the system from a keyboard one at a time."
To make the script file executeable, use below command:
chmod +x file_path
e.g. chmod +x hellow_world.sh
Let's get started with our first sheel script:
filename: hellow_world.sh
Dile Contents:
#!/bin/bash
echo "Hello World"
One important thing we must know here:
Shell Scripting She-bang:
The sign #! is called she-bang and is written at top of the script. It passes instruction to program /bin/sh.
To run your script in a certain shell (shell should be supported by your system), start your script with #! followed by the shell name.
Example:
#!/bin/bash
echo Hello World
#!/bin/ksh
echo Hello World
Using Variable in Shell Scripting:
File name: test.sh
Contents:
#!/bin/bash
NAME="Sandip"
echo $NAME
echo "$NAME"
echo "${NAME}!"
Writing interactive shell script:
filename: test_2.sh
Contents:
#! /bin/bash
echo "Your First Name?";
read a;
echo "Hi $a, Your Last Name";
read b;
echo "Thanks $a, Your full name is $a $b for telling us your name";
What next:
string manipulation, funtions, for loop, conditional statements (e.g. if else), arrays, iterations and more
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment