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

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=
    fname=””
    If you tries to print value for these variable nothing will be printed on screen.

 

By | 2017-07-21T21:54:24+00:00 August 8th, 2013|Unix|0 Comments

About the Author:

Leave A Comment