What Is a Variable in Shell Scripting?


Larry Thompson

A variable in shell scripting is a named entity that can hold a value or data. It acts as a placeholder for information that can be used throughout the script. Variables make shell scripting flexible and powerful, allowing us to store and manipulate data dynamically.

Declaring Variables

In shell scripting, variables are declared by assigning a value to them using the equal sign (=) operator. Here’s an example of declaring a variable named “name” and assigning it the value “John”:


Variable Naming Conventions:
– Variable names are case-sensitive. – They can contain letters (both uppercase and lowercase), numbers, and underscores (_).

– The first character of the variable name must be a letter or an underscore. – Variable names cannot contain spaces or special characters.

Using Variables

We can access the value stored in a variable by prefixing the variable name with a dollar sign ($). For example:

echo $name

This will output “John”.

Variables can also be used in combination with other text by enclosing them within double quotes (“”). This is called variable substitution. For instance:

echo "My name is $name"

This will output “My name is John”.

Modifying Variables

Variables in shell scripting are mutable, meaning their values can be changed throughout the script. We do this by reassigning a new value to the variable. For example:


Now, if we echo the value of the “name” variable again, it will output “Jane” instead of “John”.

Data Types:
Shell scripting does not have explicit data types for variables. By default, variables are treated as strings. However, they can hold numeric values as well.

List of Commonly Used Shell Variables:

  • $0: The name of the script itself.
  • $1, $2, $3, ..: Positional parameters passed to the script.
  • $#: The number of arguments passed to the script.
  • $?: The exit status of the last executed command.
  • $$: The process ID (PID) of the current shell instance.

Environment Variables:

Shell scripting also provides a set of predefined variables called environment variables. These variables are set and maintained by the system and can be accessed by any shell script. Some common environment variables include:

  • $HOME: The user’s home directory path.
  • $PATH: A colon-separated list of directories where executable files are located.
  • $USER: The current user’s username.


Variables play a crucial role in shell scripting as they allow us to store and manipulate data dynamically. By using variables, we can make our scripts more flexible and powerful. Remember to follow naming conventions and properly declare, access, and modify variables for efficient shell scripting.

Now that you understand what a variable is in shell scripting, you can leverage this knowledge to write more efficient and dynamic scripts. Happy coding!

Discord Server - Web Server - Private Server - DNS Server - Object-Oriented Programming - Scripting - Data Types - Data Structures

Privacy Policy