A variable is a storage space having a particular name that holds a certain value in it. You might have been working with a lot of programming languages and have a good perspective of variables. However, in the bash programming, it is slightly different. In this guide, we will learn about the rules invariable naming and execute some examples to declare a variable in a bash shell and observe its effect whether, it is valid or invalid, e.g., legal or illegal.

Legal Rules of Naming Variables in Bash

  • The variable name must be in the upper case as it is considered good practice in bash scripting.
  • Insert the dollar sign “$” before the variable name.
  • Don’t use spaces after the initialization of the variable name and its value.
  • A variable name can have letter/s.
  • A variable name can have numbers, underscores, and digits.

Illegal Rules of Name Variables in Bash

  • The variable name having lower case letters.
  • No dollar sign “$” inserted while printing it.
  • Adding spaces after the initialization of the variable name and its value.
  • Start the variable name with number, digit, or special symbols.
  • The variable name having space in it.
  • Use of keywords to name the variables, e.g., if, else, for, while, int, float, etc.

Example 01: Lower/Upper Case and Dollar Sign

Open your terminal and create a variable with an upper case. Print this variable using the statement “echo”, with and without dollar signs. Notice that with the “$” sign, it will display the value, otherwise, it will only display the variable name.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/bash-variable-name-rules-legal-illegal-01.png" data-lazy- height="122" src="data:image/svg xml,” width=”974″>

Example 02: Spaces after Variable Name and Equal Sign

Open your terminal and create a bash file named “variable.sh” using the touch command.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/bash-variable-name-rules-legal-illegal-02.png" data-lazy- height="29" src="data:image/svg xml,” width=”974″>

Open this file from the Home Directory and write the code as shown below. You can see that there are spaces after the variable name and equal sign, which is incorrect. On the other hand, a variable is not printed out without a dollar sign in the echo statement.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/bash-variable-name-rules-legal-illegal-03.png" data-lazy- height="52" src="data:image/svg xml,” width=”974″>

In the terminal, execute the bash command to run the file “variable.sh”. You will see that there is an error because of the invalid usage of rules.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/bash-variable-name-rules-legal-illegal-04.png" data-lazy- height="66" src="data:image/svg xml,” width=”974″>

Let’s correct the same code, with the dollar sign in the echo statement and no spaces in the variable name. Save and close it.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/bash-variable-name-rules-legal-illegal-05.png" data-lazy- height="50" src="data:image/svg xml,” width=”974″>

Again, running the file using the bash command, you can see that we have a valid output now.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/bash-variable-name-rules-legal-illegal-06.png" data-lazy- height="50" src="data:image/svg xml,” width=”974″>

You can also attempt it in a bash shell. Let’s take a variable with spaces before and after the equal sign. It will display an error, as shown below:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/bash-variable-name-rules-legal-illegal-07.png" data-lazy- height="147" src="data:image/svg xml,” width=”974″>

When you remove the spaces before and after the equal sign, it will be executed successfully. On the other hand, in bash, the variables are syntax sensitive, so make sure to run the correct variable. As you can see, when we print the lowercase variable, it will display its value, and on the usage of the uppercase variable, it will display nothing.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/bash-variable-name-rules-legal-illegal-08.png" data-lazy- height="145" src="data:image/svg xml,” width=”974″>

Example 03: Spaces in Variable Name

Let’s take the variable “ROLL NO” with spaces in between. It will display an error, as shown below. This means that the variable’s name cannot contain spaces.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/bash-variable-name-rules-legal-illegal-09.png" data-lazy- height="50" src="data:image/svg xml,” width=”974″>

When you remove the space, you can see it works correctly while using the echo statement and displays the value.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/bash-variable-name-rules-legal-illegal-10.png" data-lazy- height="75" src="data:image/svg xml,” width=”974″>

Example 04: Digits/Numbers in Variable Name

Let’s take a variable starting with some digit or number. As observed, it will display an error. This means that the variable name cannot have a number at the start. When you add a number in the middle or at the end of the variable, it will work correctly, as shown below. While using an echo statement, it will display the value of a variable name containing a number.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/bash-variable-name-rules-legal-illegal-11.png" data-lazy- height="191" src="data:image/svg xml,” width=”974″>

Take another example of using digit and number together. Declare a variable in the file “variable.sh” and print it out in the echo statement.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/bash-variable-name-rules-legal-illegal-12.png" data-lazy- height="56" src="data:image/svg xml,” width=”974″>

The bash command implies running the code. We will get an error due to the usage of digits and numbers at the start.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/bash-variable-name-rules-legal-illegal-13.png" data-lazy- height="79" src="data:image/svg xml,” width=”974″>

While correcting the variable, add the digit and number at the end of it and print it in an echo statement.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/bash-variable-name-rules-legal-illegal-14.png" data-lazy- height="56" src="data:image/svg xml,” width=”974″>

After doing so, it will work successfully and print the value of a variable.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/bash-variable-name-rules-legal-illegal-15.png" data-lazy- height="52" src="data:image/svg xml,” width=”974″>

Example 05: Special Characters in Variable Name

None of the special characters can be used in naming variables, e.g., asterisk, question mark, greater than, less than, hash, exclamation marks, etc. Let’s take an asterisk as an example. Even though we put it before, after, or in the middle of the name of a variable, it will cause an error generation. This means that no special character can be used in the variable name before, after, and in between.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/bash-variable-name-rules-legal-illegal-16.png" data-lazy- height="145" src="data:image/svg xml,” width=”974″>

Example 06: Underscore in Variable Name

The underscore can be used in naming variables before, after, and in between. Let’s have an example. While trying it before, after, and between the name of a variable, it will cause an error generation. This means that no special character can be used in the variable name before, after, and in between

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/bash-variable-name-rules-legal-illegal-17.png" data-lazy- height="75" src="data:image/svg xml,” width=”974″>

In the bash file, we have declared a variable with an underscore in between the variable name. The echo statement has been used to print the variable.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/bash-variable-name-rules-legal-illegal-18.png" data-lazy- height="56" src="data:image/svg xml,” width=”974″>

Run the bash file using the bash command. You can see that the value has been printed out in the terminal correctly.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/bash-variable-name-rules-legal-illegal-19.png" data-lazy- height="52" src="data:image/svg xml,” width=”974″>

Example 07: Concatenate Variable with String

Let’s have an example of concatenating the variable with the string in the echo statement using the curly braces. Open the “variable.sh” and write the appended code in it. You can see that we have defined two variables. There is a new variable “WORK”. In the echo statement, we have a string text and a variable “WORK” within the curly brackets, then combined it with the text “ing”. Save and close it.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/bash-variable-name-rules-legal-illegal-20.png" data-lazy- height="97" src="data:image/svg xml,” width=”974″>

When you use the bash command to execute the “variable.sh” file in the command shell, we can see that the variable and string text has been concatenated successfully, and it displays: “The best job is Teaching”.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/bash-variable-name-rules-legal-illegal-21.png" data-lazy- height="77" src="data:image/svg xml,” width=”974″>

Conclusion

We have learned most of the variable naming rules for Bash scripting. Hopefully, you will be able to deal with naming variables within the rules.

About the author

<img alt="Aqsa Yasin" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/02/echo/Author-Image-150×150.jpg6030bf8409058.jpg" height="112" src="data:image/svg xml,” width=”112″>

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.