TrumanWong

declare

Declare variables and set or display their values and properties.

grammar

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

The main purpose

  • Display all variables and values containing the specified attribute
  • Display one or more variables and values containing the specified attribute
  • Display the properties and values of one or more variables
  • Show the properties and values of all variables and show the definition of functions
  • Show properties and values of all variables
  • Show properties and values of all global variables
  • Display all function names and function definitions
  • Only show all function names
  • Display one or more function names and function definitions
  • Only display one or more function names
  • Declare global variables (optional: assignment)
  • Declare variables (optional: assignment, attributes)
  • Add and delete attributes of variables (optional: assignment)

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

  1. Global and local variables

    As pointed out in the example above, we need to understand these concepts when writing programs every day. Here Let’s give a brief introduction. Of course, you can also easily search for relevant content.

    • Global variables: always exist during the entire script execution as long as they are not deleted.
    • Local variables: defined within a function and deleted after the function is executed.

    It is recommended to use the local command inside the function and the declare command outside the function.

    *Do not define too many global variables in the script, as this may cause unexpected consequences when called by other functions, and it is inconvenient to check out. *

    Not to mention the lack of necessary comments - ZhuangZhu-74

    Relevant information:

  2. About the declare typeset export local readonly commands

    Why do we need to define these other commands when declare can do it?

    Because the meaning of the statement will be clearer, for example:

    • export var and declare -x var when setting variables for exported properties.
    • Use local when declaring variables within a function.
    • To declare read-only variables, use readonly.

    typeset is the same as declare command.

  3. About abnormal situations

    There are many reasons why declare fails. For these situations, please refer to [bash online document declare section(latest version)](https://www.gnu.org/software/bash/manual/bash.html#index -declare), or execute info bash Look at the last long string of sentences starting with an attempt is in the declare section.

Notice

  1. This command is a built-in bash command. For related help information, please see the help command.
  2. Please see the 'export' command for related introduction to export attributes.
  3. For information about read-only attributes, please see the 'readonly' command.
  4. Please see the example section of the 'unset' command for related introduction to reference attributes.