1. Overview

As we journey into the captivating world of Bash scripting in Linux, we’ll inevitably identify two seemingly identical commands: declare and typeset.

Accordingly, both commands offer the power to manage variables and their attributes, shaping the behavior of our scripts. However, there are subtle distinctions that exist just beneath the surface, and understanding these differences is important for generating robust and efficient scripts.

In this tutorial, we’ll discuss the declare and typeset commands in detail and delve into their functionalities. Moreover, we’ll dissect the rumors surrounding typeset‘s obsolescence and be equipped with the knowledge to confidently use these commands in our upcoming scripts.

2. Embracing the Power of Variables

First, let’s understand variables and how to utilize them to perfect our Bash scripting skills. Also, to be able to fully understand what variables are, let’s imagine them as versatile containers within our scripts.

In that matter, we’ll be able to understand their ability to define our code, as they’re capable of storing information that can be accessed and manipulated throughout our scripts.

Furthermore, we can assign values to these variables using the familiar = operator, but sometimes, we might yearn for more control over their behavior. In essence, this is where attributes come into play.

Next, let’s understand what are attributes. Attributes act as modifiers that define characteristics like the data type:

  • integer
  • string
  • boolean

In addition, attributes can define the accessibility assigned to a certain variable, such as:

  • read-only
  • exported

By effectively leveraging attributes, we can enhance the organization, clarity, and efficiency of our scripts.

3. Unveiling the declare Command

In this section, we’ll delve deep into explaining how to use the declare command to define our variables and attributes. Moreover, we’ll also highlight the correct syntax to use and identify its most commonly used options. Finally, we’ll provide some real-life examples to understand how to use and tailor it to our needs in our Linux environment.

3.1. Exploring the Syntax and Available Options

Introduced in Bash version 2, declare emerges as a powerful and versatile command for managing variables and their attributes. Here’s a breakdown of its syntax to illuminate its inner workings:

$ declare [options] [name[=value]]...

The above snippet illustrates the correct syntax to utilize the declare command. As we can see, after we type the declare command, we can utilize a wide variety of options to manipulate the command in the best way that fits our needs. We’ll discuss more on that later in this section.

Afterward, we type the name of the variable that we want to identify and assign to it a value depending on its type by utilizing the = operator. Notably, we have the option to assign a value to the variable during declaration. In other words, it’s not mandatory to determine a fixed value while creating the variable. However, in most cases, this is more convenient.

Next, let’s check out some of the common options that the declare command offers:

  • -i: specifies the variable as an integer
  • -r: makes the variable read-only, preventing modifications
  • -a: declares an array, allowing the variable to store multiple values
  • -x: exports the variable, making it accessible outside the current scope

In essence, these flags, also known as options, act as modifiers, tweaking the behavior of variables.

3.2. Practical Implementations

Since we now understand the theory as well as the syntax, let’s dive into some practical implementations.

First, let’s check out an example illustrating how to identify an integer variable with the declare command and assign a value to this variable:

# declare -i age=25
# echo $age 
25

In this snippet, we leveraged our previous knowledge from earlier sections to accomplish our task of creating a new variable with the name age and assigning it an integer value equal to 25.

Particularly, let’s have a closer look and understand each component. At first, we use the -i option with the declare command to tell the system that the variable we’ll create will hold an integer value. Afterward, we set the name of the variable as “age” and the integer value as “25“. Finally, by utilizing the echo command followed by the variable’s name, we’re able to print out the output into the Bash shell.

As we checked how to use and verify the declare command with the -i option, we’ll check out other options.

That being said, let’s discuss how to enforce a read-only status with the declare command:

# declare -r message="Hello, world!"
# message="Goodbye!"
message: read-only variable

In this example, we declare a variable named “message” with the -r option, rendering it as read-only, and assign the initial value “*Hello, world!*“. Attempting to modify the value of this read-only variable using the line message=”Goodbye!” triggers an error message, as the variable’s contents are immutable.

3.3. Exploring Advanced Options

While the fundamental functionalities of declare have been established, its capabilities extend far beyond the examples presented thus far. Delving deeper, we’ll explore some advanced options that empower us to exert even greater control over our variables.

Let’s utilize the -a option to create arrays, allowing a single variable to store multiple values:

# declare -a names=("Alice" "Bob" "Charlie")
# echo ${names[1]}
Bob

Here, after we indicate the array’s name and values, we use the echo command to print out the value of index 1, which is Bob.

Next, let’s employ the -x option to make variables accessible outside the current script’s scope:

# declare -x username="Joe"
# echo $username
Joe

We can print this even from another script if we wish to.

Finally, the declare command offers functionalities for type checking, allowing us to restrict the data types a variable can hold. Moreover, this enhances the reliability and maintainability of our scripts by preventing unexpected behavior due to incorrect data types:

# declare -i age=25
# age="hello"
age: invalid number

This example ensures the variable can only store integer values. By mastering these advanced options, we can significantly enhance the organization, maintainability, and robustness of our Bash scripts. In addition, this works with -i, -f, -s, and other options.

4. Demystifying the typeset Command

Often regarded as the elder sibling of declare, typeset offers functionalities akin to its counterpart.

In addition, typeset* syntax mirrors that of *declare:

$ typeset [options] [name[=value]]...

We’ll discuss more on that later to understand why some people refer to it as a legacy command.

While typeset was historically present in Bash, it has been officially deprecated since version 4.0. This implies that although it might still function in older versions, its future support is uncertain. To ensure compatibility and future-proof our scripts, it’s strongly recommended to prioritize using declare to avoid confusion.

Let’s try to define a variable by using the typeset command in version 4.2:

# typeset -i age=25
Obsolete.  See `help declare'.

Although in older versions like 2.x, the above command can work, and if we run the echo command, it displays the value of 25, we can’t leverage it anymore in newer versions.

In other instances, we can run into a different message providing us with the same meaning:

# typeset -i age=25
This command is deprecated.

As we can see, it’s clear that the typeset command is no longer accepted in Bash.

Moving forward, we should prioritize using declare to ensure our scripts remain compatible with the ever-evolving landscape of Bash. By embracing this forward-thinking approach, we can confidently craft scripts that thrive in the present and adapt seamlessly to the future.

5. Future Support for Both Commands

While both commands share significant similarities, there are some key distinctions to keep in mind. For example, typeset has been deprecated since Bash version 4.0 and its future support is uncertain. Moreover, both commands offer nearly identical functionalities and syntax.

Using declare guarantees compatibility with current and future Bash versions, safeguarding our scripts from potential compatibility issues down the line. Simply put, declare reigns supreme as the recommended and future-proof choice for managing variables in our Bash scripting endeavors.

Finally, whenever in doubt, we can always check the manual pages for typeset using the man command on our specific Linux distribution to see if there are any updates or deprecation notices:

$ man typeset

Otherwise, we can also check the official Bash documentation or release notes for the version we’re using.

6. Conclusion

In this article, we’ve gained a comprehensive understanding of declare and typeset, the nuances that differentiate them, and the considerations for choosing the right tool for the job.

Now that we know more, we can create strong and efficient scripts that use variables well. Finally, this will enable us to make the most of our Bash scripting skills.