ARG_MAX
| Shells
| portability
| permissions
| UUOC
| ancient
| -
| ../Various
| HOME
$() vs )
| IFS
| using siginfo
| nanosleep
| line charset
| locale
2009-02-26 (see recent changes)
( cmd; cat file; cmd ) | cmdThis was for example useful for me on a Redhat Linux, where man(1) otherwise annoyingly
( echo -e ".pl 1100i"; cat "$1"; echo ".pl \\n(nlu+10\n" ) | tbl | nroff -man
cat > textfilethen typing plain text and closing with EOF (aka <ctrl-d>) was the quickest way for me to create text files on systems running in some rescue mode.
tar cf - . | ssh remotehost 'cat > big.tar'
split(1)
cat xa* > c
...very related: a filter only reads from STDIN
and you want to hand over several arguments.
A redirection doesn't allow this. Examples: using tr,
or converting tai64-timestamps in qmail-logs:
cat log1 log2 log3 | tr '[:upper:]' '[:lower:]'
cat log1 log2 log3 | tai64nlocal
...related: falling back to STDIN if no arguments were supplied:
(Chris F.A. Johnson, Richard L. Hamilton in c.u.s)
cat ${1+"$@"} | cmdFootnote to the last example: if arguments with leading dashes are possible,
--" (end of options) after "cat" helps. It's not strictly portable.
cat known_data | new_filter | ...
command | new_filter | ...
and compare with these (avoiding the cat):
new_filter < known_data | ...
command | new_filter | ...
Alternatively, you can put the redirection at the left side (but only with simple commands,
not with compound commands like loops, grouped lists and subshells)
< known_data new_filter | ...
command | new_filter | ...
On contrast to the other items, this is only a matter of taste.
cat<<EOF cat<<EOF|cmd $VARIABLE $VARIABLE EOF EOFBe careful if you put the above into a function:
(Reminder from Janis Papanagnou for how to embed a here-doc in a pipeline added, for those who haven't seen it, yet)
cmd `cat file` for i in `cat file`; do ...
However, don't forget about possible problems with characters from IFS,
most likely spaces.
Janis Papanagnou reminds that a non-portable (even non-standard) way without cat,
supported by some modern shells is
cmd $(< file )
cat file | cmdA workaround for commands which have been compiled without largefile support but accept a pipe, e.g. compressing utilities.
select()
select can be applied to pipes, sockets and ttys but not to regular files.
A program pathologically using select could be used again.
(academic, but well spotted by Dan Mercer in comp.unix.programmer)
more(1)/less(1)/pg(1))
usually test stdout/stderr for an interactive TTY,
ps(1) to print long lines (complete arguments)
script_or_command | cat
case x in a) filter=cmd;; b) filter=cat;; esac
If the last command in a pipeline would return an undesirable exit status, force the pipeline to return success with
cmd1 | cmd2 | cat
while an alternative is "{ cmd1 | cmd2 || true; }"
cat -v
However, this is controversial from the viewpoint of the
unix tradition "do one thing and do it well".
See the paper
"Program Design in the Unix Environment" by Rob Pike and Brian Kernighan
for a rant about this.
<http://www.in-ulm.de/~mascheck/various/uuoc/>