`cat` as a shell builtin

Recently i blogged about how to get a process list if you aren’t able to fork any processes . Also a process list is allways handy, i allways find cat a very usefull tool too. But as you might have noticed cat is not a shell builtin. So everytime cat is called a new process needs to be forked. In case you can’t fork processes it’s a problem, and even if you can fork them you may save resources by “creating” a shell builtin cat.
So lets do this,
open your .profile for usage with your user only or /etc/profile for global usage (You may also use your .bashrc or .zshrc instead).
Append the following lines to the file:

#pseudo bash builtin cat function
function cat { echo “$(< $1)" }

Siehe Kommentar :)

function cat {
  if [ $# -eq 1 -a "${1:0:1}" != "-" ]
    exec 3< "${1:-/dev/stdin}"
    while read -r -u3 line
      echo -E ''"${line}";
    echo -nE ''"${line}";
    exec 3<&-
     /bin/cat "$@"

Voila, if you start a new loginshell the cat should refer to a shell function and not to a binary.
(to test this simply type type cat result should be something like cat is a shell function )

This entry was posted in Allgemein, Technik and tagged , , . Bookmark the permalink.

2 Responses to `cat` as a shell builtin

Leave a Reply

Your email address will not be published. Required fields are marked *