...

/

Declaring Variables with Attributes

Declaring Variables with Attributes

Let's find out how declare command sets attributes for new variables.

We'll cover the following...

Here are several examples of how to declare variables with attributes. First, let’s compare integer and string variables. We execute the following two commands in the terminal:

declare -i sum=11+2
text=11+2

Run the commands discussed in this lesson in the terminal below.

Terminal 1
Terminal
Loading...

We declared two variables named sum and text. The sum variable has the integer attribute. Therefore, its value equals 13—that is, the sum of 11 and 2. The text variable is equal to the “11+2” string.

Bash stores both variables as strings in the memory. The -i option does not specify the variable’s type. Instead, it limits the allowed values of the variable.

Let’s try to assign a string to the sum variable. We can do it in one of the following ways:

 ...