Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I found out that when I want to create some shell script or advanced command it always made me trouble to remember the names of the commands and their switches.


tr -d '\n' < file | wc -c

Though in programming languages I could write something like

len(replace(str(file), "\n", ""))

Which is more memorable and there is less probability to search in man pages and it's more understandable for others.

So, why shell scripting is for so many years in this cryptic form? Is their an effort to change the syntax to be clear what the code does without a lot of man pages studying? Are there some advantages of this form? How can we cope with the disadvantages to get most from the shell scripting?

share|improve this question

closed as not constructive by manatwork, jw013, Mat, sr_, l0b0 Apr 12 '12 at 13:55

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.If this question can be reworded to fit the rules in the help center, please edit the question.

Good question. But realize that you're free to use your favorite programming language as a shell if you want. (Provided it comes with an interpreter.) – Stéphane Gimenez Apr 12 '12 at 11:58
The shell is making intensive use of piping, a concept that many other programming languages are missing. So, the question on the other hand is, why do you prefer your form? There are other concepts that might fit as well. A question "Why does the shell not behave like perl/python/fortran/c++/scheme/younameit is leading nowhere. – ddeimeke Apr 12 '12 at 12:02
@ddeimeke I prefer it because it's more memorizable and I can see at the first sight what the snippet is doing. – xralf Apr 12 '12 at 13:09
Keep in mind that what might be crystal-clear to you may be cryptic for someone else. For example, what does str() do above? Is it supposed to read the contents of the filename stored in variable file? If so, it's not a well-named function. – glenn jackman Apr 12 '12 at 13:14
@xralf, well, that's what man pages are for. – glenn jackman Apr 12 '12 at 13:37

How many programming languages do you know that let you open a file for reading, call a library function that consumes the contents of that file, stream the output of that function into another one, return the output of the second function to the user, return an exit status, and then clean up everything afterwards, all in one very short line?

The example you give leaves out a lot of the support gubbins most languages need.

How many programming languages have library functions that accept arbitrary numbers of inputs, work in a variety of modes, and have a simple interface?

printf, maybe, but most libraries end up have a whole range of functions with different names and different parameters that you have to learn. I don't see the difference.

Also, most languages would call the functions in series. Shell scripts run them in parallel, and can stream arbitrary quantities of data through the pipeline without consuming much memory, and (if the data source is the keyboard, or a network socket, or something) it's all done with lazy evaluation, as and when the data arrives. Does your favourite language do that?

There are many languages that can do the job more quickly, or more efficiently, or can cruch numbers more accurately, but don't underestimate the power of the shell script!

As for the syntax? Well, it works. You'd have to learn it no matter now it looked. What more do you want?

share|improve this answer
“How many programming languages have library functions that accept arbitrary numbers of inputs?” → The question is why isn't there such libraries. (And maybe there are.) – Stéphane Gimenez Apr 12 '12 at 12:59
I wonder why many people haven't used something like Python long time ago for shell scripting. – xralf Apr 12 '12 at 13:13
@xralf simply because Python is not installed on every system ... – ddeimeke Apr 12 '12 at 13:31

Try pbs -- combining command line tools with Python -- for example like this:

# sort this directory by biggest file
print sort(du(glob("*"), "-sb"), "-rn")

# print the number of folders and files in /etc
print wc(ls("/etc", "-1"), "-l")
share|improve this answer
This seems to be the worse of both worlds, when used as a shell… (However, it might be useful to do some shell wrapping when there is no other choice, but that not related to the question.) – Stéphane Gimenez Apr 12 '12 at 13:00
Interesting answer, but there are still a lot of switches. For me it's absolutely unmemorizable for years because various programs use the same switch for different things, so I always have to grep the man page. – xralf Apr 12 '12 at 13:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.