Is there a way to forcefully echo to the command-line from a bash function call?

StackOverflow https://stackoverflow.com/questions/23570487

  •  19-07-2023
  •  | 
  •  

سؤال

I've got a bash script where I define a bunch of functions in good programming style. These functions "return" values by echo-ing them. So, for example:

some_function() {
   echo "I like pie."
}

what_do_i_like=`some_function`

Now, I'd like to add in some debugging statements to my functions. Unfortunately, anything I echo just gets caught by the caller in the variable. e.g.,

some_function() {
    if [[ "${DEBUG}" = "${TRUE}" ]]; then
        echo "We're about to find out what we like."
    fi
    echo "I like pie."
}

what_do_i_like=`some_function`

... The debug statement gets caught in the what_do_i_like variable.

Is there any way I can bypass this and forcibly print to the command-line and have the output not be caught in the variable?

هل كانت مفيدة؟

المحلول

echo "We're about to find out what we like." >/dev/tty

نصائح أخرى

Doesn't echo to stderr work? echo 1>&2 message

There is a shell command interface for syslog(3):

logger -p DEBUG Hi there
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top