A Study in Bash Shell : Output

A Study in Bash Shell : Output

$ dosomething < inputfile > outputfile

Writing Output to the Terminal/Window

$ echo Please wait.
Please wait.
$ echo "this        was very widely spaced"
this        was very widely spaced

Writing Output with More Formatting Control

$ printf '%s = %d\n' Lines $LINES
Lines = 24

Writing Output Without the Newline

$ printf "%s %s" next prompt
next prompt$
$ echo -n prompt
prompt$

Saving Output from a Command

$ echo fill it up
fill it up
$ echo fill it up > file.txt

Saving Output to Other Files

$ echo some more data > /tmp/echo.out

Saving Output from the ls Command

$ ls
a.out cong.txt def.conf file.txt more.txt zebra.list
$
$ ls > /tmp/save.out
$ cat /tmp/save.out
a.out
cong.txt
def.conf
file.txt
more.txt
zebra.list

Sending Both Output and Error Messages to Different Files

$ myprogram 1> messages.out 2> message.err

Sending Both Output and Error Messages to the Same File

$ both >& outfile

Appending Rather Than Clobbering Output

$ ls > /tmp/ls.out
$ cd ../elsewhere
$ ls >> /tmp/ls.out
$ cd ../anotherdir
$ ls >> /tmp.ls.out

Using Just the Beginning or End of a File

$ tail -2 lines
$ head lines

Saving or Grouping Output from Several Commands

$ ( pwd; ls; cd ../elsewhere; pwd; ls; ) > /tmp/all.out

Connecting Two Programs by Using Output As Input

$ cat one.file another.file > /tmp/cat.out
$ sort < /tmp/cat.out

Connecting Two Programs by Using Output As Arguments

$ rm $(find . -name '*.class')

Keeping Files Safe from Accidental Overwriting

$ set +o noclobber
$ echo something > my.file
$ echo some more > my.file
$ set -o noclobber
$ echo something > my.file
bash: my.file: cannot overwrite existing file
$ echo some more >> my.file

Clobbering a File on Purpose

$ echo something > my.file
$ set -o noclobber
$ echo some more >| my.file
$ cat my.file
some more
$ echo once again > my.file
bash: my.file: cannot overwrite existing file