Input redirection: <
The <
(less-than) symbol lets you control where a process gets its input from. For example, you’re used to giving input to Bash with your keyboard, one command at a time. Let’s try giving bash input from a file, instead!
Assume I have a file named commands.txt
with the following content (I’m using cat
here to print out my example file):
# cat commands.txt
pwd
echo $SHELL
cd /tmp
pwd
These are valid shell commands, as far as Bash is concerned, so I’m going to launch a new Bash process and use this file as standard input:
# bash < commands.txt
/tmp/gopsinspect
hello there, friends
/bin/bash
/tmp
Instead of prompting me for input and waiting until I give it, Bash reads and executes one line at a time: it reads input from the file until it comes across a newline (\n
) character, and just as if you’d hit the RETURN key, it executes the command.
Standard output is still going back to our terminal. Let’s change that.
...