Redirection of input and output
Input and output, to and from commands, may be taken from the console (user input), a file, a variable and directly
from other programs. What is possible varies on how you are using the command and what it supports, see the man pages
for each command for further info. However, in general the following options are possible:
In the following examples the pwd program is used to provide output as it
always generates a line of text, the ls command is used where it is more useful to have multiple lines for the example and
other commands are used when it assists the explanation.
- Input from a file
'mycommand myfile' is the standard syntax for a command taking a file, alternatively 'mycommand < myfile' can also be used
- Input from a variable
Variable substitution is supported so using the $ syntax provides access to the contents of variables, e.g.
'echo $myvar'
- Input/Output between commands
'ls | more' passes the output of ls to the more command which then uses it as the input stream which it will process
- Output to a file
'ls > myfile' (to create the file) and 'ls >> myfile' (to append to the file)
- Output to a variable
'myvar=`pwd`' or 'myvar=$(pwd)'