Input and output for a command and basic redirection.

Commands may either use :

  Input
    keyboard, 
    arguments as input data, 
    arguments that Id files as data sources.
    piping
    redirection 

  Output 
    screen
    arguments that Id files as data storage.
    piping
    redirection 

  Which of these are allowable will depend on the command.
     
Redirection
4 types.

  • < - redirect input from file. Only one filename allowed. Filename must follow < But command can precede or follow redirection/file-name pair. < found sort sort < found are both valid.
  • > - redirect output to file. Only one filename allowed. Filename must follow > But command can precede or follow redirection/file-name pair. * The tee command used with piping allow redirection to a second file. ls -l | tee fun1 > fn2 More commonly used between pipes. Redirection will overwrite an existing file of same name. Unless noclobber set. set -o noclobber To turn noclobber off set +o noclobber noclobber only affects redirection.
  • >> - appending output to file. This will append output to end of data in named file. Creates file if not yet in existence. In bash, noclobber has no effect. In some other shells, won't work if file non-existent and noclobber set.
  • << - here redirection. Allows command to take standard input until EOF flag word[s] sent. cat << "done" > stuff Flag word[s] has to be on line by itself in input. This will take anything (almost) typed on command line until the word done typed in. The flag word[s] must appear on a line by themselves. In bash, [ctrl]d (EOF) will also stop input but you will get a complaint. If you quote the EOF word[s], you can a multi-word EOF marker. The marker does not need to be quote. cat << "The end" > stuff Most useful in shell script where you want to include inline data instead of a second file for needed text.
    The default source/destination of a command's I/O determined by command. Some will assume specific source or target. Some require a named file. And some can or have to use redirection (file) or piping (another command) to determine source and/or target. Example of some commands and their default i/o.
  • ls Input : list of filenames or assumes current directory to be listed. Ignores input redirection. Output : standard out (screen). Use redirection to save in named regular file. Or pipe if input source for another command.
  • cat Input : filename[s] or keyboard (std-in) if no filename[s] provide. Takes input until EOF, if keyboard, this will be <ctrl>D. Use << (here) to define EOF string for input. Output : standard out (screen) Use redirection to named regular file. Or pipe if input source for another command.
  • sort Input : filename[s], If none given, will use standard input until EOF(<ctrl>D). Can use standard input redirection or here or piping. Output : standard out (screen) or redirection to named regular file. Also has -o option to specify output target which can also be one of the input files.
  • tr Input : standard in (keyboard) Must use redirection or piping of input source other than standard input. Output : standard out (screen). Use redirection or piping to route output to something other than the screen. All other arguments are either options or the character translation values.
  • ps Input : asks scheduler for what is running. Recognizes only options on command line for input. Output : standard out (screen). Use redirection or piping to route output to something other than the screen.
    Alternative I/O Standard input interface file descriptor is 0. std-in Standard output interface file descriptor is 1. std-out Standard error interface file descriptor is 2. std-err Command can generate both standard out and standard err. But each of these is sent to its own interface IDed by the descriptor. Which by default is the screen. When using redirection and piping, possible to separate std-out from std-err. To capture both, redirect descriptors to desired files : cmd > good 2> bad Combining both in a single file, use an & to connect : cmd > result 2>&1 Puts both standard output and errors in same file. The order in the file will not be predictable, it will be command specific.