redirect bash output and error for all commands
Asked Answered
S

3

7

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 ?

Septum answered 22/5, 2013 at 15:42 Comment(0)
G
5

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

Groundwork answered 22/5, 2013 at 15:44 Comment(5)
I got this: [1] 13407 bash: 2: No such file or directory 1: command not found [1]+ Exit 127 bash 2 > /dev/nullSeptum
try /usr/bin/bash instead of bash. Btw, which system are you using?Groundwork
yeah I do have it, but he doesn't recognize the 2 and the 1Septum
Type it by your own hands no paste and copyGroundwork
yes it worked. But if I want to redirect only the error output ?Septum
O
3

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 :)

Orvie answered 22/5, 2013 at 15:48 Comment(2)
It's not blocked; all output (both standard output and standard error, which includes your prompt) has been redirected to /dev/null, as requested.Orvie
exec 2>/dev/null. But that will still redirect your typing and the prompt to /dev/null/.Orvie
G
0

for scripting or other practical purpose, grouping the command is a nice solution. check http://www.gnu.org/software/bash/manual/html_node/Compound-Commands.html It says specifically Any redirections (see Redirections) associated with a compound command apply to all commands within that compound command unless explicitly overridden.

Gentleness answered 22/5, 2013 at 15:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.