Post History
The dash, -, is part of a Parameter Expansion syntax that tells Bash to check for existence of the preceding parameter, a.k.a. whether it is "set", substituting whatever follows if it isn't. For th...
Answer
#6: Post edited
The dash, `-`, is part of a [Parameter Expansion](https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html) syntax that tells Bash to check for existence of the preceding parameter, a.k.a. whether it is "set", substituting whatever follows if it isn't.- From the previously linked online Bash Manual:
- > if the colon is included, the operator tests for both parameter’s existence and that its value is not null; if the colon is omitted, the operator tests only for existence.
So `${2-}` means the same thing as `${2}`, except that Bash will test for existence of that parameter thanks to the presence of `-`.If a colon were included as well, e.g. `${2:-}`, Bash would additionally check that the value of the parameter is not null.- unset v
- echo "${v-hi}"
- echo "${v:-hi}"
- v=""
- echo "${v-hi}"
- echo "${v:-hi}"
- v=123
- echo "${v-hi}"
- echo "${v:-hi}"
- # OUTPUT:
- # > this_script.sh
- # hi
- # hi
- # <- empty string
- # hi
- # 123
- # 123
- **NOTE**: This syntax is very similar to a particular [Substring Expansion](https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html) syntax: `${parameter:offset}`. If your `offset` is negative, you must include a leading space to disambiguate from the Parameter Expansion syntax.
- unset string
- echo "${string:-3}"
- echo "${string: -3}"
- string=abcdefg
- echo "${string:-3}"
- echo "${string: -3}"
- # OUTPUT:
- # > this_script.sh
- # 3 <- alternative "3"
- # <- substring of an empty string
- # abcdefg <- alternative "3" not needed since string is set and not null
- # efg <- substring, last three characters
- As for `${2}` itself: It simply refers to the second [Positional Parameter](https://www.gnu.org/software/bash/manual/html_node/Positional-Parameters.html), whether a parameter of the script itself, or of a local function. In this case, since it's a single digit, it is equivalent to `$2`.
- # Second argument to the script
- echo "$2"
- func() {
- # Second argument to a function
- echo "$2"
- }
- func x y
- # OUTPUT:
- # > this_script.sh a b
- # b
- # y
So in your particular example, it seems the intent is to append the second argument to the array called "excludes", or append an empty string if there is no second argument. In other words, whether the argument is omitted or specified as an empty string, the result will be the same.
- The dash, `-`, is part of a [Parameter Expansion](https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html) syntax that tells Bash to check for existence of the preceding parameter, a.k.a. whether it is "set", substituting whatever follows if it isn't. For the particular case of `${2-}`, an empty string will be substituted if the second positional parameter doesn't exist.
- From the previously linked online Bash Manual:
- > if the colon is included, the operator tests for both parameter’s existence and that its value is not null; if the colon is omitted, the operator tests only for existence.
- So `${2-}` means the same thing as `${2}`, except that Bash will test for existence of that parameter thanks to the presence of `-`. If a colon were included as well, e.g. `${2:-}`, Bash would additionally check that the value of the parameter is not null.
- unset v
- echo "${v-hi}"
- echo "${v:-hi}"
- v=""
- echo "${v-hi}"
- echo "${v:-hi}"
- v=123
- echo "${v-hi}"
- echo "${v:-hi}"
- # OUTPUT:
- # > this_script.sh
- # hi
- # hi
- # <- empty string
- # hi
- # 123
- # 123
- **NOTE**: This syntax is very similar to a particular [Substring Expansion](https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html) syntax: `${parameter:offset}`. If your `offset` is negative, you must include a leading space to disambiguate from the Parameter Expansion syntax.
- unset string
- echo "${string:-3}"
- echo "${string: -3}"
- string=abcdefg
- echo "${string:-3}"
- echo "${string: -3}"
- # OUTPUT:
- # > this_script.sh
- # 3 <- alternative "3"
- # <- substring of an empty string
- # abcdefg <- alternative "3" not needed since string is set and not null
- # efg <- substring, last three characters
- As for `${2}` itself: It simply refers to the second [Positional Parameter](https://www.gnu.org/software/bash/manual/html_node/Positional-Parameters.html), whether a parameter of the script itself, or of a local function. In this case, since it's a single digit, it is equivalent to `$2`.
- # Second argument to the script
- echo "$2"
- func() {
- # Second argument to a function
- echo "$2"
- }
- func x y
- # OUTPUT:
- # > this_script.sh a b
- # b
- # y
- In the example you gave, it seems the intent is to append the second argument to the array called "excludes", or append an empty string if there is no second argument. In other words, whether the argument is omitted or specified as an empty string, the result will be the same.
#5: Post edited
- The dash, `-`, is part of a [Parameter Expansion](https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html) syntax that tells Bash to check for existence of the preceding parameter, a.k.a. whether it is "set", substituting whatever follows if it isn't.
- From the previously linked online Bash Manual:
- > if the colon is included, the operator tests for both parameter’s existence and that its value is not null; if the colon is omitted, the operator tests only for existence.
- So `${2-}` means the same thing as `${2}`, except that Bash will test for existence of that parameter thanks to the presence of `-`.
- If a colon were included as well, e.g. `${2:-}`, Bash would additionally check that the value of the parameter is not null.
- unset v
- echo "${v-hi}"
- echo "${v:-hi}"
- v=""
- echo "${v-hi}"
- echo "${v:-hi}"
- v=123
- echo "${v-hi}"
- echo "${v:-hi}"
- # OUTPUT:
- # > this_script.sh
- # hi
- # hi
- # <- empty string
- # hi
- # 123
- # 123
- **NOTE**: This syntax is very similar to a particular [Substring Expansion](https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html) syntax: `${parameter:offset}`. If your `offset` is negative, you must include a leading space to disambiguate from the Parameter Expansion syntax.
- unset string
echo "${string:-7}"echo "${string: -7}"string=01234567890abcdefghecho "${string:-7}"echo "${string: -7}"- # OUTPUT:
- # > this_script.sh
# 7## 01234567890abcdefgh# bcdefgh- As for `${2}` itself: It simply refers to the second [Positional Parameter](https://www.gnu.org/software/bash/manual/html_node/Positional-Parameters.html), whether a parameter of the script itself, or of a local function. In this case, since it's a single digit, it is equivalent to `$2`.
- # Second argument to the script
- echo "$2"
- func() {
- # Second argument to a function
- echo "$2"
- }
- func x y
- # OUTPUT:
- # > this_script.sh a b
- # b
- # y
- So in your particular example, it seems the intent is to append the second argument to the array called "excludes", or append an empty string if there is no second argument. In other words, whether the argument is omitted or specified as an empty string, the result will be the same.
- The dash, `-`, is part of a [Parameter Expansion](https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html) syntax that tells Bash to check for existence of the preceding parameter, a.k.a. whether it is "set", substituting whatever follows if it isn't.
- From the previously linked online Bash Manual:
- > if the colon is included, the operator tests for both parameter’s existence and that its value is not null; if the colon is omitted, the operator tests only for existence.
- So `${2-}` means the same thing as `${2}`, except that Bash will test for existence of that parameter thanks to the presence of `-`.
- If a colon were included as well, e.g. `${2:-}`, Bash would additionally check that the value of the parameter is not null.
- unset v
- echo "${v-hi}"
- echo "${v:-hi}"
- v=""
- echo "${v-hi}"
- echo "${v:-hi}"
- v=123
- echo "${v-hi}"
- echo "${v:-hi}"
- # OUTPUT:
- # > this_script.sh
- # hi
- # hi
- # <- empty string
- # hi
- # 123
- # 123
- **NOTE**: This syntax is very similar to a particular [Substring Expansion](https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html) syntax: `${parameter:offset}`. If your `offset` is negative, you must include a leading space to disambiguate from the Parameter Expansion syntax.
- unset string
- echo "${string:-3}"
- echo "${string: -3}"
- string=abcdefg
- echo "${string:-3}"
- echo "${string: -3}"
- # OUTPUT:
- # > this_script.sh
- # 3 <- alternative "3"
- # <- substring of an empty string
- # abcdefg <- alternative "3" not needed since string is set and not null
- # efg <- substring, last three characters
- As for `${2}` itself: It simply refers to the second [Positional Parameter](https://www.gnu.org/software/bash/manual/html_node/Positional-Parameters.html), whether a parameter of the script itself, or of a local function. In this case, since it's a single digit, it is equivalent to `$2`.
- # Second argument to the script
- echo "$2"
- func() {
- # Second argument to a function
- echo "$2"
- }
- func x y
- # OUTPUT:
- # > this_script.sh a b
- # b
- # y
- So in your particular example, it seems the intent is to append the second argument to the array called "excludes", or append an empty string if there is no second argument. In other words, whether the argument is omitted or specified as an empty string, the result will be the same.
#4: Post edited
- The dash, `-`, is part of a [Parameter Expansion](https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html) syntax that tells Bash to check for existence of the preceding parameter, a.k.a. whether it is "set", substituting whatever follows if it isn't.
- From the previously linked online Bash Manual:
- > if the colon is included, the operator tests for both parameter’s existence and that its value is not null; if the colon is omitted, the operator tests only for existence.
So `${2-}` means the same thing as `${2}`, except that Bash also tests for existence of that parameter thanks to the presence of the dash, `-`.- If a colon were included as well, e.g. `${2:-}`, Bash would additionally check that the value of the parameter is not null.
- unset v
- echo "${v-hi}"
- echo "${v:-hi}"
- v=""
- echo "${v-hi}"
- echo "${v:-hi}"
- v=123
- echo "${v-hi}"
- echo "${v:-hi}"
- # OUTPUT:
- # > this_script.sh
- # hi
- # hi
- # <- empty string
- # hi
- # 123
- # 123
- **NOTE**: This syntax is very similar to a particular [Substring Expansion](https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html) syntax: `${parameter:offset}`. If your `offset` is negative, you must include a leading space to disambiguate from the Parameter Expansion syntax.
- unset string
- echo "${string:-7}"
- echo "${string: -7}"
- string=01234567890abcdefgh
- echo "${string:-7}"
- echo "${string: -7}"
- # OUTPUT:
- # > this_script.sh
- # 7
- #
- # 01234567890abcdefgh
- # bcdefgh
- As for `${2}` itself: It simply refers to the second [Positional Parameter](https://www.gnu.org/software/bash/manual/html_node/Positional-Parameters.html), whether a parameter of the script itself, or of a local function. In this case, since it's a single digit, it is equivalent to `$2`.
- # Second argument to the script
- echo "$2"
- func() {
- # Second argument to a function
- echo "$2"
- }
- func x y
- # OUTPUT:
- # > this_script.sh a b
- # b
# y
- The dash, `-`, is part of a [Parameter Expansion](https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html) syntax that tells Bash to check for existence of the preceding parameter, a.k.a. whether it is "set", substituting whatever follows if it isn't.
- From the previously linked online Bash Manual:
- > if the colon is included, the operator tests for both parameter’s existence and that its value is not null; if the colon is omitted, the operator tests only for existence.
- So `${2-}` means the same thing as `${2}`, except that Bash will test for existence of that parameter thanks to the presence of `-`.
- If a colon were included as well, e.g. `${2:-}`, Bash would additionally check that the value of the parameter is not null.
- unset v
- echo "${v-hi}"
- echo "${v:-hi}"
- v=""
- echo "${v-hi}"
- echo "${v:-hi}"
- v=123
- echo "${v-hi}"
- echo "${v:-hi}"
- # OUTPUT:
- # > this_script.sh
- # hi
- # hi
- # <- empty string
- # hi
- # 123
- # 123
- **NOTE**: This syntax is very similar to a particular [Substring Expansion](https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html) syntax: `${parameter:offset}`. If your `offset` is negative, you must include a leading space to disambiguate from the Parameter Expansion syntax.
- unset string
- echo "${string:-7}"
- echo "${string: -7}"
- string=01234567890abcdefgh
- echo "${string:-7}"
- echo "${string: -7}"
- # OUTPUT:
- # > this_script.sh
- # 7
- #
- # 01234567890abcdefgh
- # bcdefgh
- As for `${2}` itself: It simply refers to the second [Positional Parameter](https://www.gnu.org/software/bash/manual/html_node/Positional-Parameters.html), whether a parameter of the script itself, or of a local function. In this case, since it's a single digit, it is equivalent to `$2`.
- # Second argument to the script
- echo "$2"
- func() {
- # Second argument to a function
- echo "$2"
- }
- func x y
- # OUTPUT:
- # > this_script.sh a b
- # b
- # y
- So in your particular example, it seems the intent is to append the second argument to the array called "excludes", or append an empty string if there is no second argument. In other words, whether the argument is omitted or specified as an empty string, the result will be the same.
#3: Post edited
The dash, `-`, tells Bash to check for existence of the preceding parameter, a.k.a. whether it is "set".From the [online Bash Manual](https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html):- > if the colon is included, the operator tests for both parameter’s existence and that its value is not null; if the colon is omitted, the operator tests only for existence.
- So `${2-}` means the same thing as `${2}`, except that Bash also tests for existence of that parameter thanks to the presence of the dash, `-`.
- If a colon were included as well, e.g. `${2:-}`, Bash would additionally check that the value of the parameter is not null.
As for `${2}`: It's a [shell expansion](https://www.gnu.org/software/bash/manual/html_node/Shell-Expansions.html) that evaluates to the value of the second [positional parameter](https://www.gnu.org/software/bash/manual/html_node/Positional-Parameters.html). In this case, since it's a single digit, it is equivalent to `$2`.
- The dash, `-`, is part of a [Parameter Expansion](https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html) syntax that tells Bash to check for existence of the preceding parameter, a.k.a. whether it is "set", substituting whatever follows if it isn't.
- From the previously linked online Bash Manual:
- > if the colon is included, the operator tests for both parameter’s existence and that its value is not null; if the colon is omitted, the operator tests only for existence.
- So `${2-}` means the same thing as `${2}`, except that Bash also tests for existence of that parameter thanks to the presence of the dash, `-`.
- If a colon were included as well, e.g. `${2:-}`, Bash would additionally check that the value of the parameter is not null.
- unset v
- echo "${v-hi}"
- echo "${v:-hi}"
- v=""
- echo "${v-hi}"
- echo "${v:-hi}"
- v=123
- echo "${v-hi}"
- echo "${v:-hi}"
- # OUTPUT:
- # > this_script.sh
- # hi
- # hi
- # <- empty string
- # hi
- # 123
- # 123
- **NOTE**: This syntax is very similar to a particular [Substring Expansion](https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html) syntax: `${parameter:offset}`. If your `offset` is negative, you must include a leading space to disambiguate from the Parameter Expansion syntax.
- unset string
- echo "${string:-7}"
- echo "${string: -7}"
- string=01234567890abcdefgh
- echo "${string:-7}"
- echo "${string: -7}"
- # OUTPUT:
- # > this_script.sh
- # 7
- #
- # 01234567890abcdefgh
- # bcdefgh
- As for `${2}` itself: It simply refers to the second [Positional Parameter](https://www.gnu.org/software/bash/manual/html_node/Positional-Parameters.html), whether a parameter of the script itself, or of a local function. In this case, since it's a single digit, it is equivalent to `$2`.
- # Second argument to the script
- echo "$2"
- func() {
- # Second argument to a function
- echo "$2"
- }
- func x y
- # OUTPUT:
- # > this_script.sh a b
- # b
- # y
#2: Post edited
- The dash, `-`, tells Bash to check for existence of the preceding parameter, a.k.a. whether it is "set".
`${2}` is a [shell expansion](https://www.gnu.org/software/bash/manual/html_node/Shell-Expansions.html) that evaluates to the value of the second [positional parameter](https://www.gnu.org/software/bash/manual/html_node/Positional-Parameters.html). In this case, since it's a single digit, it is equivalent to `$2`.This syntax is very similar to `${2:-}`, where the colon, `:`, indicates one additional check. From the [online Bash Manual](https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html):- > if the colon is included, the operator tests for both parameter’s existence and that its value is not null; if the colon is omitted, the operator tests only for existence.
So combined, `${2-}` means the same thing as `${2}`, except that Bash also tests for existence of that parameter thanks to the presence of the dash, `-`.Note that if a colon had been included as well, e.g. `${2:-}`, Bash would also have checked that the value of the parameter is not null.
- The dash, `-`, tells Bash to check for existence of the preceding parameter, a.k.a. whether it is "set".
- From the [online Bash Manual](https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html):
- > if the colon is included, the operator tests for both parameter’s existence and that its value is not null; if the colon is omitted, the operator tests only for existence.
- So `${2-}` means the same thing as `${2}`, except that Bash also tests for existence of that parameter thanks to the presence of the dash, `-`.
- If a colon were included as well, e.g. `${2:-}`, Bash would additionally check that the value of the parameter is not null.
- As for `${2}`: It's a [shell expansion](https://www.gnu.org/software/bash/manual/html_node/Shell-Expansions.html) that evaluates to the value of the second [positional parameter](https://www.gnu.org/software/bash/manual/html_node/Positional-Parameters.html). In this case, since it's a single digit, it is equivalent to `$2`.
#1: Initial revision
The dash, `-`, tells Bash to check for existence of the preceding parameter, a.k.a. whether it is "set". `${2}` is a [shell expansion](https://www.gnu.org/software/bash/manual/html_node/Shell-Expansions.html) that evaluates to the value of the second [positional parameter](https://www.gnu.org/software/bash/manual/html_node/Positional-Parameters.html). In this case, since it's a single digit, it is equivalent to `$2`. This syntax is very similar to `${2:-}`, where the colon, `:`, indicates one additional check. From the [online Bash Manual](https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html): > if the colon is included, the operator tests for both parameter’s existence and that its value is not null; if the colon is omitted, the operator tests only for existence. So combined, `${2-}` means the same thing as `${2}`, except that Bash also tests for existence of that parameter thanks to the presence of the dash, `-`. Note that if a colon had been included as well, e.g. `${2:-}`, Bash would also have checked that the value of the parameter is not null.