when i read linux boot scripts, i often meet this expression: ${1:-}. i can't understand it. what's more, i don't understand this condition: [ -z "${1:-}" ] too. anyone can help me ? thanks!!!

Dani AI

Generated

Short answer: this is the shell’s parameter‑expansion form that supplies a fallback when a parameter is missing. In the concrete case you saw it either yields the first positional argument or an empty string. That is why you often see it combined with an emptiness check in boot scripts.

Why scripts use it: many init/boot scripts turn on “nounset” (set -u) to catch typos. Under that option, referring to an unset positional parameter can abort the script. Using the expansion with an explicit empty fallback prevents that failure and makes tests for “no argument or empty argument” reliable. The colon in the operator also matters: it causes an empty string to be treated the same as “unset.” Quoting the expansion is important to avoid word‑splitting when the value contains spaces.

Practical effect (in words): if the script is invoked with one argument, the expression evaluates to that argument; if invoked with no arguments, it evaluates to nothing (so a test for zero length succeeds). That is exactly what ’s manual quote was getting at, but the key usage point is defensive coding under nounset and consistent handling of unset vs empty.

Related notes and troubleshooting:

  • There are sibling operators that assign defaults, raise errors, or substitute alternate text; pick the one whose semantics you need.
  • Always double‑quote expansions in tests.
  • To see why a script uses this, check for a top‑of‑script set -u or set -o nounset. A quick local experiment that shows the nounset failure is:
#!/bin/sh
set -u
echo "$1"    # will error if no argument is given

If you want a fallback value assigned when missing, the assignment‑form can be used (for example, to set a default string).

Recommended Answers

All 2 Replies

When i read linux boot scripts, i often meet this expression: ${1:-}. i can't understand it. what's more, i don't understand this condition: [ -z "${1:-}" ] too. anyone can help me ? thanks!!!

 ${parameter:-word}
           Use Default Values. If parameter is unset or null,
           the  expansion of word will be substituted; other-
           wise, the value of parameter will be substituted.

 -z string True, if length of string is zero.

doing 'man ksh' yields:

${parameter:-word}
           Use Default Values. If parameter is unset or null,
           the  expansion of word will be substituted; other-
           wise, the value of parameter will be substituted.

 -z string True, if length of string is zero.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.