Open In App

Shell Scripting – Default Shell Variable Value

Last Updated : 27 Jan, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

A shell gives us an interface to the Unix system. While using an operating system, we indirectly interact with the shell. On Linux distribution systems, each time we use a terminal, we interact with the shell. The job of the shell is to interpret or analyze the Unix commands given by users. A shell accepts commands from the user and transforms them into a form that is understandable to the kernel. In other words, it acts as a mediator between ta user and the kernel unit of the operating system. 

This article focuses upon the default shell variable value.

Setting default shell variable value

Undefined variables: A variable that is not defined at the time of declaration is known as an undefined variable. 

Example:

In this program, we are printing two undefined variables; “myVariable1” and “myVariable2”. In the output, nothing gets printed as these variables are undefined. In simple words, they do not have any initial value.

// Shell script to illustrate undefined variables

#!/bin/sh

// Printing undefined variables
echo $myVariable1
echo $myVariable2
  

Output:

Undefined variables

Method 1:

We can set variables with default or initial values in shell scripting during echo command with the help of the assignment operator just after the variable name. The default value with which we want to assign the variable, then, comes after the assignment operator. Below is the syntax to set a default value.

echo ${myVariable1= value1}
echo ${myVariable2= value2}

Here, myVariable1 and myVariable2 are variable names and value1 and value2 are corresponding default values

If “myVariable1” and “myVariable2” are not initialized then the default values, “value1” and “value2” are substituted respectively. Otherwise, the initialized values of “value1” and “value2” is substituted.

Example:

In this program, we have except “myVariable6”, all variables are defined. In simple words, “myVariable6” is initialized whereas other variables are not defined. Thus, myVariable6 has a value initialized at the time of declaration, and other variables have default values. 

# Shell script to illustrate the working
# of default variable value

#!/bin/sh

# Initializing "myVariable6"
myVariable6=25

# Printing variables
echo "myVariable1:" ${myVariable1=10}
echo "myVariable2:" ${myVariable2=1.123}
echo "myVariable3:" ${myVariable3='A'}
echo "myVariable4:" ${myVariable4=true}
echo "myVariable5:" ${myVariable5="GeeksforGeeks"}
echo "myVariable6:" ${myVariable6=100}

Output:

Method 2:

Bash parameter expansion technique is also used to assign variables with default values. In this method also, the variables are assigned with default values during the echo command. But in this case, colon and dash symbols (:-) are used just after the variable name. The default value with which we want to assign the variable, then, comes after these symbols. We can set variables with default or initial values in shell scripting with the help of the below syntax also,

echo ${myVariable1:- value1}
echo ${myVariable2:- value2}

Here, myVariable1 and myVariable2 are variable names, and value1 and value2 are corresponding default values

If “myVariable1” and “myVariable2” are not initialized before then the expansion of “value1” and “value2” is substituted respectively. Otherwise, the initialized value of “value1” and “value2” is substituted.

Example:

In this program, we have except “myVariable6”, all variables are defined. In simple words, “myVariable6” is initialized whereas other variables are not defined. Thus, “myVariable6” has a value initialized at the time of declaration, and other variables have default values. 

# Shell script to illustrate the working
# of default variable value

#!/bin/sh

# Initializing "myVariable6"
myVariable6=25

# Printing variables
echo "myVariable1:" ${myVariable1:-10}
echo "myVariable2:" ${myVariable2:-1.123}
echo "myVariable3:" ${myVariable3:-'A'}
echo "myVariable4:" ${myVariable4:-true}
echo "myVariable5:" ${myVariable5:-"GeeksforGeeks"}
echo "myVariable6:" ${myVariable6:-100}

Output:


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads