Bash Input and Output

Reading input and redirecting output

Read Input

read var # read input into variable
read -p prompt var # read with prompt
read -s var # silent input (passwords)
read -n 1 var # read single character
read -t 5 var # timeout after 5 seconds

Output Redirection

command > file # redirect stdout to file (overwrite)
command >> file # append stdout to file
command 2> file # redirect stderr to file
command &> file # redirect both stdout and stderr
command > /dev/null # discard output

Input Redirection

command < file # read input from file
command <<< string # here-string

Here Document

cat << EOF
line 1
line 2
EOF

Pipes

command1 | command2 # pipe output to input
command1 |& command2 # pipe stdout and stderr