As Bash is a dynamically typed shell, it understands many different types of variables. So, you don’t have to bother about what kind of variables you need to assign. Having a compact knowledge of Bash variable types is enough to choose the perfect data structure for your operations accordingly. In this article, I’m going to share the basic context of variable types in Bash. Let’s dive into it.
What Are Bash Variables?
Bash variables are temporary storage where you can store various data types like integers, strings, arrays, etc. They do not need any evident declaration always. In fact, these variables are the essential parts that serve users by being referenced and manipulated in Bash programming.
Types of Bash Variables
There are two types of variables in Bash. They are described below:
- System-defined Variables: System-defined variables are the default and pre-defined variables in Bash. Basically, you can define them in capital letters (UPPER_CASE). These are actually the built-in variables in the Linux Bash shell that are accessible whenever you open a new session.
- User-defined Variables: User-defined variables specify the variables that you create and manipulate as a user. These variables are not predefined by the Bash shell. So, there are no certain rules to write these. You can define them in both UPPER_CASE or LOWER_CASE. These user-defined variables are so versatile to use that you can smoothly manage data as per your preferences during the execution.
In the following section, I will discuss how you can display the variable attributes and check the types of variables in Bash:
1. Displaying Variable Values Using “declare -p” Command
In Bash scripting, declare -p
command is used to display different values and attributes of a variable whether it includes strings, integer values or arrays. Here’s an example:
#!/bin/bash
#Declaring variables
website="LinuxSimply"
declare -i number=25 #Integer attribute
declare -a array=("A" "B" "C") #Indexed-array attribute
#Displaying information about variables
declare -p website
declare -p number
declare -p array
Here, declare -p website
, declare -p number
, and declare -p array
display the values assigned in the variables website
, number
, and array
.
From the image, you can see the different values contained in the variables.
2. Checking Variable Type Using Conditional Statements
As Bash does not have any built-in data types for variables, any value inside a variable is addressed as a string by default. So, to check a variable’s type (whether it’s a string, an integer, or an array) you can employ conditional statements with specific contexts. Let’s see an example of such a scheme:
#!/bin/bash
variable1="2023"
variable2="LinuxSimply"
var_integer() {
if [[ "$1" =~ ^[0-9]+$ ]]; then
echo "Variable '$1' is an Integer."
else
echo "Variable '$1' is a String."
fi
}
var_integer "$variable1"
var_integer "$variable2"
The syntax if [[ "$1" =~ ^[0-9]+$ ]];
checks if the values inserted to the variables and passed to the var_integer
function match with ^[0-9]+$
. Here, ^[0-9]+$
checks if any string contains only digits (0-9). If the condition is satisfied, it returns a true expression and the script prints ‘Variable ‘$1′ is an Integer.’. Otherwise, it moves to the else
block and the displays ‘Variable ‘$1′ is a String.’.
The above image dictates the different types of values that I have assigned to the variables.
Best Practices for Using Various Types of Variables in Bash
Using Bash variables properly can make your script more impactful. Here are some best practices that you need to follow while using different types of variables in Bash:
- Purpose of the Variables: Comment on the purpose of each variable you are going to use. This will be helpful for others and for your future self too to get a clear overview of the variables and code.
- Relevant Variable Names: Try to use relevant and meaningful names for your variables. This will make your code more understandable to the readers.
- Double-quote Variables: Double-quote variable expansions during referencing like “$variable” to avoid word splitting.
- Lowercase Variable Names: Be cautious while naming a variable and try to use lowercase letters for this to avoid overwriting the built-in variables in Bash.
- Variable Declaration with ‘local’: During variable declaration within a function, try to use ‘local’ to limit the variables’ activities within the function.
- Define Variables: To avoid unexpected errors or bugs, always define variables with default values before using them.
- Test Empty Variables: Before further use, apply conditional statements to check if the assigned variables are null. You can use the ‘-n’ or ‘-z’ test in this case.
- Check Exit Status: Check the exit status to handle the Bash error and implement a proper control flow.
- Clear Variables: When the variables are no longer needed, unset them and clean up the memory to avoid clutter.
Conclusion
So far, you have got that Bash variables do not have any fixed type, i.e., they can hold different data types based on their assigned values. And this is one of the best things you’ll find in Bash it supports any kind of assigned variables and helps in manipulating data for interactive shell sessions.
People Also Ask
Can I change the variable type in Bash?
Yes, you can change the variable type by assigning different types of values.
Can Bash variables hold complex data structures?
Of course, Bash variables can hold complex data structures like arrays, associative arrays, etc.
Are all Default Bash variables global?
Yes, every default variable created in Bash is global and accessible anywhere in the script.
Is there any specific data type of a variable in Bash?
No, there is no specific data type of a variable in Bash. You can change their types by appending different values.
Does Bash support floating-point variables?
No, by default Bash doesn’t support floating-point variables. You have to use commands like bc to employ floating-point variables in Bash.
Related Articles
- What Are Built-in Variables in Bash [2 Cases With Examples]
- An Ultimate Guide of Using Bash Environment Variables
- The “.bashrc” Environment Variables [4 Cases]
- String Variables in Bash [Manipulation, Interpolation & Testing]
- What is Variable Array in Bash? [4 Cases]
- An Extensive Exploration of Bash Special Variables [9 Examples]
- What is Boolean Variable in Bash? [3 Cases With Examples]
- What is HereDoc Variable in Bash? [5 Practical Cases]
- What is PS1 Variable in Bash? [3 Customization Examples]
- What is PS3 Variable in Bash? [3 Practical Examples]
<< Go Back to Bash Variables | Bash Scripting Tutorial
FUNDAMENTALS A Complete Guide for Beginners
Your test only checks if a variable is numeric. Real integer types can only be defined with `declare -i`.
Thanks for your response. You are correct. However, variable type declaration is not mandatory in Bash. But specifically, if you want to check the ‘integer type variable’, you can look into the second case.