Baeldung Pro – Linux – NPI EA (cat = Baeldung on Linux)
announcement - icon

Learn through the super-clean Baeldung Pro experience:

>> Membership and Baeldung Pro.

No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.

Partner – Orkes – NPI EA (tag=Kubernetes)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

1. Introduction

Maintaining readability and reliability in shell scripting requires adhering to standard naming conventions when naming variables. This can improve comprehensibility and precision in scripts, making them easy to update or fix.

In this tutorial, we’ll cover some conventions for naming shell variables in Bash.

2. Naming Conventions for Shell Variables

Naming conventions are rules and standards used in Bash scripting and coding to name variables, methods, and other programming elements. Also, they help developers quickly comprehend the purpose and diversity of various elements.

Here, we’ll discuss some standard conventions for naming the shell variables.

2.1. Meaningful Variable Names

We must select names for variables that accurately represent their intended purpose and declare variables with accurate names that people can understand.

We’ve initialized two separate meaningful and human-understandable shell variables, city and country, with the = sign followed by the string values “Paris” and “America” respectively.

Meanwhile, the echo statements are printing the values of each variable:

$ city="Paris"
$ echo $city
Paris
$ country="America"
$ echo $country
America

Additionally, we can also assign meaningful names to variables having integer values. For instance, the variable name num1 represents the first integer variable having a value of 20 while the num2 represents the second integer variable having a value of 50:

$ num1=20
$ num2=50
$ echo $num1 and $num2
20 and 50

Finally, the above code snippet depicts the meaningful variable names.

2.2. Uppercase for System Environment Variables

Environment variables are conventionally upper-case shell variables presented by the operating system and marked as unique. Additionally, an underscore can also be incorporated to name environmental variables.

To demonstrate that, we’ve utilized the echo statements in the shell to print the values of a few different and most used environment variables.

Each environment variable serves a different purpose and stores a different value:

$ echo $HOME
/home/aqsa
$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin
$ echo $BASH_VERSION
5.1.16(1)-release
$ echo $SHELL
/bin/bash

Here, the above script shows that the environment variables are presented in upper case and follow the naming convention of shell variables.

2.3. Lowercase for User-Defined Shell Variables

As all environmental variables are conventionally uppercase, it is recommended that user-defined variables be named in lowercase.

Let’s look at an example of initializing lowercase user-defined shell variables.

Here, we’re initializing a lowercase shell variable “name” with a string value “Peter”:

$ name="Peter"
$ echo $name
Peter

So, we’ve used lowercase for our user-defined shell variables, following the basic naming convention for shell variables.

2.4. Use of Underscore

Sometimes, a variable name can be a combination of multiple words. In such cases, using an underscore to separate each word and avoid spaces is suggested:

$ first_name="Liza"
$ echo $first_name
Liza

For example, the above code snippet includes the combination of the word first and name separated by an underscore to form a valid variable first_name having a value Liza.

2.5. Avoid Special Characters

Each special character in shell scripting is specific for performing different functions. Therefore, a shell variable shouldn’t have any special character except the underscore, as it can lead to errors.

Now, let’s take a look at it with the help of the following demonstration:

$ @name="LIZA"
@name=LIZA: command not found

$ first@name="LIZA"
first@name=LIZA: command not found

In the example above, attempting to use @ as part of the variable name gives us an error.

Another example of using special characters that leads to an error can be seen here:

$ n%m=21
n%m=21: command not found

Here, we’ve used the % character in the middle for the variable initialization n%m, which leads to an error.

Now, we’re using another special character * here:

$ first*name="Peter"
first*name=Peter: command not found

Using the * character between two random words first and name again displays an error.

Also, let’s have a look at the usage of another special character $ that leads to an error:

$ $name="John"
Peter=John: command not found

Here, using a $ sign to start a variable name is also an incorrect practice.

Note: The $ character is used to access a variable value in shell scripting:

$ first name="LIZA"
first: command not found

Like other special characters, whitespace should be avoided in between to name a variable comprising multiple words. Here, we’ve tried to initialize a variable first name but have failed to do so.

2.6. Avoid Spaces

While initializing shell variables, the whitespaces (spaces or tabs) should be avoided. Any whitespace before or after the assignment operator leads to an error.

To demonstrate that, let’s have an example of using the whitespace on either side of the = operator:

$ var = "Elena"
var: command not found

As in the above script, initializing a variable var with whitespaces on both sides of the assignment operator = leads to an error:

$ var= "Elena"
Elena: command not found

Here, initializing a variable var with whitespace after the assignment operator = also leads to a command not found error:

$ var ="Elena"
var: command not found

Just like that, initializing a variable var with whitespace before the assignment operator leads to the same error:

$ var="Elena"

Meanwhile, avoiding whitespaces at both sides of the assignment operator leads to the successful initialization of variable var:

$ echo $var
Elena

So, it is clear now that avoiding extra spaces leads to successful script execution.

2.7. Avoid Numbers at the Start

While naming shell variables, it’s a significant naming convention to evade using an integer at the start of a variable name.

Here is an illustration of using an integer as the first character while naming a variable in shell scripting:

$ 1v="Maria"
1v=Maria: command not found
$ v1="Maria"
$ echo $v1
Maria

Here, using 1 as the first character to name a variable leads to an error. Moreover, we’re using the number 1 as the last character to name a variable, which leads to successful initialization.

2.8. Avoid Reserved Words

Avoid using reserved words when naming shell variables. For instance, names of loops, statements, or break statements shouldn’t be used as variable names:

$ if="Paris"
$ echo $if
Paris
$ while=21
$ echo $while
21

Here, the above script utilizes if and while as variable names (specific to the if statement and while loop). However, the best practice is to avoid all the reserved words.

3. Conclusion

In this article, we’ve seen some of the most followed conventions for naming shell variables. These include the use of uppercase and lowercase alphanumeric words and underscore. Finally, we’ve also demonstrated how to avoid using special characters, reserved words, and numbers.