Declare variables and give them attributes.

      declare [-aAfFgiIlnrtux] [-p] [name[=value]]

   -a   Each name is an array variable.

   -A   Each name is an associative array variable

   -f   Use function names only.

   -F   Inhibit the display of function definitions; 
        only the function name and attributes are printed. (implies -f)

   -g   Force variables to be created or modified at the global scope, even when declare is executed
        in a shell function. It is ignored in all other cases. 

   -i   The variable is to be treated as an integer; arithmetic evaluation (see Shell Arithmetic)
        is performed when the variable is assigned a value.

   -I   Cause local variables to inherit the attributes (except the nameref attribute) and value
        of any existing variable with the same name at a surrounding scope.
        If there is no existing variable, the local variable is initially unset. 

   -l   When the variable is assigned a value, all upper-case characters are
        converted to lower-case. The upper-case attribute is disabled.

   -n   Give each name the nameref attribute, making it a name reference to another variable.
        That other variable is defined by the value of name.
        All references, assignments, and attribute modifications to name, except for those using
        or changing the -n attribute itself, are performed on the variable referenced by name’s value.
        The nameref attribute cannot be applied to array variables.

   -p   Display the attributes and values of each name. 
        When -p is used with name arguments, additional options, other than -f and -F, are ignored.
        When -p is supplied without name arguments, declare will display the attributes and values
        of all variables having the attributes specified by the additional options.
        If no other options are supplied with -p, declare will display the attributes and values
        of all shell variables. The -f option will restrict the display to shell functions. 

   -r   Make names readonly. 
        These names cannot then be assigned values by subsequent assignment statements 
        or unset.

   -t   Give each name the trace attribute.
        Traced functions inherit the DEBUG and RETURN traps from the calling shell.
        The trace attribute has no special meaning for variables.

   -u   When the variable is assigned a value, all lower-case characters are converted to upper-case.
        The lower-case attribute is disabled.

   -x   Mark each name for export to subsequent commands via the environment.

If no names are given, then 'declare' will display the values of variables instead.

Using + instead of - turns off the attribute instead, with the exceptions that ‘+a’ and ‘+A’ may not be used to destroy array variables and ‘+r’ will not remove the readonly attribute.

When used in a function, declare makes each name local, as with the local command, unless the -g option is used.

If a variable name is followed by =value, the value of the variable is set to value.

The typeset command is supplied for compatibility with the Korn shell; however, it has been deprecated in favor of the declare builtin command.

When using -a or -A and the compound assignment syntax to create array variables, additional attributes do not take effect until subsequent assignments.

The return status is zero unless an invalid option is encountered, an attempt is made to define a function using '-f foo=bar', an attempt is made to assign a value to a readonly variable, an attempt is made to assign a value to an array variable without using the compound assignment syntax, one of the names is not a valid shell variable name, an attempt is made to turn off readonly status for a readonly variable, an attempt is made to turn off array status for an array variable, or an attempt is made to display a non-existent function with '-f'.

"My mother never saw the irony in calling me a son-of-a-bitch" ~ Jack Nicholson

Related linux commands

alias - Create an alias.
env - Display, set, or remove environment variables.
echo - Display message on screen.
export - Set an environment variable.
hostname - Print or set system name.
local - Create variables.
printenv - Print environment variables.
readonly - Mark variables/functions as readonly.
shift - Shift positional parameters.
Equivalent Windows command: SET - Display, set, or remove Windows environment variables.

Copyright © 1999-2024
Some rights reserved