TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
declare command is a Bash shell builtin used to instantiate variables, assign specific type attributes, and modify variable scope. It enforces data types and behaviors on variables in a predominantly typeless shell environment. When invoked within a function, declare implicitly restricts the variable’s scope to that function, mirroring the behavior of the local command.
Syntax
Attribute Flags
Attributes dictate how the shell evaluates and mutates the data assigned to a variable. Using a hyphen (-) enables the attribute, while using a plus sign (+) disables it. Note that read-only (-r) and array (-a, -A) attributes cannot be removed once applied.
-a: Defines the variable as an indexed array (zero-based integer keys).-A: Defines the variable as an associative array (string-based keys). Requires Bash 4.0+.-i: Assigns the integer attribute. The shell will treat subsequent assignments to this variable as arithmetic expressions and evaluate them automatically.-r: Marks the variable as read-only. Any subsequent attempt to reassign or unset the variable will result in a shell error.-x: Marks the variable for export, adding it to the environment of subsequently executed child processes. When used within a function,declare -xcreates a local variable that is exported to child processes of that function. This contrasts with theexportcommand, which creates or modifies a global variable unless the variable was already explicitly declared as local.-n: Assigns the nameref attribute. The variable acts as a direct reference (pointer) to another variable specified by its assigned value. Requires Bash 4.3+.-l: Assigns the lowercase attribute. The shell automatically converts any assigned string value to lowercase.-u: Assigns the uppercase attribute. The shell automatically converts any assigned string value to uppercase.-t: Assigns the trace attribute. When applied to a function, it inherits theDEBUGandRETURNtraps from the calling shell.
Operational Flags
These flags change the behavior of thedeclare command itself, rather than modifying variable attributes.
-p: Prints the attributes and values of the specified variables in a format that can be reused as input. If no variable names are provided, it outputs all variables currently defined in the shell environment.-f: Restricts the command’s operation to shell functions. When used without arguments, it prints the names and definitions of all functions.-F: Modifies-fto display only the function names and their attributes, omitting the function bodies.
Scope and Context
By default, variables declared in the main body of a script are globally scoped. Whendeclare is executed within a function body, the variable is bound to the local scope of that function and its children, shadowing any global variable with the same name.
To override this default local scoping within a function, the -g flag is utilized:
-g: Forces the variable to be created or modified at the global scope, regardless of the current execution context.
Master Bash with Deep Grasping Methodology!Learn More





