Bash built-in that always fails with a specific error code?

I think I can write it myself as

unrollme-dev-dan:~ Dan$ echo 'exit $@' > fail
unrollme-dev-dan:~ Dan$ chmod +x fail
unrollme-dev-dan:~ Dan$ ./fail 42
unrollme-dev-dan:~ Dan$ echo $?
42

Is there a built-in module that does this? exit, of course, will not work, because it will exit the current process, and will not create a child process and exit it. test- This is the easiest way to find 0or as the return code 1.

Like http://httpstat.us/ I need unit test specific error codes. This was incredibly difficult for Google, since most of the results were to handle errors, not cause them.

+4
source share
2 answers

:

( exit 42 )
+4

POSIX . , , , ​​

fail() {
  return $(( $1 ))
}

.

a=6
fail a

6. return $1, .

+2

Source: https://habr.com/ru/post/1535074/


All Articles