LWN.net Logo

Shell Scripts

Shell Scripts

Posted May 5, 2010 14:28 UTC (Wed) by nye (guest, #51576)
In reply to: Shell Scripts by paulj
Parent article: Poettering: Rethinking PID 1

This seems fairly self-evident to me.

Shell syntax is unique, and not in a good way. Writing shell scripts is *hard*, and *nasty* unless you do it all day, every day. It's nothing like any other language you might use, and there are innumerable gotchas that can be hard to spot. The result is that, more so than most languages, shell scripts tend to be written using largely a mixture of copy/paste cargo-culting, and experimentation until it seems to work for you.

[The last time I wrote a shell script I was bitten by the fact that I *still* can never remember when I should be doubling up square brackets, or when I'm supposed to use whitespace within them or not, plus the fact that the exclamation mark can be used as negation in some places, but in others will expand to the previous command. At least I've now internalised the knowledge that zero evaluates to true, bizarrely, but it still makes test expressions harder to read. Lets not even go into the weird way in which arguments are passed around.]


(Log in to post comments)

Shell Scripts

Posted May 5, 2010 15:42 UTC (Wed) by paulj (subscriber, #341) [Link]

Well, programming is just hard, full stop. I don't personally think shell is any worse or better than other languages. It does though have the advantage of long-standing ubiquity. Now, you could raise the barrier by changing the language, so that those without the training can't hack at things by copying, but then you also suffer from the fact that people won't be able to get things done by hacking at things.

Judging by Lennart's blog though, he does not intend to get rid of shell. Rather he wants to move common functionality out of the myriad shell scripts and up into the init system. Potentially leaving just a per-service configuration file to be filled in (for many services anyway)...

Shell Scripts

Posted May 5, 2010 22:12 UTC (Wed) by HelloWorld (subscriber, #56129) [Link]

> I don't personally think shell is any worse or better than other languages.
That only proves that you don't know it very well :)

Shell Scripts

Posted May 6, 2010 2:12 UTC (Thu) by AndreE (subscriber, #60148) [Link]

Programming should be hard for the right reasons. Having an inconsistent and pinicky syntax in your language doesn't seem like right reason.

Shell Scripts

Posted May 6, 2010 12:39 UTC (Thu) by nescafe (subscriber, #45063) [Link]

If your script starts with #!/bin/sh, you never use [[ ]] syntax. [[ ]] is not POSIX. If you are targeting POSIX, test your script with dash or posh.

If your shell script starts with #!/bin/bash or #!/bin/ksh93, you should use [[ ]] syntax -- it is in general faster and more flexible than the test command (which is what you are actually using when you use single square brackets).

If there is any doubt, read the fine man page for the shell you are targeting. The bash man page is quite thorough -- I recommend having it open as a reference when coding in bash.

Shell Scripts

Posted May 6, 2010 15:41 UTC (Thu) by nye (guest, #51576) [Link]

>If your shell script starts with #!/bin/bash or #!/bin/ksh93, you should use [[ ]] syntax -- it is in general faster and more flexible than the test command (which is what you are actually using when you use single square brackets).

Still don't know what the difference is though. The bash manual is not especially enlightening in this area (NB. if I really cared I would Google it, of course)

>If there is any doubt, read the fine man page for the shell you are targeting. The bash man page is quite thorough -- I recommend having it open as a reference when coding in bash.

Thorough, no doubt. Not especially useful however - unless you know precisely what to search for, finding a needle in that haystack is almost never worth the time when Google is available. The dash manpage is slightly less unwieldy, as one might expect.

Still, this seems to prove the point rather well - what other languages require you to keep the documentation open constantly due to unintuitive and inconsistent choices in basic syntax?

I'm not saying that shell scripting is absolutely intolerably awful, just that it tends to make easy things hard. For scripts that don't need specific features like setting traps or atomic lockfile creation, it's much less painful to use, say, Lua. Or Python if speed is of no account.

Shell Scripts

Posted May 6, 2010 17:12 UTC (Thu) by nescafe (subscriber, #45063) [Link]

>>If your shell script starts with #!/bin/bash or #!/bin/ksh93, you should use [[ ]] syntax -- it is in general faster and more flexible than the test command (which is what you are actually using when you use single square brackets).

>Still don't know what the difference is though. The bash manual is not especially enlightening in this area (NB. if I really cared I would Google it, of course)

[[ ]] is syntax, [ ] and test are (builtin) commands. [[ ]] does not perform word splitting or pathname expansion on its arguments (no more quoting "just because", and you can use bash's builtin regex support (and get rid alot of trivial uses of sed and awk). The manpage goes into more detail.

>Still, this seems to prove the point rather well - what other languages require you to keep the documentation open constantly due to unintuitive and inconsistent choices in basic syntax?

All of them.

Copyright © 2012, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds