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
- Method 1: Declaring a Variable Using Only Alphabets
- Legal Ways of Declaring Variables Using Only Alphabets
- Illegal Ways of Declaring Variables Using Only Alphabets
- Method 2: Combination of Alphabets With a Number
- Legal Ways to Combine Alphabets With a Number
- Illegal Way to Combine Alphabets With a Number
- Method 3: Combination of Alphabets and Numbers With Underscore
- Legal Ways to Combine Alphabets and Numbers With Underscore
- Illegal Ways to Combine Alphabets and Numbers With Underscore
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:
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:
Next, we will be declaring the variable that contains both small and capital letters as shown below:
Note: We have saved each file with the name of “test.sh”, and you can execute the script by writing the following command:
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.
The output of the above bash script when you execute it is mentioned below:
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:
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:
Note: When you run any of the above bash scripts you will get the same output which is mentioned below:
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:
When you run the above bash script, then you will get the following error:
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:
You can use the underscore with the combination of alphabets and numbers as shown below:
You can also use multiple underscores as well while declaring a variable as shown below:
Similarly, you can place the underscore at the start of the variable as well that can be shown below:
The four different ways described above are legal. They will give you the same output as shown below:
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:
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:
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.
When you run such bash scripts then, you will encounter an error which is shown below:
Let’s summarize the content discussed above.
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.
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.