What is the difference between these three ways to redirect to STDERR in bash?

I have been using the warning function in bash for some time.

function warn { [ $# -gt 0 ] && printf '%s\n' "$*" >&2 ; }

But I was recently told that I should use one of the following constructs.

function warn { [ $# -gt 0 ] && printf >&2 '%s\n' "$*" ; }

or

function warn { [ $# -gt 0 ] && printf >&2 -- '%s\n' "$*" ; }

I think I understand --(if the passed parameters contain a dash, it will not be parsed as a parameter).

But what is the difference between redirecting immediately after printfor at the end?

+4
source share
2 answers

Not a single offer is required. The problem is --trying to solve if you did something like

printf "$somestring" >&2

$somestring ( ). '%s\n', , . printf.

>&2 , , .

- ,

  • function POSIX.
  • , , , , .

warn () { [ $# -gt 0 ] && printf '%s\n' "$1" >&2 ; }

warn "Do not do that"  # not warn do not do that
+3

. - , bash, . , .. >&2 printf ....

0

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


All Articles