Skip to main content
The declare (also known as typeset) built-in in Bash lets you assign attributes and data types to your variables. While Bash is dynamically typed, using declare can help enforce types (like integers), create read-only variables, and define arrays. In this article, we’ll cover:
  • Data types in Bash
  • Dynamically typed variables
  • Enforcing integer types with declare -i
  • Other useful declare flags
  • Working with arrays using declare -a
The image features a list of programming-related topics with checkmarks, including explaining data types, dynamically typed syntax, the declare command in Bash, and using declare for arrays. It also includes simple line drawings of a person and a lightbulb.

Data Types in Bash

Bash offers two fundamental data types:
  1. String
  2. Integer
The image shows a split screen with "String" labeled as 1 on the left and "Integer" labeled as 2 on the right, under the title "arrays-declare."
You can assign values using the typical syntax:

Dynamically Typed Variables

Bash determines the type at runtime, based on context:
You can even switch types on the fly:
Bash’s dynamic typing means it tries to interpret values at runtime—no compile-time errors for type mismatches.

Enforcing Integer Types with declare -i

To enforce integer semantics (mimicking static typing), use:
Non-numeric assignments reset the variable to 0.

Other declare Flags

Here’s a quick reference for some common attributes:

Read-Only Variables (-r)

Attempting to modify a readonly variable will terminate your script with an error.

Case Conversion (-u, -l)

Arrays with declare -a

Bash supports both indexed and associative arrays:

Watch Video