Variables in Shell Scripting | Shell Scripting tutorial
In previous article we learnt what is shell, what is a shell script. We also learnt about what is Kernel, Shell and Terminal. In this chapter, we will learn how to use Shell variables in Unix. If you didn't read the previous article then I kindly request you to please read it because these are key terminologies in Linux shell scripting you must know to get started.
In Linux shell script variable is a string or a character which have a value assigned. The value assigned could be a number, text, filename, device, or any other type of data. A variable is nothing more than a pointer to the actual data.
Variable Naming Convention
A variable name could contain any alphabet (a-z, A-Z), any digits (0-9), and an underscore ( _ ). However, a variable name must start with an alphabet or underscore. It can never start with a number.
Find below some examples of valid variables in shell scripting.
govind
_Govind_3
Govind12
GOVIND
Note: It must be noted that no other special character except underscore ( _ ) can be used in a variable name because all other special characters have special meanings and are reserved in Shell Scripting.
Syntax for defining variable in Shell Script
var_name = <var_value>
These are called scalar variables and they holds one value at a time. We can also define readonly
variables that later can't be modified and can only be read. For that you can follow this syntax readonly var_name= var_value
Accessing variable in Shell Script
Variables defined could be accessed by appending the variable name with ‘$’
#!/bin/bash
NAME="GOVIND"
AGE=24
echo "I am $NAME and I am $AGE years old"
When we declare a variable by assigning a value or data to it we store it in buffer memory. We can delete that variable and remote it from memory using unset
command. Please find below execution of unset
command with a example.
#!/bin/bash
NAME="GOVIND"
AGE=24
echo "I am $NAME and I am $AGE years old"
unset NAME
# Now prining NAME will not work
echo "$NAME"
Variable Types
There are 3 types of variable in Shell Script
Environment variable: Environment variables are available to all programs, not just one. Environment variables are only created once, after which they can be used by any user or in any shell script.
export PATH=/usr/local/bin:$PATH
export PRO_USER="GOVIND"
Shell variable: Shell variables are specific to the shell session you're using; they aren't seen by other programs running on your computer.
`$PWD` = to store working directory
`$HOME` = to store user’s home directory
`$SHELL` = to store the path to the shell program that is being used.
Local variable: Local variables are only available in one particular shell session in which they are declared. They cannot be accessed from outside or any other shell session. We already seen how to use local variables in shell.
This is all about the variables in Shell scripting if you know anything more then please feel free to comment it. Please subscribe CodeManTalks for more updates.