[Top][All Lists]

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

Re: How to detect bash?

From: mwoehlke
Subject: Re: How to detect bash?
Date: Tue, 10 Oct 2006 18:14:22 -0500
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv: Gecko/20060909 Thunderbird/ Mnenhy/

Stephane Chazelas wrote:
On Tue, Oct 10, 2006 at 05:12:07PM -0500, mwoehlke wrote:
Dave Rutherford wrote:
On 10/10/06, mwoehlke <address@hidden> wrote:
Anyone have any clever, VERY reliable tricks for detecting if the
current shell is bash?
Well, I don't know if it's clever, but how about:
Oh, my... Where do I *start*?

$ if [ "${SHELL//*/bash}" = "bash" ]; then echo y; fi
$ echo $SHELL
$ echo $BASH
$ foo
bash: foo: command not found

There is *ABSOLUTELY* no guarantee that $SHELL correctly points to bash, or that $SHELL is even remotely correct for that matter. This is /worse/ than relying on $BASH.

But it does bring up an interesting possibility:
[ "`/dev/null 2>&1`" = "bash: /dev/null: Permission denied" ]

You quest looks a bit pointless to me. What prevents the user to
edit a copy of your script to remove the check anyway?

$ zsh -c 'echo "`/dev/null 2>&1`"' bash
bash: /dev/null: Permission denied

$ zsh
$ ARGV0=bash ash -c 'echo "`/dev/null 2>&1`"; echo $BASH'
bash: /dev/null: Permission denied

Eh? I get:

$ zsh -c 'echo "`/dev/null 2>&1`"' bash
zsh:1: permission denied: /dev/null

$ ARGV0=bash ash -c 'echo "`/dev/null 2>&1`"; echo $BASH'
/dev/null: permission denied

So neither of your counter-examples is working for me (although both look like they *should*; go figure). But since you didn't counter BASH_SUBSHELL (and since I'm too lazy to change it now) I guess I'll stick with that. :-)

Clearly the exact output depends on the version/flavor of the shell, meaning that relying on verbatim output like this might not work (I didn't test against any 2.x bash's). Whereas BASH_SUBSHELL ought to be safer w.r.t. false negatives.

This is a fascinating discussion though; thanks (to Dave R., also) for the input!

$ echo '/dev/null(){echo "bash: /dev/null: Permission denied"}' \
  >> ~/.zshenv
$ zsh -c 'echo "`/dev/null 2>&1`"'
bash: /dev/null: Permission denied

And whatever check you do can be worked around in a way or

True, but the main point of the exercise is to go with a check that's unlikely to be worked around "by accident". If someone intentionally circumvents the check (and you're right, editing the script would be easy), well then they deserve whatever happens. But I *am* paranoid enough to not trust that $BASH is never set - other than by bash - for some reason. Or that it hasn't been *unset* (since that seems to kill it forever), because we have 'clean environment' scripts that would do this sort of thing.

KDE: Desktop Excellence

reply via email to

[Prev in Thread] Current Thread [Next in Thread]