0

I try to understant the if condition

log_daemon_msg () {
    if [ -z "${1:-}" ]; then
        return 1
    fi
    log_daemon_msg_pre "$@"

    if [ -z "${2:-}" ]; then
        echo -n "$1:" || true
        return
    fi

    echo -n "$1: $2" || true
    log_daemon_msg_post "$@"
}

what is mean "${1:-}" and "${2:-}"

Toto
  • 89,455
  • 62
  • 89
  • 125
looock7897
  • 27
  • 4
  • BTW, `echo -n "$1:"` is not good form -- it would be better written as `printf '%s:' "$1"` – Charles Duffy Aug 08 '21 at 17:04
  • Also, about `set -eu` -- see [BashFAQ #105](https://mywiki.wooledge.org/BashFAQ/105#Exercises) for discussion of why `set -e` is considered a bad idea among serious shell developers, and [BashFAQ #112](https://mywiki.wooledge.org/BashFAQ/112) discussing the pros and cons of `set -u` (while this isn't a bad idea in the way that `set -e` is, needing to use `${var:-}` instead of just `$var` when one wants unset values to not be harmful is one of the cons). – Charles Duffy Aug 08 '21 at 17:05
  • @CharlesDuffy To your first comment: why? – IMSoP Aug 08 '21 at 17:06
  • @IMSoP, the POSIX standard for `echo` makes `-n` _allowed_, but doesn't require it to have any particular behavior. `echo -n` is allowed to print `-n` on output, instead of suppressing newlines. – Charles Duffy Aug 08 '21 at 17:09
  • @CharlesDuffy Ah, so a potential portability problem, I didn't know that, thanks. – IMSoP Aug 08 '21 at 17:09
  • @IMSoP, ...moreover, bash can be configured (either at compile time, at runtime, or through environment variables) to have that behavior, so even when you know for sure your shell is bash, you don't know how `echo` will behave. – Charles Duffy Aug 08 '21 at 17:09
  • 1
    @IMSoP See https://unix.stackexchange.com/a/65819/3113 for an in-depth discussion, and the APPLICATION USAGE section of [the POSIX `echo` standard](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/echo.html#tag_20_37) recommending `printf` instead. – Charles Duffy Aug 08 '21 at 17:10

1 Answers1

2

See Shell Parameter Expansion, {parameter:-word}.

${parameter:-word}
If parameter is unset or null, the expansion of word is substituted. Otherwise, the value of parameter is substituted.

So ${1:-} is the first argument of the function log_daemon_msg or the empty string if the function was called without arguments or with an empty first argument.

Normally, that doesn't really make sense, as just writing $1 would have the same effect. However, if your script runs with set -u (exit when using an undefined variable) ${1:-} can be used to get the standard-behavior ($1 turns into the empty string if unset). But the echo -n "$1: $2" at the end would still fail in the case of missing arguments.

Socowi
  • 25,550
  • 3
  • 32
  • 54
  • I was about to post the same thing, but then wasn't able to find any reason this would be needed, since `"$1"` is already an empty string if `$1` is not set. – IMSoP Aug 08 '21 at 17:00
  • 1
    @IMSoP Since there are `|| true`s on the end of statements that aren't terminal to functions, this code is clearly written to be run with `set -e`. Someone who uses `set -e` is likely to also use `set -u`. – Charles Duffy Aug 08 '21 at 17:05
  • @Socowi, ...I don't believe that `set -u` exits can be avoided via `|| true`. See https://ideone.com/SMPT7x; in that respect, I believe the most recent edit makes the answer worse. – Charles Duffy Aug 08 '21 at 17:08
  • 1
    @CharlesDuffy Thank you for the notice. I fell into a trap: I tested it in an interactive shell session, but posix specifies "[An interactive shell shall not exit](https://pubs.opengroup.org/onlinepubs/009695399/utilities/set.html)". – Socowi Aug 08 '21 at 17:12