Shell Scripting – Default Shell Variable Value
Last Updated :
27 Jan, 2022
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:

Similar Reads
Non-linear Components
In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Class Diagram | Unified Modeling Language (UML)
A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Spring Boot Tutorial
Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
AVL Tree Data Structure
An AVL tree defined as a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees for any node cannot be more than one. The absolute difference between the heights of the left subtree and the right subtree for any node is known as the balance factor of
4 min read
Backpropagation in Neural Network
Backpropagation is also known as "Backward Propagation of Errors" and it is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network. In this article we will explore what
10 min read
Linux Commands Cheat Sheet
Linux, often associated with being a complex operating system primarily used by developers, may not necessarily fit that description entirely. While it can initially appear challenging for beginners, once you immerse yourself in the Linux world, you may find it difficult to return to your previous W
13 min read
What is Vacuum Circuit Breaker?
A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac
13 min read
3-Phase Inverter
An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
Polymorphism in Java
Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
Random Forest Algorithm in Machine Learning
A Random Forest is a collection of decision trees that work together to make predictions. In this article, we'll explain how the Random Forest algorithm works and how to use it. Understanding Intuition for Random Forest AlgorithmRandom Forest algorithm is a powerful tree learning technique in Machin
7 min read