Variables in linux shell

Variables are key-value pairs which are used by different processes in Linux. There are 4 types of variables.
  • Temporary local Shell variables (Only available in current shell)
  • Temporary Shell Environment variables (available in current shell, processes spawned by current shell, and child shells)
  • Permanent User Environment Variables (stored and exported in .bashrc, .bash_profile, .bash_login, .profile)
  • System environment permanent variables (stored in /etc/environment, /etc/profile, /etc/profile.d/, /etc/bash.bashrc)

Creating temporary local shell variables

To create local shell variable, you can execute below command. Here we are creating variables with name “var1” and it’s value is “value1”.

set command can be used to see all local variables as well as environment variables.

Creating temporary environment variables

To create Global shell variable, you can execute below command. This variable can be accessed in child processes and child shells.
export var1=value1

Below image shows how you can create your own variables and then convert them into global variables. In below example, we have created new variable called as “SAGAR” and assigned it the value as “Hello”. This is a temporary variable (Also called as Session Variable). To convert it into temporary environment variable, we have used export command. Note that exported variables are only available in child processes. Also next time if you open terminal and view the content of any exported variables, you will see nothing. To make the change permanent, you have to put that variable in .bashrc file under your home directory.

Creating Environment Variables

Global shell variables can be accessed in child shells and processes. But the problem is that these variables will be erased as soon as we close current shell terminal. That’s when environment variables come into picture. To convert the exported global variables into environment variables, you need to store below command in .bashrc or your login profile file. So that every time you login or open the shell, those variables can be accessed
export var1=value1

This is called as user defined environment variables. But there are many built-in environment variables that you can access. Here is the list of some of the system built-in environment variables.
echo $BASH  – displays the path of bash shell.
echo $HOME – displays the home directory of current user.
echo $OSTYPE – displays the OS type.
echo $SHELL – displays current shell type.
echo $PWD – displays present working directory.
echo $PATH – displays contents of the PATH variable.

Permanent User Environment Variables

You can view all the environment variables by using below command.

or you can also use “env” command to print the environment variables. Note that these command will not show local shell variables. Sample output of above command is given below.
printenv output

To view specific variable value, you can use any of the below commands.
echo $HOME
printenv HOME

Editing Environment Variables

You can edit the environment variables by using below command. Editing the variable is very simple. Just set the new value for the variable at command prompt. Remember that changes made to the environment variables within shell are temporary. For example – If you want to change the variable called PWD, you can do it using below command.
PWD=”new directory name”

Deleting the Environment Variables

You can delete the variables by using unset command.
unset SAGAR

Special variables in Linux

  • $# – This displays total number of command line arguments.
  • $* and $@ – This displays all arguments to the shell.
  • $$ – This displays the process id of current shell.
  • $! – This displays the process id of the last background process started with & symbol.
  • $0, $1, $2 and so on – $0 is the name of the command running at the moment. $1 is the first argument to the command. $2 is the second argument to the command and so on.
  • Storing environment variables
You should store all environment variables in any of below files.
  • ~/.bashrc – This is user specific file. This file is sourced every time you open new bash shell.
  • ~/.bash_profile – This is user specific file.
  • ~/.bash_login – This is user specific file. Variables are initialized from this file at the start of login shell.
But you can also store the variables in below files. Variables in below files will be available in all applications (not only bash)
  • /etc/environment – Variables stored in this file are available across system and for every user. Also these variables will be available local as well remote sessions.
  • /etc/profile – Variables in this file will not be accessible for local login session.
  • /etc/bash.bashrc – Variables are available for local user sessions.
You can get more information about start up files using below command.
$ man bash

Web development and Automation testing

solutions delivered!!