ARG_MAX
| Shells
| whatshell
| portability
| permissions
| UUOC
| ancient
| -
| ../Various
| HOME
$@
"
| echo/printf
| set -e
| test
| tty defs
| tty chars
| $()
vs )
| IFS
| using siginfo
| nanosleep
| line charset
| locale
(No "cat content" media here. Believe me, that I was more than attempted.)
2022-01-03 (see recent changes)
( cmd; cat file; cmd ) | cmdA generic method to mix both fixed/existing and dynamic output.
Rather special example: This was useful for me on a Redhat Linux, where man(1) otherwise annoyingly
would intersperse the output with headers and footers for a print version, which is rarely used, though...
I tried:
( echo -e ".pl 1100i"; cat "$1"; echo ".pl \\n(nlu+10\n" ) | tbl | nroff -man
Another example: If your bc(1) cannot be made to default to some decimal digits (Tom Rodman, in comp.unix.shell)
_bc() { { echo "scale=4"; cat;}|bc -l;}
or bc() { { echo "scale=4"; cat;}|command bc -l;} if you like
Another example via hackernews: a commandline "webserver":
while :; do nc -l -p 8888 -c "printf 'HTTP/1.0 200 OK\r\n\r\n'; cat file"; echo request answered; done
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 -- "$@" | cmd cat ${1+"$@"} | cmdThe double dash safes from confusing cat with file arguments with leading dashes.
cat known_data | new_filter | ... command | new_filter | ...and compare with this, which avoids
cat
but means more typing:
new_filter < known_data | ... command | new_filter | ...
This is just a matter of taste.
But I frequently use the variant with cat
exactly for this reason of convenience.
PS: In some cases, you could 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 | ...
if [ -f remote-file-copy ] then cat remote-file-copy else curl address | tee remote-file-copy fi | further-pipeline-processing
cat<<EOF cat<<EOF|cmd $VARIABLE $VARIABLE EOF EOFBe careful if you put the above into a function: Traditional bourne shells and ksh up to 88g can have
(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
if condition then filter() { cat - ;} else filter() { some-filter-pipeline ;} fi
or, depending on the constraints, you might decide just in time,
filter() { if condition then cat - else some-filter-pipeline fi;}This item originally saved the command to a variable, which would be unecessary difficult to use later.
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; }
"
A process can't be removed from a pipeline transparently.
But it could replace itself with cat(1)
as transparent connector.
exec("/bin/cat");Mentioned by Warren Toomey on the THUS mailing list
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
(local copy) for a rant about this. Also known as "cat -v considered harmful".
<http://www.in-ulm.de/~mascheck/various/uuoc/>