Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Post History

66%
+2 −0
Q&A What does ${2-} mean in bash?

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...

posted 22d ago by nogjam‭  ·  edited 20d ago by nogjam‭

Answer
#6: Post edited by user avatar nogjam‭ · 2024-11-01T18:06:49Z (20 days ago)
Improve the answer's "abstract"
  • 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 by user avatar nogjam‭ · 2024-11-01T17:55:21Z (20 days ago)
Clarify substring expansion example
  • 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.
  • 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 by user avatar nogjam‭ · 2024-11-01T17:47:30Z (20 days ago)
Add conclusion referring to the specific code in OP's question
  • 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 by user avatar nogjam‭ · 2024-11-01T17:33:21Z (20 days ago)
Clarify meaning of "checks for" and add examples
  • 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 by user avatar nogjam‭ · 2024-10-30T21:23:35Z (21 days ago)
Reorder answer for clarity
  • 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 by user avatar nogjam‭ · 2024-10-30T19:27:07Z (22 days ago)
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.