Skip to content

Latest commit

 

History

History
75 lines (54 loc) · 892 Bytes

bash-redirect.md

File metadata and controls

75 lines (54 loc) · 892 Bytes

Bash redirect IO

stderr as stdout

# stdout → stderr
>&2 echo "foo"
echo "foo" 1>&2

# stderr → stdout
echo "foo" 2>&1

# when combining with other redirects,
# stdout/stderr must come last.  E.g. suppress all output:
{ echo "foo"; >&2 echo "bar"; } >/dev/null 2>&1

Process Substitution

Stdout as file-input

cat <(ls -l)
diff -y <(ls) <(ls -a)

here-string

Any string as stdin:

read -r <<< Hello
read -r <<< "$PATH"
read -r <<< "$(ls -l)"

here-document

Multiline string as stdin:

cat <<EOF
USER: ${USER}
HOME: ${HOME}
EOF

Without substitution:

cat <<'marker'
USER: ${USER}
HOME: ${HOME}
marker

Suppress leading tabs:

cat <<-EOF
	Line 1
	Line 2
EOF

Can be used with the null command to "comment out" blocks of code:

: <<COMMENT
echo "this will not run"
touch file
COMMENT