0

I have this command which may output an error, so I hide it using &>/dev/null.

Now I would like to run this command, then run another command.

This is what I have now:

( aaa &>/dev/null ) && bbb

I'm expecting to see:

Command 'bb' not found...

But I do not see any output. Maybe it is because the first command does not exist? If it does exist, the second command will run.

I also tried using || instead of &&. This does not work when the first command actually does succeed.

How do I achieve this for both a succesful first command run and an unsuccesful case in a single line?

Daniel T
  • 4,594
Z0q
  • 125

1 Answers1

3

You can use ; instead of || or &&. The ; just separates commands and the second command doesn't depend on the first at all:

$ false; echo yeah
yeah
$ true; echo yeah
yeah
$ false || echo yeah
yeah
$ false && echo yeah
$ true && echo yeah
yeah
$ true || echo yeah
$ 

Also, using ( command ) means that command will run in its own subshell. You can use { command; } if you want to group commands without subshells, but here you don't need any of that. You can just do:

aaa &>/dev/null ; bbb

That will first run aaa, redirecting both output and error to /dev/null, and then it will run bbb. Note that you didn't need the subshell for && or || either. You could just have done aaa >/dev/null && bbb and it would have done the same thing.

Finally, if all you want is to ignore the error message, you can redirect standard error only, no need to also redirect output:

aaa 2>/dev/null

And here are some useful references for the things I mentioned:

terdon
  • 100,812