Shell Scripting Rules for Naming Variables in Shell Scripting (original) (raw)

Last Updated : 9 Mar, 2026

Variables are fundamental in shell scripting and are used to store and manage data within a script. Proper variable naming ensures correct execution and improves readability. In Shell/Bash scripting, variable names must follow specific rules. Ignoring these rules can lead to syntax errors or unexpected behavior.

Rules for Naming Variables in Shell/Bash

Rule 1: A variable name can have letters, numbers, and underscores

A variable name in Shell/Bash can include alphabets (both lowercase and uppercase), numbers, and the underscore (_) character. These are the only characters allowed for defining valid variable names.

**Examples:

var_name="value"
X2=10
user1_name="John"
DATA_FILE_01="file.txt"

**Output:

calling_variable

**Note:

Rule 2: Do Not Use Whitespace Around the Assignment Operator (=)

In Shell/Bash scripting, there must be no spaces before or after the assignment operator (=) when assigning a value to a variable. Adding whitespace causes the shell to misinterpret the statement and produce errors.

**Incorrect Examples:

name = "John"
name= "John"
name ="John"

**Output:

invalid_variable

**Correct Example:

name="John"

**Output:

valid_variable

Rule 3: Variable Names Cannot Contain Special Characters

In Shell/Bash scripting, variable names cannot include special characters except for the underscore (_). Special characters have predefined meanings in the shell and are used for operations such as variable expansion, pattern matching, and command control.

**Invalid Examples:

user-name="Sam"
total$=100
price#=50
value@=10

**Output:

invalid_special_cha_var

**Meaning of Special Characters in Linux:

Rule 4: The First Character Cannot Be a Number

In Shell/Bash scripting, a variable name may contain numbers, but it cannot begin with a number. The first character must always be a letter or an underscore.

**Invalid Examples:

2X=10
1st_name="Sam"
6_gate="open"

**Output:

invalid_strt_num_var

**Valid Examples:

gate6="open"
name1="Sam"
_2ndValue=20

**Output:

valid_num_variable

Rule 5: Variable Names Should Not Be Reserved Words

In Shell/Bash scripting, reserved words (keywords) are part of the shell’s syntax and control structures. Using these keywords as variable names is not recommended because it can reduce readability and may cause logical confusion or unexpected behavior.

**Common Reserved Words:

**Bad Practice Examples:

if="value"
for=10
case="test"

**Output:

badpractice_var

**Note: Although these may not always produce errors, they make the script difficult to read and understand.

Rule 6: Variable Names Cannot Contain Spaces

In Shell/Bash scripting, variable names must not contain spaces. The shell treats spaces as separators between commands and arguments, so including a space inside a variable name will cause an error.

**Incorrect Example:

first name="John"

incorrect_var

**Correct Example:

first_name="Sahil"

valid_unde_variable