Shell Scripting – Rules for Naming Variable Name
Variables are quite important in any script or program, so we need to understand what is the convention to name these variables. There are certain rules and standards to keep in mind while giving names to the variables in Shell scripting. In this article, we will discuss and list down all the rules and conventions to declare a variable name in Shell/Bash scripting.
Rule 1: A variable name can have letters, numbers, and underscores
A variable name can be alphanumeric i.e alphabets and numbers along with an underscore. You can use lowercase or uppercased alphabets in the name. We can use the underscore character at any position in the variable name.
The variables var_name have an underscore and have lowercased characters. The variable X2 has an uppercase character and number. Finally, the last variable has uppercase as well as lowercase characters, numbers, and underscores.
Note: Do not use _ as a variable name as when you try to access the variable _ with $_ it will, in turn, give an error as it is a reserved command for accessing the large argument of the last command.
Rule 2: There should be no whitespace on either side of the assignment operator(=)
The variable name should not be followed by any whitespace on either side of the assignment operator. If you have any spaces before or after the assignment operator the shell interpreter might give error messages about the variable name is a command in itself.
The variable assignment is considered illegal in the following cases:
name = "John" name= "John" name ="John"
The correct way to assign a variable in shell scripting is without any whitespace on either side of the assignment operator(=) which is shown as follows:
Rule 3: The variable name cannot have special characters
Apart from an underscore as discussed in the first rule, a variable name cannot have any special character. As shell scripting uses certain special characters for some operational tasks like $ for accessing the variable literal value, & as the and operator * and # as the array/string extractor and so on. So, it is rather not possible to use any special characters in the variable name in a shell script.
Rule 4: The first character of the variable name cannot be a number
Though we can use numbers in the variable name the only exception will arise when the number is the first character in the variable name. So, any position is valid for the number except the first character of the variable name.
Following are the illegal variable names as they have numbers as the first character.
2X 1st_name 6_gate
Rule 5: Variable names cannot be reserved words
It is not a good practice to use reserved keywords like if, else, while, for, until, case, etc as a variable name. Though it doesn’t give any errors it is still a bad practice and can easily mess up the readability of the shell script and cause certain bugs in some cases.
The reserved words in shell/bash scripting are as follows:
if elif else then while do done for until case esac continue break function
Try to avoid using this as the variable names.
Rule 6: Variable name cannot have whitespace in between
We cannot have a space between the variable name like
This will generate an error as the shell interpreter will treat the word first as a command and will return an error. So, we need to avoid using spaces in between variable names we can use _ instead to create multi-word variable names for better readability.
So these are the rules to be kept in mind while declaring variable names in a shell scripting environment.