BASH Programming

Bash Variable Name Rules: Legal and Illegal

Every programming language has its syntax to declare variable names that a user needs to follow. In the same way, bash scripts have their syntax, some of which are legal while others are illegal, which will be discussed in this article.

There are three different methods that you should consider while declaring a variable which is mentioned below

Let’s start this guide!

Method 1: Declaring a Variable Using Only Alphabets

There are some legal ways and illegal ways as well that you might consider while declaring a variable using only alphabets which are discussed below.

Legal Ways of Declaring Variables Using Only Alphabets

To declare a variable, you can use alphabets that can either be small, capitalized, or their combination as shown in the image below:

$ varname=“Welcome to LinuxHint”

In the above image, we have only used the small letters which is a legal way to run the bash script. Now are going to declare a variable with all capital letters as shown below:

$ VARNAME=“Welcome to LinuxHint”

Next, we will be declaring the variable that contains both small and capital letters as shown below:

$ VaRnAmE=“Welcome to LinuxHint”

Note: We have saved each file with the name of “test.sh”, and you can execute the script by writing the following command:

$ bash test.sh

Illegal Ways of Declaring Variables Using Only Alphabets

If you are trying to separate the words or alphabets by using a space, then it won’t work and is considered to be illegal, as shown below.

$  var name=“Welcome to LinuxHint”

The output of the above bash script when you execute it is mentioned below:

$ bash test.sh

Note: When you are assigning anything to a variable, then there should be no space on both sides of the assignment operator (=) as well.

It is not a good practice to declare variables using reserve words like if, while, and until. Although they are not illegal but using them can be confusing as they are primarily made for another purpose in programming.

Method 2: Combination of Alphabets With a Number

You can combine alphabets with any number as well to declare a variable, but there are some legal and illegal ways to do so as described below:

Legal Ways to Combine Alphabets With a Number

The legal way of combining alphabets with a number is mentioned below:

$varname123=“Welcome to LinuxHint”

You can see in the above image that we have first write the alphabets and then we wrote numbers, but you can also write numbers in between the numbers as followed below:

$va1rn2am3e=“Welcome to LinuxHint”

Note: When you run any of the above bash scripts you will get the same output which is mentioned below:

$ bash test.sh

Similarly, you can also combine Capital letters or the combination of small and capital letters along with the numbers.

Illegal Way to Combine Alphabets With a Number

The illegal way of combining alphabets with a number is when you write the numbers first, followed by the alphabets as explained below:

$123varname=“Welcome to LinuxHint”

When you run the above bash script, then you will get the following error:

$ bash test.sh

This means that a number should not come first while declaring a variable, or else it will consider it as a Linux command.

Note: If you want to separate words and numbers using a space, then it is also considered illegal.

Method 3: Combination of Alphabets and Numbers With Underscore

You can combine an underscore with alphabets and numbers, so the legal and illegal ways of doing that are discussed below.

Legal Ways to Combine Alphabets and Numbers With Underscore

Underscores are usually used to separate one word or a number from others, and one of its examples is mentioned below:

$ var_name=“Welcome to LinuxHint”

You can use the underscore with the combination of alphabets and numbers as shown below:

$ varname_123=“Welcome to LinuxHint”

You can also use multiple underscores as well while declaring a variable as shown below:

$ var_name_123=“Welcome to LinuxHint”

Similarly, you can place the underscore at the start of the variable as well that can be shown below:

$ _var_name_123=“Welcome to LinuxHint”

The four different ways described above are legal. They will give you the same output as shown below:

$ bash test.sh

Illegal Ways to Combine Alphabets and Numbers With Underscore

If a variable starts with a number, then placing an underscore anywhere will have no meaning and considered illegal, as covered in the previous section:

$ 123_var_name=“Welcome to LinuxHint”

You can see that the variable is now in white color compared to the blue in other bash scripts and this is one of the indications that the variable name is illegal. So, when you run the above bash script, it will give you the following error:

$ bash test.sh

Note: Using any special character such as @, $, or # anywhere while declaring a variable is also not a legal way, and one of its examples is shown below.

$ var@name=“Welcome to LinuxHint”

When you run such bash scripts then, you will encounter an error which is shown below:

$ bash test.sh

Let’s summarize the content discussed above.

Summary

We have summarized all the legal and illegal rules which have been discussed in this article below:

Legal Ways to Declare Variables Illegal Ways to Declare Variables
Use of any combination of letters/alphabets. A space anywhere, or reserved words
Alphabets or an underscore should come first A number first, and then the alphabets or an underscore
Special characters anywhere.

That’s all from this article.

Conclusion

In Bash scripting, various methods can be followed to name a variable, i.e., only alphabets, alphabets with numbers, and alphabets with numbers/underscore. Each method has some legal ways to name a variable in Bash. Moreover, the legal and illegal perspectives of each method are also explored. While declaring variables, the usage of special characters is illegal. This guide has presented the possible methods and legal/illegal aspects to name a variable in Bash.

About the author

Talha Saif Malik

Talha is a contributor at Linux Hint with a vision to bring value and do useful things for the world. He loves to read, write and speak about Linux, Data, Computers and Technology.