Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

redirect bash output and error for all commands

How to redirect all commands executed on the bash to /dev/null ?

It's obvious that for a command we have to do:

command > /dev/null  2>&1

How about all commands that will be executed further on ?

like image 616
TheForbidden Avatar asked May 22 '13 15:05

TheForbidden


2 Answers

Simply start a new shell:

bash >/dev/null 2>&1

Now you can type commands blind :) If you want to leave that mode type: exit

But typing commands blind will normally not what you want. So I would suggest to create a text file like script.sh, place your commands in it:

command1 foo 
command2 bar

and than execute it using:

bash script.sh >/dev/null 2>&1

The ouput of all commands in that script will be redirected to /dev/null now

like image 182
hek2mgl Avatar answered Nov 15 '22 10:11

hek2mgl


Use exec without a command:

exec > /dev/null 2>&1

As hex2mgl pointed out, if you do this in an interactive shell, you won't even see your prompt anymore, as the shell prints that to standard error. I assume this is intended for a script, as it doesn't make a lot of sense to ignore all output from commands run interactively :)

like image 31
chepner Avatar answered Nov 15 '22 10:11

chepner