How to create variable in shell script

Here we will learn to create variables in shell script. Variable are used to hold some values which can be used in other parts of script.

Types of Variable

There are two type of variable used in Shell scripting.

  1. User Defined Variable (UDV)
  2. System Variable

Syntax for creating variable

You need to use “=” sign to create variable.

Example of creating variable


firstName=”Vivekanand Gautam”

Naming Convention for variable

  1. Names must start with a letter.
  2. A name must not contain embedded spaces. Underscores can be used instead.
  3. Punctuation marks can not be used.

Rules Related to Variable (UDV and System Variable)

  1. You can not have spaces between = and variable name and / or value. e.g firstName=Vivekanand  (Valid Syntax)
    firstName =Vivekanand (Invalid Syntax)
    firstName= Vivekanand (Invalid Syntax)
  2. Variable Name should be on left sign of equal sign(=) and variable value should be on right side of equal sign.
  3. Variables are case sensitive. no and No are different in terms of variable names.
  4. Do not use * and ?.. in variable names.
  5. NULL value is treated as No Value. If you want to define variable with NULL value then define variable without any value or empty spaces.  e.g fname=
    If you tries to print value for these variable nothing will be printed on screen.


Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to top Search