declare - Linux


Overview

The declare command in Linux is used to define or display shell variables and their attributes. Primarily used in shell scripting, it sets properties for variables beyond just assigning values, such as creating arrays, making variables read-only, and providing integer arithmetic support. It’s integral for managing variable behaviors in complex scripts and ensuring script variables do not accidentally modify the shell environment.

Syntax

The basic syntax of the declare command is as follows:

declare [options] [name[=value] ...]
  • name: The name of the variable to declare.
  • value: The value to assign to the variable (optional).

Options/Flags

  • -a: Declare the variable as an array.
  • -f: Display function names and definitions. If used with -p, display the attributes and values of each name.
  • -i: Declare the variable as an integer.
  • -r: Make the variable read-only. Once set, the value of this variable cannot be changed.
  • -x: Mark the variable for export to subsequent commands via the environment.
  • -p: Display the attributes and value of each variable. If no names are given, display the attributes and values of all variables.

Examples

  1. Basic Variable Declaration:

    declare x=10
    echo $x
    

    This declares a variable x and initializes it with the value 10.

  2. Read-Only Variable:

    declare -r y=20
    echo $y
    

    Attempts to modify y will result in an error due to the read-only attribute.

  3. Integer Variable for Arithmetic Operations:

    declare -i z
    z=10+5
    echo $z
    

    This will output 15, as z is treated as an integer rather than a string.

  4. Array Declaration:

    declare -a cities=('New York' 'London' 'Tokyo')
    echo ${cities[1]}
    

    Accesses and prints London, which is the second element of the array.

Common Issues

  • Scope Issue: Variables declared inside a function have local scope by default unless declared with -g (global).
  • Modification of Read-Only Variables: Attempting to change a read-only variable will result in a permission error. Ensure the readonly attribute is genuinely required.
  • Incorrect Array Usage: When using arrays, forgetting the parentheses () can lead to unexpected behavior or assignment errors.

Integration

declare can be combined with other shell commands for robust script functionalities. Here’s a complex script example that combines variables with conditional statements:

declare -i count=10
if [ $count -gt 5 ]; then 
  echo "Count is greater than 5"
else
  echo "Count is 5 or less"
fi

This script uses an integer-declared variable for arithmetic comparison within an if-else statement.

  • set – Used to set or unset shell options and positional parameters.
  • export – Marks variables or functions for automatic export to the environment of subsequently executed commands.
  • readonly – Marks variables/functions as read-only.

For more comprehensive understanding and other related commands, refer to the Linux command line or shell scripting books, and online resources such as the GNU Bash manual.