Shell scripting – Introduction & Variables

Shell scripting

If you are a pentester, system administrator or you know someone who has these jobs, you would probably know that the daily task of these jobs includes executing hundreds of commands, a smart work here will be a single command doing all the work for us, this can be done by a shell script in UNIX- like systems, in windows it’s the batch script that do the same work.

What is a shell script?

shell script is a computer program designed to be run by the UNIX shell, a command-line interpreter. The various dialects of shell scripts are considered to be scripting languages. Typical operations performed by shell scripts include file manipulation, program execution, and printing text. A script which sets up the environment runs the program, and does any necessary cleanup, logging, etc. is called a wrapper.

The term is also used more generally to mean the automated mode of running an operating system shell; in specific operating systems they are called other things such as batch files (MSDos-Win95 stream, OS/2), command procedures (VMS), and shell scripts (Windows NT stream and third-party derivatives like 4NT—article is at cmd.exe), and mainframe operating systems are associated with a number of terms

Why use shell scripts?

  • Quickstart
  • Batch jobs
  • Interactive debugging
  • Provides special shortcuts

A key feature of shell scripts is that the invocation of their interpreters is handled as a core operating system feature. So rather than a user’s shell only being able to execute scripts in that shell’s language, or a script only having its interpreter directive handled correctly if it was run from a shell (both of which were limitations in the early Bourne shell’s handling of scripts), shell scripts are set up and executed by the OS itself. A modern shell script is not just on the same footing as system commands, but rather many system commands are actually shell scripts (or more generally, scripts, since some of them are not interpreted by a shell, but instead by Perl, Python, or some other language). This extends to returning exit codes like other system utilities to indicate success or failure and allows them to be called as components of larger programs regardless of how those larger tools are implemented.


The first line(shebang) indicates which interpreter should execute the rest of the script.

Shell script example:


echo “hello world”

to run this script, you have to give it the required permissions and for that use command

chmod +x

In this shell script video series detailed introduction, functionalities, different commands use have been illustrated.

Shell script variable

The names of a variable can contain only letters (a to z or A to Z), numbers (0 to 9) or the underscore character (_).

By convention, Unix shell variables will have their names in UPPERCASE.

The following examples are valid variable names −

  • VAR_1
  • VAR_2

Accessing variables

To access the value stored in a variable, prefix its name with the dollar sign ($) −

For example, the following script will access the value of defined variable NAME and print it on STDOUT:




echo $NAME

The above script will produce the following value −


Variable Types

When a shell is running, three main types of variables are present −

  • Local Variables − A local variable is a variable that is present within the current instance of the shell. It is not available to programs that are started by the shell. They are set at the command prompt.
  • Environment Variables − An environment variable is available to any child process of the shell. Some programs need environment variables in order to function correctly. Usually, a shell script defines only those environment variables that are needed by the programs that it runs.
  • Shell Variables − A shell variable is a special variable that is set by the shell and is required by the shell in order to function correctly. Some of these variables are environment variables whereas others are local variables.

You can see a practical demonstration of the above illustration in the videos above and coming videos, hence keep watching keep learning.


Liked it? Take a second to support Alexis on Patreon!
Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *