Skip to content

Instantly share code, notes, and snippets.

@MoElaSec
Last active July 28, 2021 05:42
Show Gist options
  • Save MoElaSec/9e773b18ecc4867882794ebb9aa5e5eb to your computer and use it in GitHub Desktop.
Save MoElaSec/9e773b18ecc4867882794ebb9aa5e5eb to your computer and use it in GitHub Desktop.
🎯Redirect standard input/output/error for shell commands.

There are three I/O devices available on the command line:

1.standard input - 0

2.standard output - 1

3.standard error - 2

To redirect standard output (the default output) to a file (and overwrite the file), use:

command > file.log

To append to file.log, use two >

command >> file.log

To redirect standard error to the file.log, use

command 2> file.log

And to append

command 2>> file.log

To combine the outputs into one stream and send them all to one place

command > file.log 2>&1

This sends 2 (standard error) into 1 (standard output), and sends standard output to file.log

Notice that it's also possible to redirect standard input into a command that expects standard input

command << file.txt



Hide standard output:

./command >/dev/null

Hide standard output and standard error:

./command >/dev/null 2>&1

Hide standard output and standard error and release terminal (run the command in the background):

./command >/dev/null 2>&1 &


For more details, check out the Advanced Bash Scripting Guide.

note: this is collected from answer given here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment