When writing shell scripts in Bash, choosing appropriate names for variables is essential for code readability and maintainability. By adhering to consistent naming conventions, you can enhance the clarity and understandability of your scripts, making them easier to debug and modify. This article will state some rules for Bash Variable naming conventions in the shell script.
- Learning of Bash Variable Naming Conventions.
- Creating your own variable name using the provided materials.
- An assignment to test your understanding of the Variable Naming Convention.
6 Different Bash Variable Naming Conventions in Shell Script
To build a comprehensive and easily understandable code, naming the variables with a proper guideline is crucial. This also allows one to avoid any unexpected errors occurring during Debugging time.
While Bash doesn’t enforce any strict naming conventions, developers often follow certain conventions to enhance code clarity and consistency. Down below, I have discussed six different rules that should be followed while choosing your variable name.
Rule 01: Naming a Variable Using Alphabets and Numbers
While alphabets are commonly used in variable names, incorporating numbers can add a level of specificity and clarity to your code. However, bash script is a case-sensitive language which means you can store different data in the same variable having different case combinations.
❶ At first, launch an Ubuntu Terminal.
❷ Write the following command to open a file in Nano:
❸ Copy the script mentioned below:
#!/bin/bash #Variable naming using only alphabets var=35 #Using Uppercase VAR="VAR variable Contains Upper Case Letter" #Variable naming using alphabets and numbers var123=34.4 echo $var echo $VAR echo $var123
❹ Press CTRL+O and ENTER to save the file; CTRL+X exit.
❺ Use the following command to make the file executable:
chmod u+x var_alphabet.sh
❻ Run the script by the following command:
From the above image, you can see that all three variables have returned their respective values: 35, VAR variable Contains Upper Case Letter, and 34.4.
Rule 02: Avoid Using Number at the Beginning of the Variable Name
When it comes to naming variables in Bash shell scripts, one important convention to follow is to avoid using a numeric digit as the first character of a variable name. Here is a sample example demonstrated below where I created several variables placing the number digit at the beginning and end of the variable name. The variable with the number digit at the beginning does not return its corresponding value in the execution time.
Script (var_number.sh) >
As 123var is not the correct way of naming, there is an error that occurred for the variable. However, the echo command successfully displays the “Correct Naming” string contained in the var123 variable.
#!/bin/bash #variable with number first (invalid way) 123var="Wrong Way to name" #correct way var123="Correct Naming" echo $123var echo $var123
Rule 03: Using Underscore to Avoid Whitespace
If a variable name consists of multiple words, it is advisable to separate them using underscores (_). For instance, user_name is advisable instead of using username
Script (var_underscore.sh) >
#!/bin/bash #bad way to name your variable var name="jhon" #correct way to name your variable var_name="Tom" echo $var name echo $var_name
As you can see from the above image, only the var_name variable returns its string value “Tom” during execution time.
Rule 04: No Whitespace on Either Side of the Assignment Operator(=)
When using this operator, it is recommended to have no whitespace (spaces or tabs) immediately before or after the operator. The interpreter takes it as a command instead of assigning value to the variable otherwise. Here is a relevant example stated below.
Script (var_whitespace.sh) >
The output image shows that only the var3 variable returns its string value “Great” in the execution time.
#!/bin/bash #Wrong way to declare assign data var1 =23 var2= 25 #Correct way var3="Great" #print the variables echo $var1 echo $var2 echo $var3
Rule 05: Avoiding Special Characters (Except Underscore)
In general, it is best to stick to alphanumeric characters (letters and numbers) and underscores when naming variables. Avoid using special characters, whitespace, or punctuation marks, as they can cause syntax errors or introduce unexpected behaviour. For instance, using any special character such as @, $, or # anywhere while declaring a variable is not legal.
Script (var_special_character.sh) >
The image shows @, #, $ as the wrong output and 45 as the correct output.
#!/bin/bash #wrong way to name a variable var@=5 var#=8 var$=23 #To display the variable data echo $var@ echo $var# echo $var$ #Correct way to name var=45 echo $var
Rule 06: Avoid Reserved Keywords or Built-in Command Names as Variable Names
Bash has a set of reserved keywords that have special meanings and functionalities within the shell. Although you will get the variable data, to prevent conflicts and unexpected behaviour, it is crucial to avoid using these reserved keywords as variable names. Examples of reserved keywords include if, else, case, do, while, etc.
Script (var_Reserved_keyword.sh) >
The above image depicts that all four variables while, if, elif, and var return their respective stored value 56, 34, 67, and 50.
#!/bin/bash #Wrong way to name your variable while=56 if=34 elif=67 #To display the variable value echo $while echo $if echo $elif #correct way to name variable=50 echo $variable
Comparison of Correct and Incorrect Variable Naming Conventions
Lastly, I draw a summary of the discussion in a table to describe the permissible and non-permissible ways to name the variable. Check this out to get the overall view.
|Correct Naming Convention||Incorrect Naming Convention|
|Use lowercase letters and underscores.||Start with a number or special character (except underscore).|
|Start with a letter or underscore.||Contain spaces or special characters like !, @, #, %, etc.|
|Consist of lowercase letters, underscores, and digits.||Use uppercase letters.|
|Can include numbers and underscores within the name (except as the first character).||Incorporating reserved keywords or built-in command names as variable names.|
|Use descriptive and meaningful names.||Use inconsistent or ambiguous names that hinder code readability.|
Assignment on Bash Variable Naming Convention
Here I have also made a list of assignments to practice by yourself. Don’t forget to share your answer in the comment section.
In conclusion, adhering to proper variable naming conventions is essential when working with Bash variables in shell scripts. In this article, I have tried to give you some must known rules to follow while choosing your variable name. Things are easy if you remember however if you have any query related to this article. Feel free to comment below. Thank you.
People Also Ask
- How to Declare Variable in Bash Scripts? [5 Practical Cases]
- How to Assign Variables in Bash Script? [8 Practical Cases]
- How to Assign Float to Variable in Bash Script? [2 Methods]
- How to Check Variable Value Using Bash Scripts? [5 Cases]
- How to Use Default Value in Bash Scripts? [2 Methods]
- How to Use Set – $Variable in Bash Scripts? [2 Examples]
- How to Read Environment Variables in Bash Script? [2 Methods]
- How to Export Environment Variables with Bash? [4 Examples]