Meaning of> & 2 at the beginning of the line (used to control the container startup order by docker-compose)

Question: Question:

I have a question about shell scripts.

There are >&2 descriptions in the code below, what does that mean?

#!/bin/bash

set -e

host="$1"
shift
cmd="$@"

until psql -h "$host" -U "postgres" -c '\l'; do
  >&2 echo "Postgres is unavailable - sleeping"
  sleep 1
done

>&2 echo "Postgres is up - executing command"
exec $cmd

引用元: http://docs.docker.jp/compose/startup-order.html

There is an understanding of redirecting ls standard output to standard error output in the form $ ls >&2 .

My current perception may be redirecting the result of psql -h "$host" -U "postgres" -c '\l'; to standard error.
However, I haven't seen such a description, so I can't understand it. I'm also wondering why I'm redirecting to the error output.

It's been a long time, but please teach me.

Answer: Answer:

>&2 written before the command also means to redirect to standard error output. It is used to send the shell script log to the error output.

The Bash manual 3.6 Redirections states :

The following redirection operators may precede or appear anywhere within a simple command or may follow a command.
The following operators for redirection can be placed before, in the middle of, or after a simple command.

If you use it like this time, only the output of echo will be sent to the error output. In other words, the log message of this shell script is output as an error.

Scroll to Top