2

I was wondering if it is possible to remove the error messages from bash. So for example, a bash script take a users input and if it runs properly in the terminal it doesn't return anything (it just runs the command), but if it fails, then it removes the error message and runs another script. Initially I thought this would be possible using \e[K, but that won't work I don't think. || doesn't work either as it still returns the error message before running the secondary command. Is there any way I can do this.

Thanks Ben

bd3dowling
  • 35
  • 1
  • 4

1 Answers1

3

You could say:

bash script1 2>/dev/null || bash script2

This would redirect STDERR of script1 to /dev/null and execute script2 if the former exited with a non-zero status code.

devnull
  • 118,548
  • 33
  • 236
  • 227
  • Yes that worked, thanks. I will use that. But is it possible to use an ASCII escape code of some form? Just wondering. – bd3dowling Jan 06 '14 at 13:39
  • @bd3dowling I'm not sure what you're trying to say. Note that the _error_ messages from the script would be written to `STDERR` and you'd either need to redirect those to `/dev/null` to get rid of those, or have those redirected to `STDOUT` and manipulate it. – devnull Jan 06 '14 at 13:42
  • I see, ok. I get it now. – bd3dowling Jan 06 '14 at 13:45
  • ANSI escape codes could only be used to affect how the error messages are displayed on the terminal, specifically by erasing them after the fact. Redirecting them to `/dev/null` prevents them from ever being displayed in the first case, a much cleaner solution. – chepner Jan 06 '14 at 14:10