Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can bash echo command return non-zero exit code?

Tags:

bash

unix

sh

echo

Like the title says, is there any case where echo will exit non-zero in bash/sh?

code ex.

until monitor_thing_happens; do
  test $retry_counter -eq 0 && echo "thing didn't happen" && exit 1
  let "retry_counter--"
  echo "tries remaining: ${retry_counter}"
  sleep 5
done

In the above example, if echo exits non-zero, the && logic breaks, we never exit 1, and we loop forever. Any danger / edge case where echo can exit non-zero?

like image 604
jdf Avatar asked Apr 15 '16 15:04

jdf


3 Answers

Nope, no risk. From man bash:

echo [-neE] [arg ...]
Output the args, separated by spaces, followed by a newline. The return status is always 0. If -n is specified, the trailing newline is suppressed. If the -e option is given, interpretation of the following backslash-escaped characters is enabled. The -E option disables the interpretation of these escape characters, even on systems where they are interpreted by default. The xpg_echo shell option may be used to dynamically determine whether or not echo expands these escape characters by default. echo does not interpret -- to mean the end of options. echo interprets the following escape sequences:

Emphasis on "The return status is always 0".

From a code quality standpoint, I would recommend not using test unless you're forced to for shell compatibility reasons. In general, use [[, but for arithmetic expressions you can also use ((:

# The generic way
[[ $retry_counter -eq 0 ]] && echo "Thing didn't happen" && exit 1

# The arithmetic way
(( retry_counter == 0 )) && echo "Thing didn't happen" && exit 1
like image 163
Mr. Llama Avatar answered Oct 24 '22 22:10

Mr. Llama


Yes, echo has a non-zero return status if there's a write error.

Quoting the bash manual:

'echo'

echo [-neE] [ARG ...]

Output the ARGs, separated by spaces, terminated with a newline. The return status is 0 unless a write error occurs.

A demonstration:

$ cat foo.bash
#!/bin/bash

echo hello
echo "The echo command returned a status of $?" > /dev/tty
$ ./foo.bash > /dev/full
./foo.bash: line 3: echo: write error: No space left on device
The echo command returned a status of 1
$ 

/dev/full is a device, similar to /dev/zero except that any attempt to write to it will fail with an ENOSPC error.

like image 23
Keith Thompson Avatar answered Oct 24 '22 21:10

Keith Thompson


From help man (bash):

Exit Status:

Returns success unless a write error occurs.

UPDATED

So if you echo to a stream that suddenly fails, you will get another exit code.

like image 43
totoro Avatar answered Oct 24 '22 20:10

totoro