5

Possible Duplicate:
How to test what shell I am using in a terminal?

I want to detect which interpreter a shell script is using. For example the following script:

#!/bin/bash

issue_interpreter_name()

Should reveal the interpreter:

bash
Ingo
  • 51
  • @jw013 more or less the same question but jippie's answer is better then the ones in the linked question :-) – Matteo May 03 '12 at 18:52

2 Answers2

12
#!/bin/sh  
ps h -p $$ -o args='' | cut -f1 -d' '
  • ps process list
  • h do not print column headers
  • -p <PID> list only process id PID
  • $$ replaced by the shell with current PID
  • -o args print the command line, no other information
  • cut cut the output into parts
  • -f1 print only the first field
  • -d' ' use a space as a field separator

    $ ./testje

    /bin/sh

jippie
  • 14,086
  • On Mac OX X I get an additional line with the header ARGS. With -o args='' you can remove the header – Matteo May 03 '12 at 18:50
  • 1
    Verified Matteo's comment on Linux (ubuntu 12.04) and works as designed. Updated my answer. – jippie May 03 '12 at 18:55
-1

If it's a local user, the 7th field of /etc/passwd is their shell. Are you looking for the currently preferred shell?