Page 1 of 3 123 LastLast
Results 1 to 10 of 22

Thread: Find out EXACTLY what shell is being used

  1. #1
    Join Date
    Jan 2009
    Location
    Michigan
    Beans
    87
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Question Find out EXACTLY what shell is being used

    Hi All:

    So I was browsing the web today and came across a post where an individual was interested in finding out what shell they are using. There were several responses: echo $0, ps -p $$, echo $SHELL, however, none of them hit the mark (IMHO). If you were, for example, using Ubuntu 9.04 /bin/sh is a symlink to /bin/dash. If you ran echo $0 or ps -p $$ while using /bin/sh (/bin/dash) they would both report /bin/sh as the shell, not /bin/dash (the real interpretor). Furthermore if you ran echo $SHELL while using /bin/sh it would report /bin/bash (the login shell).

    I wrote a little snippet of code to try to figure out what shell is actually being used, however, have hit a little bit of a thunker.

    CODE:
    var=`ps -p $$ | sed 's/[0-9]*//g' | sed -re 's/^.+\///g' | sed -re 's/://g' | sed -re 's/[A-Z]*//g' | sed -re 's/ //g'`
    echo $var | grep "/" > /dev/null
    if ! [ $? -eq 0 ]; then fullvar=`echo "/bin/"$var | sed -re 's/ //g'`; fi
    readlink $fullvar > /dev/null
    if ! [ $? -eq 0 ]
    then echo $fullvar" is not a link. It must be the REAL shell interpretor."
    else
    fullvar=`readlink -f $fullvar` > /dev/null
    var=`echo $var | sed -re 's/ //g'`
    echo $fullvar" is the REAL shell interpretor, not /bin/"$var
    fi

    Obviously the first thing that stands out is how sloppy it is.
    The second thing that stands out is that if the interpretor is outside of /bin the output would be wrong.
    The third is using multiple symlinks to "fool" the script.

    Any ideas on how to "fix" it?

    Or better yet is there a one liner to figure out the current shell interpretor?

    Thanks!
    Last edited by for.i.am.root; October 26th, 2010 at 01:20 AM. Reason: Grammar & Spelling

  2. #2
    Join Date
    Jun 2007
    Beans
    192
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Find out EXACTLY what shell is being used

    Nothing against Mr "root",but since we're in the "Absolute Beginner Talk" Forum. I'd like to reiterate the warning not to run code/scripts unless you know what it does.

    Again, let me say this is just a generic warning, not any implication against root.

    EDIT: @ for.i.am.root, If I can make a suggestion, you might get better answers in the "Programming Talk" forum. (I think they cover scripting as well...)
    http://ubuntuforums.org/forumdisplay.php?f=39
    Last edited by ArgusVision; October 26th, 2010 at 01:59 AM. Reason: Added suggestion and link
    You know, in a way I actually like Vista...
    It's what introduced me to Linux...
    New? Checkout https://help.ubuntu.com/10.04/index.html

  3. #3
    Join Date
    Jan 2009
    Location
    Michigan
    Beans
    87
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: Find out EXACTLY what shell is being used

    Hi Vision:

    Based on the grammar and punctuation used, your post seems more like a rebuke than a suggestion.

    For those that are interested:

    ps - report a snapshot of the current process
    sed - stream editor for filtering and transforming text
    readlink - display value of a symbolic link

    The rest is (hopefully) self explanatory.

    If a mod finds that this post is better suited in the "Programming Talk" forum, I would have no objections to it being moved.

    Thanks!

  4. #4
    Join Date
    Feb 2010
    Location
    White Plume Mountain
    Beans
    8,222
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Find out EXACTLY what shell is being used

    Quote Originally Posted by for.i.am.root View Post
    If a mod finds that this post is better suited in the "Programming Talk" forum, I would have no objections to it being moved.

    Thanks!
    Done!
    Thank you for your contributions. "So long and thanks for the fish!"

  5. #5
    Join Date
    Jun 2007
    Beans
    192
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Find out EXACTLY what shell is being used

    Please accept my apologies. It's dificult to express tone in type.
    I simply think that you'll find more people with scripting and programming experience viewing your post there than in here.
    The use of quotes is to indicate that it is an exact name being used for that forum.
    You know, in a way I actually like Vista...
    It's what introduced me to Linux...
    New? Checkout https://help.ubuntu.com/10.04/index.html

  6. #6
    Join Date
    Jan 2009
    Location
    Michigan
    Beans
    87
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: Find out EXACTLY what shell is being used

    Hi uRock:

    You Rock! Thanks!

    Hi Vision:

    Thanks for the suggestion!

  7. #7
    Join Date
    Dec 2006
    Beans
    65

    Re: Find out EXACTLY what shell is being used

    You might want to check out pwd -P.

  8. #8
    Join Date
    Dec 2006
    Beans
    256

    Re: Find out EXACTLY what shell is being used

    Quote Originally Posted by for.i.am.root View Post
    Or better yet is there a one liner to figure out the current shell interpretor?
    The following will print the full path wrapped in quotes.

    Code:
    stat -c%N $(which `ps -o comm= $$`)|awk -F' ' '{print $NF}'
    Note that the $(command) construct is not available in the original Bourne shell (sh) -- if it is not supported in your shell(s) then you will have to separate things out a bit more (backticks can't be nested).

    Additionally, 'stat' appears to only perform one level of dereferencing of symbolic links.
    "We visited sixty-six islands and landed eighty-one times, wading, swimming (to shore). Most of the people were friendly and delightful; only two arrows shot at us, and only one went near -- So much for savages!" - J.C. Patterson

  9. #9
    Join Date
    Jan 2009
    Location
    Michigan
    Beans
    87
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: Find out EXACTLY what shell is being used

    Hi saulgoode:

    stat -c%N $(which `ps -o comm= $$`)|awk -F' ' '{print $NF}'

    Thanks!

    That worked really well.

    jay@DARKSTAREEE:~$ stat -c%N $(which `ps -o comm= $$`)|awk -F' ' '{print $NF}'
    `/bin/bash'
    jay@DARKSTAREEE:~$ /bin/dash
    $ stat -c%N $(which `ps -o comm= $$`)|awk -F' ' '{print $NF}'
    `/bin/dash'
    $ /bin/sh
    sh-3.2$ stat -c%N $(which `ps -o comm= $$`)|awk -F' ' '{print $NF}'
    `bash'
    sh-3.2$ exit
    exit
    $ exit
    exit
    jay@DARKSTAREEE:~$

    For those that are interested:

    stat - display file or file system status
    -c --format=FORMAT
    use the specified FORMAT instead of the default; output a new‐line after each use of FORMAT
    %N
    Quoted file name with dereference if symbolic link

  10. #10
    Join Date
    Aug 2010
    Location
    Lancs, United Kingdom
    Beans
    1,099
    Distro
    Xubuntu 14.04 Trusty Tahr

    Re: Find out EXACTLY what shell is being used

    Quote Originally Posted by for.i.am.root View Post
    Hi saulgoode:

    stat -c%N $(which `ps -o comm= $$`)|awk -F' ' '{print $NF}'

    Thanks!

    That worked really well.
    We have a bit of chicken and egg situation here. The syntax works for those shells for which it works, and doesn't work for other shells where this is the wrong syntax, csh for instance.

    Also, what if /bin/sh was a hard link to dash? It isn't, but what if it was? ( /bin/uncompress and /bin/gunzip are hard links to the same file for example, so it's conceivable that shells could do this in a future release.)

    Why doesn't using shebang solve the problem and tell you everything you need to know?

Page 1 of 3 123 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •