JP Vossen on 5 Apr 2011 00:35:58 -0700

[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] Bash 101 resources

Date: Sun, 3 Apr 2011 19:43:30 -0400
From: Mag Gam<>
Subject: Re: [PLUG] Bash 101 resources

Very nice slide show.

Thanks.  I hope it's useful.

One comment about the if statements. Should they be double brackets
versus single?

It depends...

#correct way?
if [[ -d /foo ]]; then

I'd say "new" way.

instead of
if [ -d /foo ]; then

See my page and pick "The bash Reference Guide" link for the version of bash you are using. I picked 4.2 just to have the latest, but this stuff hasn't changed in a while. See the "NEWS" and/or "CHANGES" links if you care what changed when.


(Hint: search for 'test' when looking for '[', as there are tons of '[' characters in the document.)

"Conditional expressions are used by the [[ compound command and the test and [ builtin commands. [...]"

So according to Chet, '[[' is a compound command but '[' is a builtin.

What does that mean to you?  I dunno.

My *personal* and admittedly imprecise rule-of-thumb is basically this. I think of '[[' as the "new" one that can do better pattern matching and regular expressions. But I mostly use the older '[' out of habit unless I'm doing something that really needs the new one. And come to think of it, '[[' is probably less portable. Yup. According to, dash doesn't have '[['. If you don't think you care about dash, you're wrong if you use Debian or Ubuntu... See the link as to why. (Hint: /bin/sh == dash in newer Ubuntu & Debian, and bash != Bourne != dash...)

Getting back to the simple example above, except for portability I don't think it really matters if you use '[' or '[['. I'd use '[' out of habit and because it's less typing and maybe looks a tiny bit less cluttered.

Actually, for something that simple I'd probably write:
	[ -d foo ] && echo 'exists'

JP Vossen, CISSP            |:::======|
My Account, My Opinions     |=========|
"Microsoft Tax" = the additional hardware & yearly fees for the add-on
software required to protect Windows from its own poorly designed and
implemented self, while the overhead incidentally flattens Moore's Law.
Philadelphia Linux Users Group         --
Announcements -
General Discussion  --