Archive:
Subtopics:
Comments disabled |
Sun, 30 Jul 2023
The shell and its crappy handling of whitespace
I'm about thirty-five years into Unix shell programming now, and I continue to despise it. The shell's treatment of whitespace is a constant problem. The fact that
doesn't work is a constant pain. The problem here is that if one of
the filenames is
and fail, saying
or worse there is a file named To make it work properly you have to say
with the quotes around the Now suppose I have a command that strips off the suffix from a filename. For example,
simply prints
Ha ha, no,some of the files might have spaces in their names. I have to write:
Ha ha, no, fooled you, the output of
At this point it's almost worth breaking out a real language and using something like this:
I think what bugs me most about this problem in the shell is that it's
so uncharacteristic if the Bell Labs people to have made such an
unforced error. They got so many things right, why not this? It's
not even a hard choice! 99% of the time you don't want your strings
implicitly split on spaces, why would you? And the shell doesn't have
this behavior for any other sort of special character. If you have a
file named Even if it was a simple or reasonable choice to make in the beginning,
at some point around 1979 Steve Bourne had a clear opportunity to
realize he had made a mistake. He introduced
and then run it:
except that doesn't work because
Oh, I see what went wrong, it thinks it got three arguments, instead
of two, because the elements of
No, the quotes disabled all the splitting so that now I got one argument that happens to contain two spaces. This cannot be made to work. You have to fix the shell itself. Having realized that
and
so that inside of I deeply regret that, at the moment that Steve Bourne coded up this weird special case, he didn't instead stop and think that maybe something deeper was wrong. But he didn't and here we are. Larry Wall once said something about how too many programmers have a problem, think of a simple solution, and implement the solution, and what they really need to be doing is thinking of three solutions and then choosing the best one. I sure wish that had happened here. Anyway, having to use quotes everywhere is a pain, but usually it works around the whitespace problems, and it is not much worse than a million other things we have to do to make our programs work in this programming language hell of our own making. But sometimes this isn't an adequate solution. One of my favorite trivial programs is called
Many programs stick files into that directory, often copied from the
web or from my phone, and often with long and difficult names like
or
or
except ha ha, no I don't, because none of those work reliably, they all fail if the difficult filename happens to contain spaces, as it often does. Instead I need to type
which in a command so short and throwaway is a noticeable cost, a cost extorted by the shell in return for nothing. And every time I do it I am angry with Steve Bourne all over again. There is really no good way out in general. For
The actual script is somewhat more reliable, and is written in Python, because shell programming sucks. [ Addendum 20230731: Drew DeVault has written a reply article about
how the [ Addendum 20230806: Chris Siebenmann also discusses [Other articles in category /Unix] permanent link |