Monday, 14 March 2016

Unix/Linux Shell Variables

Variables in Unix/Linux Shell

To process our data/information, data must be kept in computers RAM memory. RAM memory is divided into small locations, and each location had unique number called memory location/address, which is used to hold our data. Programmer can give a unique name to this memory location/address called memory variable or variable (Its a named storage location that may take different values, but only one at a time).

In Unix/Linux(Shell), there are two types of variable:

1)  System variables : 

    Created and maintained by Linux itself. This type of variable defined in CAPITAL LETTERS.
    You can see system variables by giving command like $ set, There is a long list of system variable
    so if you interested to see all the system variable then you should redirect the all system variable
    into a text file by using command as "set > systemVariable.txt". Now check systemVariable.txt in
    your current directory.

Some of the important System variables are as following:
   
System Variable Description
BASH=/bin/bash System Shell Name
BASH_VERSION=4.2.25(1)-release System Shell Version Name
COLUMNS=80 Number of Column for your screen
LINES=34 Number of Lines for your screen
HOME=/home/mukesh Your Shell's Home Directory
LOGNAME=mukesh Logging Name of Current System
OSTYPE=Linux Our Operating System Type
PATH=/usr/bin:/sbin:/bin:/usr/sbin Our System's Path Setting
PS1=[\u@\h \W]\$ Our Prompt Settings
SHELL=/bin/bash Our Shell Name
PWD=/home/mukesh/Documents Our Current Working Directory
USERNAME=mukesh Username who is currently log in into system
   
   
    Note :Some of the above settings can be different in your Unix/Linux environment as per your system.
    You can print any of the above variables contains as follows:

    $ echo $USERNAME
    $ echo $BASH_VERSION



2) User defined variables : 
  •   Created and maintained by user. This type of variable defined in lower letters.
  •   User-defined variable can be assigned a value.
  •   The value of a named variable can be retrieved by preceding the variable name with a ‘$’ sign
  •   Used extensively in shell-scripts.
  •   Used for reading data, storing and displaying it.

    Accepting Data and Displaying It on Screen:

  •  To accept input data from user you can use read command.
  •  In shell scripts , read is used for accepting data from the user and storing it for other processing.
  •  Example : $ read yourname

  • Type command "read yourname" and hit enter. Type your name and hit enter again. So now your name has been stored into user defined varaible name yourname.

    Displaying Value:
    To display the value you can use the echo command.
    So to display your name which you have stored into a variable yourname type the following command.

      echo $yourname



No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...