
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Bash Declare Statement: Syntax and Examples
As a Linux user, you've probably heard of Bash shell. Bash is a powerful tool that allows you to perform a variety of tasks on your system. One of most useful features of Bash is declare statement. In this article, we'll explore what declare statement is, how it works, and provide examples of how you can use it in your scripts.
What is Declare Statement?
The declare statement is a built-in Bash command that allows you to set attributes for variables. When you declare a variable, you are telling Bash to treat that variable in a certain way. For example, you can declare a variable to be read-only or to be an array.
Syntax of Declare Statement
The syntax of declare statement is simple. Here's basic format ?
declare [options] variable=value
The declare keyword tells Bash that you want to declare a variable. options allow you to set attributes for variable. Finally, you assign a value to variable.
Options for Declare Statement
There are several options you can use with declare statement. Here are some of most common ones ?
-a ? This option declares variable to be an array.
-i ? This option declares variable to be an integer.
-r ? This option declares variable to be read-only.
-x ? This option exports variable to environment.
Examples of Declare Statement
Let's take a look at some examples of how you can use declare statement in your scripts.
Declare a Variable as an Array
declare -a my_array=("apple" "banana" "cherry")
In this example, we are declaring a variable called my_array as an array. We are also assigning three values to array: "apple", "banana", and "cherry".
Declare a Variable as an Integer
declare -i my_num=5
In this example, we are declaring a variable called my_num as an integer. We are also assigning a value of 5 to variable.
Declare a read-only Variable
declare -r my_name="John"
In this example, we are declaring a variable called my_name and making it read-only. This means that value of my_name cannot be changed once it has been set.
Export a Variable to Environment
declare -x my_path="/usr/local/bin"
In this example, we are declaring a variable called my_path and exporting it to environment. This means that other programs that are run from same shell session will be able to access value of my_path.
Additional Examples of Declare Statement
Let's dive into some more examples of declare statement to gain a deeper understanding of its functionality ?
Declare a Variable with a Default Value
declare my_var="Hello World!"
In this example, we are declaring a variable called my_var and assigning it a default value of "Hello World!".
Declare an Integer Variable and Perform Arithmetic Operations
declare -i num1=5 declare -i num2=10 declare -i result=num1+num2 echo "The result is: $result"
In this example, we are declaring two integer variables called num1 and num2, and then declaring a third integer variable called result, which is sum of first two variables. We then print out result using echo command.
Declare an Associative Array
declare -A my_array=([apple]="red" [banana]="yellow" [cherry]="red") echo "${my_array[banana]}"
In this example, we are declaring an associative array called my_array with three key-value pairs. We then print out value of banana key using ${my_array[banana]} syntax.
Declare a Function
declare -f my_func my_func() { echo "Hello World!" } my_func
In this example, we are declaring a function called my_func using -f option. We then define function, which simply prints out "Hello World!". Finally, we call function using my_func command.
Declare a Variable Using a Reference
declare -n my_ref=my_var my_var="Hello Universe!" echo "$my_ref"
In this example, we are declaring a variable called my_var with a value of "Hello Universe!". We then declare a reference variable called my_ref, which is assigned value of my_var. We then print out value of my_ref, which is same as my_var.
Additionally, you can use the "declare" statement to set attributes for variables that affect their behavior in different ways. For example, you can use the "-t" option to set a trace variable, which allows you to debug Bash scripts by tracing the execution of commands.
Example
declare -t var1="This is a trace variable" echo $var1
Output
+ echo 'This is a trace variable' This is a trace variable
Here, we set the "var1" variable as a trace variable using the "-t" option. When we ran the script, Bash displayed a "+" sign before executing the "echo" command, indicating that the trace variable is enabled.
Another useful attribute you can set with the "declare" statement is the "-u" option, which converts the value of a variable to uppercase.
Example
declare -u var2="this is a lowercase string" echo $var2
Output
THIS IS A LOWERCASE STRING
Here, we declared a variable "var2" with the "-u" option, which converted the lowercase string to uppercase.
One important thing to note is that the "declare" statement can also be used to declare and assign multiple variables in a single command.
Example
declare var3=10 var4="This is another variable" var5=("apple" "banana" "orange") echo $var3 echo $var4 echo ${var5[1]}
Output
10 This is another variable banana
Here, we used a single "declare" statement to declare and assign three variables: "var3", "var4", and "var5". Then, we accessed the values of these variables using their names and indices.
Conclusion
The declare statement is a powerful tool that allows you to set attributes for variables in Bash. By using options available with declare statement, you can make your scripts more efficient and easier to read. Whether you are declaring an array, an integer, a read-only variable, or exporting a variable to environment, declare statement is an essential command for any Bash programmer.