declare
Quick Reference
Command Name:
declare
Category:
shell builtin
Platform:
linux
Basic Usage:
Common Use Cases
- 1
Variable typing
Declare variables with specific attributes and constraints in Bash
- 2
Array creation
Define and manage indexed and associative arrays in shell scripts
- 3
Function parameter handling
Create variables with specific behaviors for function inputs
- 4
Script debugging
Enable variable tracing and error detection in complex scripts
Syntax
declare [options] [name[=value]]...
Options
Option | Description |
---|---|
-a | Declare array variable |
-A | Declare associative array (requires Bash 4+) |
-f | Restrict to function names and definitions |
-F | Display function names only (without definitions) |
-g | Create global variables when used in a shell function |
-i | Declare integer variable |
-l | Convert variable value to lowercase |
-n | Declare name reference to another variable |
-p | Display attributes and values of each name |
-r | Make variables read-only (cannot be unset or changed) |
-t | Give each variable the trace attribute |
-u | Convert variable value to uppercase |
-x | Export variables to environment for child processes |
Examples
How to Use These Examples
The examples below show common ways to use the declare
command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.
Basic Examples:
# Declare a simple variable declare var1="Hello World" echo $var1
Advanced Examples:
# Declare and export a variable in one step declare -x exported_var="This is exported to child processes" bash -c 'echo $exported_var'