2

I have a script with some functions (~/func/functions.sh) and I have the other script (~/scripts/example.sh)

Code: functions.sh

 #!/bin/bash
 function NameofFunction()
 {
  # do something...
  echo -e "\e[31m[ ERROR ]\e[39m more text..." 1>&2
  }

Code: example.sh (work well)

#!/bin/bash

. ~/func/functions.sh
function functioninExample()
{
#do something...
NameofFunction ${VAR1} ${VAR2}

}

functioninExample 2>/dev/null

Code: example.sh (doesn't work)

#!/bin/bash

. ~/func/functions.sh
function functioninExample()
{
#do something...
NameofFunction ${VAR1} ${VAR2} 2>/dev/null

}

functioninExample

How can I redirect the echo from my function without editing the function?

NameofFunction ${VAR1} ${VAR2} 2>/dev/null 

doesn't work.

How can I redirect the echo from my function without redirecting the functioninExample function?

FaxMax
  • 726
  • Could you please change ${VAR1}, ${VAR2} to some real strings and show us what is your function printing exactly? – rudimeier Sep 28 '16 at 13:15
  • example for ${VAR1} 50000 and for ${VAR2} 2000 i start some pearl scripts and check the arguments of my script – FaxMax Sep 28 '16 at 21:34

1 Answers1

5

This is because your function is printing to stdout not stderr, try

NameofFunction ${VAR1} ${VAR2} >/dev/null

or redirect both stderr and stdout:

NameofFunction ${VAR1} ${VAR2} >/dev/null 2>&1

Note it's good style to print errors to stderr, so instead of my answer above you should better change your function, like this:

echo -e "\e[31m[ ERROR ]\e[39m more text..." 1>&2
rudimeier
  • 10,315
  • Hi, this dosnt work, i tested it before – FaxMax Sep 28 '16 at 13:11
  • @FaxMax Using the exact code you posted it worked for me, please make a minimal example that actually doesn't work. – Random832 Sep 28 '16 at 14:27
  • @Random832 you are right, i testet it with 2 new files, i updated my question. thanks! – FaxMax Sep 28 '16 at 15:59
  • @FaxMax Both of your examples still work for me. What does it do, and are you actually running your examples to confirm that they don't work? If whatever you're really doing isn't working, maybe the problem is in something you cut out for the examples. – Random832 Sep 28 '16 at 18:52