Skip to main content
Added arithmetic evaluation contexts, for completeness.
Source Link
rici
  • 10k
  • 1
  • 42
  • 39

The shell also implements a variety of conditional expressionsexpansions, including expansions with side effects:

Conditional errors:

###The colon In all of the conditional syntaxes, it can be seen that using a : makes unset variables and variables set to the empty string act in the same way. That's usually a good idea, because sometimes it is not easy to unset a variable. For example, in the local assignment syntax shown immediately above, it is easy to assign a child variable to the empty string, but not at all easy to remove a variable from the child environment:

$ export foo=Goodbye
$ foo= dash -c 'echo "${foo-Hello}", world'
, world
$ foo= dash -c 'echo "${foo:-Hello}", world'
Hello, world
$ echo "$foo"
Goodbye

###Conditional expansions as commands Sometimes it is convenient to just be able to execute a conditional expansion for its side effect, without actually using it in a command. Technically this is not possible, but you can use the special builtin command :, which does nothing:

# Equivalent to:
#  if [ -z "$x" ]; then x="default value"; fi
: "${x:=default value}"
# Equivalent to:
#   if [ $# -lt 3 ]; then
#     echo "Three arguments are required" 1>&2
#     exit 1
#   fi
: "${3?Three arguments are required.}" 

###Variable assignments The shell provides a simple syntax for assigning a value to a scalar variable:

###The colon In all of the conditional syntaxes, it can be seen that using a : makes unset variables and variables set to the empty string act in the same way. That's usually a good idea, because sometimes it is not easy to unset a variable. For example, in the local###Extended assignment syntax shown immediately above, it is easy to assign a child variable to the empty string, but not at all easy to remove a variable from the child environment:

$ export foo=Goodbye
$ foo= dash -c 'echo "${foo-Hello}", world'
, world
$ foo= dash -c 'echo "${foo:-Hello}", world'
Hello, world
$ echo "$foo"
Goodbye

###Mutating assignmentssyntaxes

Finally, someSome shells have associative arrays, which support indexed assignments. Consult individual shell manuals for details on associative arrays, including how to declare them.

###Assignments in arithmetic evaluation

Within arithmetic evaluation, C-like assignment operators (=, *=, +=, etc.) are available, but they only perform arithmetic assignment. A Posix shell only has two arithmetic evaluation contexts: the arithmetic expansion syntax $((...)) and the for ((expr; expr; expr)) compound statement. (You can use the : special builtin as above to turn an arithmetic expansion into a statement.)

However, most shells also allow conditional statements ((...)) and will also use arithmetic evaluation in context requiring a number, such as array subscripts and assignment to variables declared to be integers. Details vary; consult shell manuals.

Arithmetic evaluation contexts need to be treated with caution because in an arithmetic evaluation context, variable expansions are reinterpreted as arithmetic expression, not just as integers. In some shells (bash, for example), this can be exploited with injection attacks. Best practice is to never use an untrusted variable in an arithmetic evaluation context.

The shell also implements a variety of conditional expressions:

Conditional errors:

###Variable assignments The shell provides a simple syntax for assigning a value to a scalar variable:

###The colon In all of the conditional syntaxes, it can be seen that using a : makes unset variables and variables set to the empty string act in the same way. That's usually a good idea, because sometimes it is not easy to unset a variable. For example, in the local assignment syntax shown immediately above, it is easy to assign a child variable to the empty string, but not at all easy to remove a variable from the child environment:

$ export foo=Goodbye
$ foo= dash -c 'echo "${foo-Hello}", world'
, world
$ foo= dash -c 'echo "${foo:-Hello}", world'
Hello, world
$ echo "$foo"
Goodbye

###Mutating assignments

Finally, some shells have associative arrays, which support indexed assignments. Consult individual shell manuals for details on associative arrays, including how to declare them.

The shell also implements a variety of conditional expansions, including expansions with side effects:

Conditional errors

###The colon In all of the conditional syntaxes, it can be seen that using a : makes unset variables and variables set to the empty string act in the same way. That's usually a good idea, because sometimes it is not easy to unset a variable. For example, in the local assignment syntax shown immediately above, it is easy to assign a child variable to the empty string, but not at all easy to remove a variable from the child environment:

$ export foo=Goodbye
$ foo= dash -c 'echo "${foo-Hello}", world'
, world
$ foo= dash -c 'echo "${foo:-Hello}", world'
Hello, world
$ echo "$foo"
Goodbye

###Conditional expansions as commands Sometimes it is convenient to just be able to execute a conditional expansion for its side effect, without actually using it in a command. Technically this is not possible, but you can use the special builtin command :, which does nothing:

# Equivalent to:
#  if [ -z "$x" ]; then x="default value"; fi
: "${x:=default value}"
# Equivalent to:
#   if [ $# -lt 3 ]; then
#     echo "Three arguments are required" 1>&2
#     exit 1
#   fi
: "${3?Three arguments are required.}" 

###Variable assignments The shell provides a simple syntax for assigning a value to a scalar variable:

###Extended assignment syntaxes

Some shells have associative arrays, which support indexed assignments. Consult individual shell manuals for details on associative arrays, including how to declare them.

###Assignments in arithmetic evaluation

Within arithmetic evaluation, C-like assignment operators (=, *=, +=, etc.) are available, but they only perform arithmetic assignment. A Posix shell only has two arithmetic evaluation contexts: the arithmetic expansion syntax $((...)) and the for ((expr; expr; expr)) compound statement. (You can use the : special builtin as above to turn an arithmetic expansion into a statement.)

However, most shells also allow conditional statements ((...)) and will also use arithmetic evaluation in context requiring a number, such as array subscripts and assignment to variables declared to be integers. Details vary; consult shell manuals.

Arithmetic evaluation contexts need to be treated with caution because in an arithmetic evaluation context, variable expansions are reinterpreted as arithmetic expression, not just as integers. In some shells (bash, for example), this can be exploited with injection attacks. Best practice is to never use an untrusted variable in an arithmetic evaluation context.

Fixed += description to make it clearer that it is a (common) extension. Ditto arrays.
Source Link
rici
  • 10k
  • 1
  • 42
  • 39

The shell###Mutating assignments

Some shells (bash, ksh and zsh, for example) also providesprovide the syntax

which adds value to var. What "adds" means depends on what type of variable var is: For normal string variables, value is appended to the end of var; while for array variables declared as integer, value is pushed onto the end of the array;(mathematically) added. In ksh, zsh and some versions of bash, this assignment syntax can be used for integer variableschild assignments as part of a command. Consult individual shell manuals for the details on how to declare a variable to be numeric. value(It is (mathematicallynot sufficient to simply assign a number to the variable.) added

These shells also have array variables, which are assigned and appended to using the syntaxes:

array=(value1 value2 ...)
array+=(value3 ...)

The parentheses in the above assignments are syntactic; you can think of the open parenthesis as being part of the assignment token. Array assignments cannot be used as part of a simple command, since arrays cannot be exported to the child environment.

Finally, some shells have associative arrays, which support indexed assignments. Consult individual shell manuals for details on associative arrays, including how to declare them.

The shell also provides the syntax

which adds value to var. What "adds" means depends on what type of variable var is: For normal string variables, value is appended to the end of var; for array variables, value is pushed onto the end of the array; and for integer variables value is (mathematically) added.

###Mutating assignments

Some shells (bash, ksh and zsh, for example) also provide the syntax

which adds value to var. What "adds" means depends on what type of variable var is: For normal string variables, value is appended to the end of var; while for variables declared as integer, value is (mathematically) added. In ksh, zsh and some versions of bash, this assignment syntax can be used for child assignments as part of a command. Consult individual shell manuals for the details on how to declare a variable to be numeric. (It is not sufficient to simply assign a number to the variable.)

These shells also have array variables, which are assigned and appended to using the syntaxes:

array=(value1 value2 ...)
array+=(value3 ...)

The parentheses in the above assignments are syntactic; you can think of the open parenthesis as being part of the assignment token. Array assignments cannot be used as part of a simple command, since arrays cannot be exported to the child environment.

Finally, some shells have associative arrays, which support indexed assignments. Consult individual shell manuals for details on associative arrays, including how to declare them.

added 16 characters in body
Source Link
mikeserv
  • 59.4k
  • 10
  • 122
  • 242
$ unset x
$ echo Hello, $"${x=world}"
Hello, world
$ echo "$x"
world

$ # This sets x to the empty string
$ x=
$ echo Hello, $"${x=world}"
Hello,
$ echo "$x"

$ echo Hello, $"${x:=world}"
Hello, world
$ echo "$x"
world
$ unset x
$ echo Hello, $"${x-world}"
Hello, world
$ echo "$x"

$ # This sets x to the empty string
$ x=
$ echo Hello, $"${x-world}"
Hello,
$ echo Hello, $"${x:-world}"
Hello, world
$ echo "$x"
$ export foo=Goodbye
$ foo= dash -c 'echo $"${foo-Hello}", world'
, world
$ foo= dash -c 'echo $"${foo:-Hello}", world'
Hello, world
$ echo "$foo"
Goodbye
$ unset x
$ echo Hello, ${x=world}
Hello, world
$ echo "$x"
world

$ # This sets x to the empty string
$ x=
$ echo Hello, ${x=world}
Hello,
$ echo "$x"

$ echo Hello, ${x:=world}
Hello, world
$ echo "$x"
world
$ unset x
$ echo Hello, ${x-world}
Hello, world
$ echo "$x"

$ # This sets x to the empty string
$ x=
$ echo Hello, ${x-world}
Hello,
$ echo Hello, ${x:-world}
Hello, world
$ echo "$x"
$ export foo=Goodbye
$ foo= dash -c 'echo ${foo-Hello}, world'
, world
$ foo= dash -c 'echo ${foo:-Hello}, world'
Hello, world
$ echo "$foo"
Goodbye
$ unset x
$ echo Hello, "${x=world}"
Hello, world
$ echo "$x"
world

$ # This sets x to the empty string
$ x=
$ echo Hello, "${x=world}"
Hello,
$ echo "$x"

$ echo Hello, "${x:=world}"
Hello, world
$ echo "$x"
world
$ unset x
$ echo Hello, "${x-world}"
Hello, world
$ echo "$x"

$ # This sets x to the empty string
$ x=
$ echo Hello, "${x-world}"
Hello,
$ echo Hello, "${x:-world}"
Hello, world
$ echo "$x"
$ export foo=Goodbye
$ foo= dash -c 'echo "${foo-Hello}", world'
, world
$ foo= dash -c 'echo "${foo:-Hello}", world'
Hello, world
$ echo "$foo"
Goodbye
added 2367 characters in body
Source Link
rici
  • 10k
  • 1
  • 42
  • 39
Loading
Source Link
rici
  • 10k
  • 1
  • 42
  • 39
Loading