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

Re: how to determine the interpreter



On Fri, 26 Nov 2010 10:11:07 -0500 (EST), Joao Ferreira wrote:
> 
> considering sh, bash and csh, can I somehow, inside a script, determine
> if I'm being run with any of these 3 shells...
> 
> I need to determine wich interpreter is running me...

The login shell is set in /etc/passwd on a user-by-user basis.
For example, consider this entry:

   steve:x:1000:1000:Stephen Powell,,,:/home/steve:/bin/bash

This shows that when user "steve" logs in, his login shell will be
bash.  For a shell script, the script itself can force a particular
shell to be used by a special comment in line 1:

   #!/bin/bash

in line 1 will force bash to be used.  What most shell scripts do
is put

   #!/bin/sh

in line 1, and this causes the default shell on the system to be
used.  The default shell can be determined by

   ls -Al /bin/sh

For example, on my system the output is

   lrwxrwxrwx 1 root root 4 Nov 13 11:20 /bin/sh -> dash

which indicates that the default shell for scripts on my system is
dash.  If a script wants to know which shell is running it, the
variable $0 might work.  For example,

   echo $0

might produce

   -bash

If there is a leading hyphen, it is the login shell; otherwise it
is not the login shell.  A shell script should never see a
leading hyphen.

-- 
  .''`.     Stephen Powell    
 : :'  :
 `. `'`
   `-


Reply to: