Skip to main content
I believe it is easier to read, maybe a small edit could further improve this format.
Source Link

Normally, a variable is a place to store a value. You assign a value to the variable (var="some value"), and after that you can recall the value with a variable expansion (writing "$var" is equivalent to writing "some value").

It's possible to make variables that do something special when you assign a value to them, or in other circumstances where the shell accesses variables. An attribute on a variable is an annotation that the shell stores next to the variable's name and value, which tells the shell to apply this special behavior.

One example

For example, declare -i x tells the shell that x must contain integer values only. Normally, when you assign a value to a variable, the shell takes the string that results from expanding the right-hand side of the equal sign and stores it as the variable's value. But if the variable has the integer attribute, the shell parses that string as an arithmetic expression and stores the result of evaluating that expression. For example:

$ x=2+2; echo $x
2+2
$ declare -i x; x=2+2; echo $x
4
$ declare -i x; x=2+hello; echo $x
2
$ declare -i x; x=2+
bash: 2+: syntax error: operand expected (error token is "+")

(The third line with x=2+hello sets x to 2 because hello is a variable name which is not defined, and unset variables are silently interpreted as 0 by default.)

Likewise, declare -l var declares that var must contain lowercase letters only. When the shell stores the value of the variable, it converts any uppercase letter to lowercase. declare -u var does the conversion in the other direction. declare -r var makes var read-only, which is also a special behavior of assignment: it causes every subsequent assignment to var to fail.

More examples

declare -x var causes var to be exported to the environment. For this attribute, the special behavior happens when bash runs an external command: external commands see an environment that contains the variables that the shell is exporting at the time the shell runs the external command.

  • declare -l var declares that var must contain lowercase letters only. When the shell stores the value of the variable, it converts any uppercase letter to lowercase.
  • declare -u var does the conversion in the other direction.
  • declare -r var makes var read-only, which is also a special behavior of assignment: it causes every subsequent assignment to var to fail.
  • declare -x var causes var to be exported to the environment. For this attribute, the special behavior happens when bash runs an external command: external commands see an environment that contains the variables that the shell is exporting at the time the shell runs the external command.

Normally, a variable is a place to store a value. You assign a value to the variable (var="some value"), and after that you can recall the value with a variable expansion (writing "$var" is equivalent to writing "some value").

It's possible to make variables that do something special when you assign a value to them, or in other circumstances where the shell accesses variables. An attribute on a variable is an annotation that the shell stores next to the variable's name and value, which tells the shell to apply this special behavior.

For example, declare -i x tells the shell that x must contain integer values only. Normally, when you assign a value to a variable, the shell takes the string that results from expanding the right-hand side of the equal sign and stores it as the variable's value. But if the variable has the integer attribute, the shell parses that string as an arithmetic expression and stores the result of evaluating that expression. For example:

$ x=2+2; echo $x
2+2
$ declare -i x; x=2+2; echo $x
4
$ declare -i x; x=2+hello; echo $x
2
$ declare -i x; x=2+
bash: 2+: syntax error: operand expected (error token is "+")

(The third line with x=2+hello sets x to 2 because hello is a variable name which is not defined, and unset variables are silently interpreted as 0 by default.)

Likewise, declare -l var declares that var must contain lowercase letters only. When the shell stores the value of the variable, it converts any uppercase letter to lowercase. declare -u var does the conversion in the other direction. declare -r var makes var read-only, which is also a special behavior of assignment: it causes every subsequent assignment to var to fail.

declare -x var causes var to be exported to the environment. For this attribute, the special behavior happens when bash runs an external command: external commands see an environment that contains the variables that the shell is exporting at the time the shell runs the external command.

Normally, a variable is a place to store a value. You assign a value to the variable (var="some value"), and after that you can recall the value with a variable expansion (writing "$var" is equivalent to writing "some value").

It's possible to make variables that do something special when you assign a value to them, or in other circumstances where the shell accesses variables. An attribute on a variable is an annotation that the shell stores next to the variable's name and value, which tells the shell to apply this special behavior.

One example

declare -i x tells the shell that x must contain integer values only. Normally, when you assign a value to a variable, the shell takes the string that results from expanding the right-hand side of the equal sign and stores it as the variable's value. But if the variable has the integer attribute, the shell parses that string as an arithmetic expression and stores the result of evaluating that expression. For example:

$ x=2+2; echo $x
2+2
$ declare -i x; x=2+2; echo $x
4
$ declare -i x; x=2+hello; echo $x
2
$ declare -i x; x=2+
bash: 2+: syntax error: operand expected (error token is "+")

(The third line with x=2+hello sets x to 2 because hello is a variable name which is not defined, and unset variables are silently interpreted as 0 by default.)

More examples

  • declare -l var declares that var must contain lowercase letters only. When the shell stores the value of the variable, it converts any uppercase letter to lowercase.
  • declare -u var does the conversion in the other direction.
  • declare -r var makes var read-only, which is also a special behavior of assignment: it causes every subsequent assignment to var to fail.
  • declare -x var causes var to be exported to the environment. For this attribute, the special behavior happens when bash runs an external command: external commands see an environment that contains the variables that the shell is exporting at the time the shell runs the external command.
Source Link
Gilles 'SO- stop being evil'
  • 865.3k
  • 205
  • 1.8k
  • 2.3k

Normally, a variable is a place to store a value. You assign a value to the variable (var="some value"), and after that you can recall the value with a variable expansion (writing "$var" is equivalent to writing "some value").

It's possible to make variables that do something special when you assign a value to them, or in other circumstances where the shell accesses variables. An attribute on a variable is an annotation that the shell stores next to the variable's name and value, which tells the shell to apply this special behavior.

For example, declare -i x tells the shell that x must contain integer values only. Normally, when you assign a value to a variable, the shell takes the string that results from expanding the right-hand side of the equal sign and stores it as the variable's value. But if the variable has the integer attribute, the shell parses that string as an arithmetic expression and stores the result of evaluating that expression. For example:

$ x=2+2; echo $x
2+2
$ declare -i x; x=2+2; echo $x
4
$ declare -i x; x=2+hello; echo $x
2
$ declare -i x; x=2+
bash: 2+: syntax error: operand expected (error token is "+")

(The third line with x=2+hello sets x to 2 because hello is a variable name which is not defined, and unset variables are silently interpreted as 0 by default.)

Likewise, declare -l var declares that var must contain lowercase letters only. When the shell stores the value of the variable, it converts any uppercase letter to lowercase. declare -u var does the conversion in the other direction. declare -r var makes var read-only, which is also a special behavior of assignment: it causes every subsequent assignment to var to fail.

declare -x var causes var to be exported to the environment. For this attribute, the special behavior happens when bash runs an external command: external commands see an environment that contains the variables that the shell is exporting at the time the shell runs the external command.