Oer Oer - 1 year ago 51
Bash Question

Read specific information from the "echo" command while printing and ignoring others

I am writing some bash scripts and I am faced with the following problem. Let's say:

function echo_ignore_print()
echo "ignoring this print" # print this line for the user without storing its value
echo "$value" # return this value


echo "$info_to_keep" # ignoring this print 187fef

I need to be able to print "ignoring this print" as soon as the script gets to that line but I also need to return the value of the variable "$value" by itself, so I can use it later on. I would rather not have global variables.

Therefore, is there a way to king of dump all the string of the echo command and save some for later? or any other way to do this?

Thank you for your help.

Answer Source

You can write to-be-ignored stuff on stderr instead of stdout:

echo_ignore_print() {
   echo "ignoring this print" >&2
   echo "$value"

ignoring this print

echo "$info_to_keep"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download