This is an old revision of the document!


The declare builtin command

declare [-aAfFgilrtux] [-p] [NAME[=VALUE] ...]

# obsolete typeset synonym
typeset [-aAfFgilrtux] [-p] [NAME[=VALUE] ...]

declare is used to display or set variables along with variable attributes. When used to display variables/functions and their value, the output is re-usable as input for the shell.

If no NAME is given, it displays the values of all variables or functions when restricted by the -f option.

If NAME is followed by =VALUE, declare also sets the value for a variable.

When used in a function, declare makes NAMEs local variables, unless used with the -g option.

Don't use it's synonym typeset when coding for Bash, since it's tagged as obsolete.

Options

Below, [-+]X indicates an attribute, use -X to set the attribute, +X to remove it.

Option Description
[-+]a make NAMEs indexed arrays (removing with +a is valid syntax, but leads to an error message)
[-+]A make NAMEs associative arrays
-f restrict action or display to function names and definitions (removing with +f is valid syntax, but leads to an error message)
-F restrict display to function names only (plus line number and source file when debugging)
-g create global variables when used in a shell function; otherwise ignored (by default, declare declares local scope variables when used in shell functions)
[-+]i make NAMEs have the "integer" attribute
[-+]l convert NAMEs to lower case on assignment (makes sure the variable contains only lower case letters)
-p display the attributes and value of each NAME
[-+]r make NAMEs readonly (removing with +r is valid syntax, but not possible)
[-+]t make NAMEs have the "trace" attribute (effective only for functions)
[-+]u convert NAMEs to upper case on assignment (makes sure the variable contains only upper case letters)
[-+]x make NAMEs exported

Return status

Status Reason
0 no error
!= 0 invalid option
!= 0 invalid variable name given
!= 0 attempt to define a function using -f
!= 0 assignment to a readonly variable
!= 0 removing the readonly-attribute from a readonly variable
!= 0 assignment to an array variable without the compound assignment syntax (array=(…))
!= 0 attempt to use +a to "destroy" an array
!= 0 attemt to display a non-existent function with -f

Display defined functions

declare -f can be used to display all defined functions…

$ declare -f
foo () 
{ 
    echo "FOO is BAR"
}
world () 
{ 
    echo "Hello World!"
}
…or just a specific defined function.
$ declare -f foo
foo () 
{ 
    echo "FOO is BAR"
}

  • declare is not specified by POSIX®
  • there is a Bash synonym typeset, which is marked as obsolete: Use declare for Bash, other shells may use typeset
  • the Korn shell typeset special command is not fully compatible, but some options are
This website uses cookies for visitor traffic analysis. By using the website, you agree with storing the cookies on your computer.More information
You could leave a comment if you were logged in.
  • commands/builtin/declare.1294120591.txt
  • Last modified: 2011/01/04 05:56
  • by thebonsai