declare

Declare variables and set or display their values and properties.

grammar

declare [-aAfFgilnrtux] [-p] [name[=value] ...]

The main purpose

Options

-f limits the operation or display to function names and function definitions.
-F Displays only the function name (with line numbers and source files appended when debugging).
-g creates global variables when used within a shell function; otherwise ignored.
-p displays the attributes and values of each name.

*Options to set properties:
-a Create array (if supported).
-A Creates an associative array (if supported).
-i adds integer attributes.
+i deletes integer attributes.
-l adds lowercase attribute, and the value of the variable will be converted to lowercase.
+l removes lowercase attributes.
-n Increase the reference attribute (if this option is present).
+n Removes the reference attribute if this option is present.
-r adds read-only attribute.
-t adds tracking attributes.
+t Remove tracking attributes.
-u adds the uppercase attribute, and the value of the variable will be converted to uppercase.
+u removes uppercase attributes.
-x adds export attributes.
+x removes exported properties.

Parameters

name (optional): variable name or function name.
value (optional): The value of the variable.

return value

declare returns true unless you provide an illegal option or an assignment error. For specific circumstances that lead to exceptions, please see About exceptions in the Discussion chapter.

example

# Declare variables. Of course, you are also welcome to query linux commands on this website (thanks to the initiator of this project @jaywcjlove).
declare reference_website='https://wangchujiang.com/linux-command/'

# Display all variables and values containing integer attributes.
declare -i
# Define variable b and assign it a value of 3, which has an integer attribute.
declare -i b=5
# Display attributes, return declare -i b="5".
declare -p b
# Delete integer attributes.
declare +i b
# Display attributes, return declare -- b="5".
declare -p b
# Force the English case of the value to be converted based on the variable attributes.
declare -u uc_var='abc'
declare -l lc_var='ABC'
# Display 'ABC abc';
echo "${uc_var} ${lc_var}"
# Define global variables within the function
function test(){
   declare -g a=3
   # or
   local -g b=3
   # or
   c=3
   # Let's look at their properties.
   declare -p a b c
}
#Execute function.
test
# Return results.
# declare -- a="3"
# declare -- b="3"
# declare -- c="3"

# Define global variables outside the function
declare a=3
b=3
declare –p a b
#The returned results are as follows.
# declare -- a="3"
# declare -- b="3"

# Define local variables
function test2(){
   local -i a=3
   declare -i b=3
}
test2
# There is no such variable (already destroyed)
echo "${a} ${b}"
# Therefore, the most common thing like 'a=3' in our daily scripts is actually declaring and assigning a global variable.
# In the next **discussion** session, the issue of global and local variables will be extended.
# Note that you cannot use `+a` or `+A` to cancel an array, nor can you use `+r` to cancel a read-only attribute.

# Define a read-only array, and define the assignment while setting the properties.
declare -ar season=('Spring' 'Summer' 'Autumn' 'Winter')
# Or something like this.
season=('Spring' 'Summer' 'Autumn' 'Winter')
declare -ar season
# Display all arrays.
declare -a
# Define associative array.

declare -A fruits=(['apple']='red' ['banana']='yellow')
# Display all associative arrays.
declare -A
# Display the attributes and values of all variables and display the definition of the function. The output is very long.
declare
# Display the attributes and values of all variables.
declare -p
# Display the properties and values of all global variables.
declare -g
# Display all function names and function definitions.
declare -f
# Only display all function names.
declare -F

# Define two functions.
function func_a(){ echo $(date +"%F %T"); }
function func_b(){ cd /; ls -lh --sort=time; }
# Display one or more function names and function definitions.
declare -f func_a func_b
# Display only one or more function names, useful when verifying whether a certain name has been defined as a function.
declare -F func_a func_b
# It is best not to make the function name and variable name the same.

discuss

Notice