/**/ Using Variables in Shell Scripting - Dextutor - Dextutor

Using Variables in Shell Scripting

Variables are nothing but a placeholder for storing some value or data. Referencing the value of the variable is called variable referencing. Next, we will learn about using variables in shell scripting.

How to assign value to a variable?

The synatx for using a variable in shell scripting is simple

variable_name=value

Remember: Never give space before and after the equal-to (=) sign or else the script will throw an error during execution.

1_variables

var=hello
echo "Value of the variable is $var"

Output:

The variable var is assigned the value hello. Next, echo command prints the value if the variable var. Note hoe we use the ‘$’ sign with the variable name. Whenever you want to use the value of the variable always use the $ sign with the variable name

Using Variables in Shell Scripting

If the $ sign is not used with the variable name then the script will assume you want to work on the variable name and not its value. Modify the above script as below and observe the difference

var=hello
echo "Value of the variable is var"

Do Variables in Shell Scripting has data types?

No, variables in shell scripting has no data types i.e., they are “untyped”. All variables are treated as string by default but depending upon the context they can be used to perform arithmetic and comparison operations too.

#script1.sh
var1="hello world"
var2=20
var3=3+3
echo "Value if var1 is: $var1"
echo "Performing Addition: $(($var2+5))"
echo "Mixed value in var3: $var3"

Special Variable Types

  • Local variables
  • Environmental Variables
  • Positional Parameters

Local Variables

Variables visible only within a code block or function. For example the variables used in the above scripts are local variables

Environmental Variables

Variables that affect the behaviour of the shell and user interface. For example, HOME, SHELL, PATH etc.

#script2.sh: Script to use environmental variables
echo "The user home directory is : $HOME"
echo "The shell used by your system is : $SHELL"

Environmental variables are generally declared in Uppercase. Use the env or printenv command to view the list of all environmental variables along with their values.

Positional Parameters / Command Line arguments

Arguments passed to the script from the command line are called command line arguments. For example,

$ls f1 f2

here the file names f1 and f2 are command line arguments. Similarly,

$touch xyz xyz1 xyz2

are three command line arguments passed with the touch command. The value of these command line arguments is stored as below:

  • $0 – stores the name of the script
  • $1 – stores the first argument
  • $2 – stores the second argument
  • $3 – stores the third argument
  • $# – stores the total number of argument
  • $* – stores the value of all argument
#script3.sh: Script to use command line arguments
echo "The name of the script is: $0"
echo "The first argument passed is: $1"
echo "The second argument passed is: $2"
echo "Total number of arguments passed are: $#"
echo "The value of each argument is: $*"

Output

$chmod u+x script3.sh
./script3.sh first 2nd
passing command line arguments

Video Link

Solved Questions on Using Variables in Shell Scripting

Q1. Write a shell script to create a file whose name is specified by the user. Also, display a confirmation message

echo "enter file name"
read f_name
touch $f_name
echo "$f_name is created"

Q2. Write a shell script to create a file whose name is specified by the user at the command line. Also, display a confirmation message

touch $1
echo "$f_name is created"

Note: while you run the above script do not forget to specify the name as the first argument. For example, if your script name is xyz.sh then run it as
./xyz.sh date.txt

This will create the file date.txt

Practice Questions on Using Variables in Shell Scripting

Q1. Write a shell script to create a file and a directory. The file name should be passed as first argument and the directory name should be passed as second argument.

Q2. Write a shell script that displays the total number of arguments passed and the value of each argument.

Next
if-else statement

Leave a Comment