0

Reproducible example

Consider this example:

#!/usr/bin/env bash
echo "[Status] $(killall --wait example)"

Expected output:

[Status] example: no process found

Actual result (being wrong order and 2 lines):

example: no process found

[Status]

Workaround

result=$(killall --wait example 2>&1)
echo "[Status] ${result}"

Questions

  1. How come the output isn't on the same line in the reproducible example?
  2. Apart from the workaround, is there any way to await the result of command substitution and print it on the same line (e.g. perhaps via process substitution with pipes)?
0

1 Answer 1

1

Change:

echo "[Status] $(killall --wait example)"

into:

echo "[Status] $(killall --wait example 2>&1)"

Explanation: killall prints out on standard error instead of standard output.

4
  • That's a one-liner version of the workaround. How come every other command substitution (like ls , df , getopt , type , podman) doesn't require a redirection?
    – GrabbenD
    Commented May 27, 2024 at 10:13
  • 1
    You require a redirection when what you want to capture is not printed on standard output, but on standard error. In general, progress and warning messages should not be mixed with the actual output of the command; that's why there are two output file handles.
    – tripleee
    Commented May 27, 2024 at 10:15
  • Thanks! Out of curiosity, are there any other ways of solving this issue (e.g. using a alternative syntax)?
    – GrabbenD
    Commented May 27, 2024 at 10:31
  • What sort of alternative syntax are you hoping for? Some shells have different redirection operators like &> and of course you can use the obsolescent ` `backtick syntax` ` instead of the modern $(command substitution) syntax.
    – tripleee
    Commented May 27, 2024 at 11:14

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.