Aesthetics of shell scripting

Here is the problem; you need to print a single line filled with dashes. Will you just echo 80 dashes or write a beautiful loop for it?

The ease solution

$ echo "--------------------------------------------------------------------------------"

The aesthetic solution

$ n=0 ; while [ $n -lt 80 ] ; do printf "-" ; n=$(($n+1)) ; done ; echo

Both give the same result, but the easy solution is faster. Result from the machine where I am working on:

Test Easy way Aesthetics way
Time to execute: 0.000 seconds 0.039 seconds
Bytes on disk 88 70
Complexity level 1 7
System calls 37 657
lines printed in 1 minute 1471371 18318

The numbers show that simplicity is more efficient.


This is the first time I've

This is the first time I've known this idea. This will be a useful information now. - Marla Ahlgrimm

Personally I can't say typing

Personally I can't say typing 80 "-"s is easy.

I would go with a for loop and remove some of the tests/addition to speed it up.

for i in `seq 80` ; do echo -n - ; done ; echo

Or how about this one?

Or how about this one? (Learnt it after writing this article:

for i in {1..80} ; do echo -n - ; done ; echo

in bash: for((i=0; i < 80;

in bash:
for((i=0; i < 80; i++)); do echo -n -; done ; echo

+5 insightful. Simplicity,

+5 insightful. Simplicity, elegance, beauty, and efficiency are intrinsically linked. To quote Buckminster Fuller:
'When I am working on a problem I never think about beauty. I only think about how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong.'

hmmm, i some times have to

hmmm, i some times have to change code (even mine) and it read faster, easier and is mor understandable, which results in less errors when recoding.

KISS (keep it simple st.... )

About Consultancy Articles Contact

References Red Hat Certified Architect By Robert de Bock Robert de Bock
Curriculum Vitae By Fred Clausen +31 6 14 39 58 72
By Nelson Manning [email protected]