Shell variables in Linux are placeholders that stores information in form of variable and value to use it in shell scripts or commands. In shell scripting, the dollar sign ($) is used to access the value stored in a shell variable. When you prepend a variable name with a dollar sign, it is referred to as variable expansion or variable substitution.
Top Shell Variables
- case sensitive
- dollar sign
- creating variables
- quotes
- set
- unset
- path
- env
In shell scripting, case sensitivity refers to the distinction between uppercase and lowercase characters when working with variables. By default, shell variables are case-sensitive, which means that variables with different capitalization are treated as distinct entities. For example, consider two variables, “HOSTNAME” and “hostname”:
In this case, the output is: “Hello MYLINUX” and “Hello”.
As you can see, the two variables are treated as separate entities because they have different capitalization.
However, it’s important to note that the convention in shell scripting is to use uppercase letters for environment variables, while lowercase letters are typically used for user-defined variables. This convention helps to avoid unintentional clashes between user-defined variables and predefined environment variables.
In shell scripting, the dollar sign ($) is used to access the value stored in a shell variable. When you prepend a variable name with a dollar sign, it is referred to as variable expansion or variable substitution.
For example, if you have a variable called “HOSTNAME” with the value “MYLINUX”, you can access its value using the dollar sign notation like this:
To create variables in a shell script, you simply need to assign a value to a variable name. Shell variables can hold various types of data, such as strings, numbers, or even arrays. Here’s the general syntax to create variables in shell scripting:
Syntax: variable_name=value
Here is an example of creating variables
In shell scripting, quotes are used to control the interpretation of special characters and preserve whitespace within a variable’s value. There are three types of quotes commonly used in shell variables:
Single Quotes (‘): When a variable is enclosed in single quotes, the characters within the quotes are treated as literal values. No variable substitution or command substitution takes place within single quotes. For example:
In this case, the variable name is assigned the value ‘Raj’, and when we echo the variable, it is printed as it is without any substitution.
Double Quotes ("
): Double quotes in allow for variable substitution and command substitution within the quotes. Variables inside double quotes are expanded, replacing them with their respective values. For example:
Here, the variable “lname
“ is expanded within the double quotes, and its value is substituted in the output.
Backticks (`
): Backticks, although not technically quotes, are used for command substitution. When a command is enclosed within backticks, it is executed, and the output of the command is substituted into the variable. However, it is recommended to use the '$()
‘ syntax for command substitution instead of backticks, as it is more readable and allows for nesting. For example:
In this example, the “date” command is executed within backticks, and its output (the current date) is assigned to the “date” variable.
In Linux shell scripting, the “set” command is used to modify or display the shell’s internal variables and options. It is a built-in command available in most Unix-like operating systems, including Linux.
In shell scripting on Linux, the “unset” command is used to remove or unset a variable or function. When you unset a variable, its value and existence are both removed from the shell’s environment. Here’s the syntax for using the “unset” command:
Syntax: unset variable_name
Here is an example of “unset” command:
In the above example, we can see the output while we didn’t unset the value of variable $fname.
In Linux, the “$PATH” variable is an environment variable that specifies the directories where the shell looks for executable files when you type a command without specifying its full path. The directories specified in “$PATH” are searched in order from left to right until a matching executable file is found.
When you enter a command in the shell, such as “ls” or “gcc”, the shell will search for the corresponding executable file in the directories listed in the “$PATH” variable. If the executable is found, it is executed; otherwise, you’ll receive a “command not found” error.
The “$PATH” variable is a colon-separated list of directories. Here’s an example of how it might be defined:
The “env” command in shell scripting is used to display or modify the environment variables. It is short for “environment” and is a built-in command available in most Unix-like operating systems, including Linux.
Usage of “env” command:
Displaying Environment Variables: When you run the “env” command without any arguments, it displays a list of all the environment variables and their values. This includes variables such as “HOSTNAME”, “HOME”, “USER”, etc., along with any custom variables that have been set.
Wrapping up
In Linux, shell variables are used to store data and configure the behavior of shell scripts. They are created by assigning values to variable names and can hold strings, numbers, arrays, and more. Shell variables are case-sensitive by default, and quotes can be used to control interpretation. The unset
command removes variables, while env
displays or modifies environment variables. Shell variables are essential for customization and control within the Linux shell environment. If you wish to learn more on Shell Scripting in Linux, please follow this and if you wish to learn about Control Operators, click here.