So just to see if I could, I wrote a version of cat using pure bash. Pure bash is a bash script which uses nothing but bash builtins to accomplish it’s goal. To determine if a particular command is a builtin, you can use the command type -t “command” (the command type, is itself a builtin). Some notable commands which are builtins include echo, read, exec, return. Some notable commands which are not builtins include cat and grep. As follows is my implementation of cat in pure bash.

 1 #!/bin/bash
 2 INPUTS=( "${@:-"-"}" )
 3 for i in "${INPUTS[@]}"; do
 4 	if [[ "$i" != "-" ]]; then
 5 		exec 3< "$i" || exit 1
 6 	else
 7 		exec 3<&0
 8 	fi
 9 	while read -ru 3; do
10 		echo -E "$REPLY"
11 	done
12 done

Now, keep reading if you want a small lesson in advanced bash. I’ll go line by line to explain what this is doing.


Line 1 is the shebang

INPUTS=( "${@:-"-"}" )

Line 2 assigns the array variable INPUTS either the arguments provided on the command line if they exist, or the single character -. The way this happens is as follows: [email protected] is the variable to reference the positional parameters (the arguments to your program). If you have not heard of $*, read this. The way I reference the positional parameters is like ${@}. That’s because the brackets allow me to add a default value to the variable. A default value is the value that the variable will seem to have if the variable is not set. The way to use a default value is with the :-, like so: ${@:-"hello"}. So if [email protected] is not set, it will seem to have the value "hello". You will then notice that is all enclosed in (). That makes an array out of the positional parameters (the first argument to the program becomes the first element in the array, the second argument becomes the second element, etc.).

for i in "${INPUTS[@]}"; do

Line 3 begins a for loop which will assign to i each value stored in the array INPUTS which was discussed earlier. The @ index used is the same for arrays as [email protected] is for the positional parameters.

Maybe I’ll explain more when I’m less lazy.

blog comments powered by Disqus