Skip to content

Instantly share code, notes, and snippets.

@magnetikonline
Last active January 25, 2023 03:34
Show Gist options
  • Save magnetikonline/096e4ff4f37027e65d74 to your computer and use it in GitHub Desktop.
Save magnetikonline/096e4ff4f37027e65d74 to your computer and use it in GitHub Desktop.
Shell redirection cheatsheet.

Shell redirection cheatsheet

Examples

stdout to file

$ /bin/exec >/path/to/file.out

stderr to file

$ /bin/exec 2>/path/to/file.out

stdout to stderr

$ /bin/exec 1>&2

# or in most instances...
$ /bin/exec >&2
$ echo "Error: You did bad!" >&2

# note: redirections can be placed at either end if desired
$ 1>&2 /bin/exec 
$ >&2 /bin/exec 

stderr to stdout

$ /bin/exec 2>&1

stdout and stderr to file

Note: Only works with Bash shells.

$ /bin/exec &>/path/to/file.out

# as an alternative for both /bin/sh and /bin/bash
$ /bin/exec >/path/to/file.out 2>&1

string to stdin

$ myString="My string"
$ /bin/exec <<<$myString

# which is (somewhat) equivalent to (but does not create a sub-shell)
$ echo "My string" | /bin/exec

Reference

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