-2

Platform:debian8 + bash.
Running the following command in my terminal gives me the following result:

prompt> echo $0
/bin/bash

I want to get what shell login status ,echo give more info than echo $SHELL.
1.the shell type is bash
2.it is a non-login shell

prompt> echo $0
-/bin/bash

It means not only bash shell was used but only a login-shell.

When I create a file test.sh, containing only the line:

echo $0

running it produces:

prompt> /bin/bash test.sh
test.sh

In other words, I get the script name rather than the shell name. Is there a way to get the shell name /bin/bash instead?
echo $SHELL is not i want to get,echo $SHELL only contain shell type ,not telling me it is login shell or non-login shell.

To make my intent clarity,let's edite two files: ~/.profile and ~/.bashrc

vim  .profile
varLog="i am login shell"

vim  .bashrc
varLog="i am not login shell"

~$ /bin/bash
~$ echo $varLog
i am non-login shell
~$ /bin/bash --login
~$ echo $varLog
i am login shell

Now to edit a bash script.

vim  /tmp/decideShell.sh
echo  $varLog

Logout and login again.

debian8@hwy:~$ ls -al  /tmp/decideShell.sh
-rw-r--r-- 1 debian8 debian8 13 Mar  8 09:40 /tmp/decideShell.sh

debian8@hwy:~$ /bin/bash  /tmp/decideShell.sh

debian8@hwy:~$ /bin/bash  --login   /tmp/decideShell.sh
i am login shell

Why nothing output for /bin/bash /tmp/decideShell.sh?

showkey
  • 482
  • 42
  • 140
  • 295
  • 1
    I *think* I've tidied it up with your actual intent, please check and comment if I'm wrong. – paxdiablo Mar 07 '17 at 05:08
  • yes,thank paxdiablo – showkey Mar 07 '17 at 05:12
  • You might be looking for `shopt login_shell`, but it's not quite clear. – Benjamin W. Mar 07 '17 at 05:40
  • It's unclear what you actually want to accomplish. Possibly relevant questions and answers are at http://stackoverflow.com/questions/18186929/what-are-the-differences-between-a-login-shell-and-interactive-shell and http://stackoverflow.com/questions/2683279/how-to-detect-if-a-script-is-being-sourced but on the face of it, it looks like you simply want `$SHELL` and `$-`. – tripleee Mar 07 '17 at 11:10
  • 1
    $- wil get you the set options but won't tell you if it's a login shell. shopt login_shell will get you that information – louigi600 Mar 07 '17 at 11:46
  • This is a a badly posed duplicate on how to get information about the shell. – louigi600 Mar 08 '17 at 06:48
  • To save **shopt login_shell** in file,it can distiguish between login-shell and non login-shell. – showkey Mar 08 '17 at 07:36
  • How to fix my way to distiguish between login-shell and non login-shell? – showkey Mar 08 '17 at 07:38
  • In the link I gave you in my answer it's quite explicit. shopt -q login_shell && echo "lohin shell" || echo "not login shell" you can rewrite that in various forms but in essence it's the return code of "shopt -q login_shell" that matters. – louigi600 Mar 08 '17 at 12:23

3 Answers3

-1

This script can give you some idea:

echo ppid pid command
me=$$
while [ $me != 1 ]; do
  ps=$(ps h  -o ppid,pid,args -p $me)
  echo $ps
  me=$(echo $ps |cut -d" " -f1)
done

Output in my case (Debian stable linux, launched from an mrxvt launched from putty) is:

ppid pid command
4921 4922 bash
3938 4921 bash
3937 3938 mrxvt -fn --fixed-medium-r---12------iso8859-15 -vb -sl 300
1 3937 -bash

This script prints out the tree of the process, starting from the current up to init (which is not printed because we know everything about it). For every line there is the parent pid, used to go up, the pid, and the full command line. You can vary the options to ps(1), and check its output. May be you are interested only in the first line printed (the current process), or in its parent ($PPID).

I hope it helps to start.

UPDATE after comment. Running this script via this command inside putty:

./sc.sh

the output is:

ppid pid command
3938 4004 -bash
3936 3938 -bash
2306 3936 sshd: root@pts/1
1 2306 /usr/sbin/sshd

and I can see that the shell I am in is a login shell (because of "-bash").

Instead, calling the script in the following way:

bash --login script.sh

the output turns to:

ppid pid command
3938 3945 bash --login sc.sh
3936 3938 -bash
2306 3936 sshd: auser@pts/1
1 2306 /usr/sbin/sshd

shows that the process per se is not in a login shell, but it has been launched from a login shell. I think that the methods used here can be used to detect any situation.

  • can't tell apart login shell from non-login shell. – showkey Mar 08 '17 at 05:49
  • @it_is_a_literature: it does... in the listing above you see that the process non-login bash was invoked by another bash, which in turn was invoked from mrxvt, which in turn was invoked by a login shell "-bash". There can be different situations, and the script I posted can analyze all of them. – linuxfan says Reinstate Monica Mar 08 '17 at 06:32
-1

$0 has a special meaning and it's value depends on how the shell or the shell is interpreting the script is called. There are special cases that are explained in detail in the bash man page but in most cases it's the name of the shell or the shell script being interpreted ... so it's perfectly normal that your script returns it's name not the shell that is being used to interpret the script (and in your case the interpreter is implicit in the command used to execute it).

I don't really understand why you want to know some things that would be implicit of the way you executed the script or depend on the way the interpreter is specified in the script itself ... but you might have your reasons for doing that ... in which case you might want to look at this answer on stackexchange: https://unix.stackexchange.com/a/26782

As to which shell is interpreting $SHELL is the right place to look. If you just want the shell without the path do a longest prefix removal on $SHELL like this:

echo ${SHELL##*/}
Community
  • 1
  • 1
louigi600
  • 716
  • 6
  • 16
  • 1
    I show only the code to get the shell path or the shell name ... no other code ... so I cannot be wrong about non login shell ! Moreover if you care to read I give you a link on where to get that information and initially I did not down-vote your question for being a duplicate. But since you don't even read the answers I din now. – louigi600 Mar 08 '17 at 06:52
-1

Try the following:

myname=$(id -u -n)
myloginshell=$(grep "${myname}:" /etc/passwd | cut -d ':' -f 7)
printf "my login shell: %s\n" ${myloginshell}

Depends on your login privileges and security rules you will find your username in /etc/passwd or not (i.e. ldap authority or other). If your username is stored in this file, you can find your default login shell as the 7th column. The first column is your username.

If you are authorized by other way (ldap or special other) and didn't find your username in the passwd-file, you can change your profile (depends on your unix-version in .profile or .bashrc or whatelse). Set your own variable (i.e. myloginshell) with

myloginshell=$(echo $0 | sed -e 's/\-//')

and don't forget to export this variable. The complete path to your shell can everytime examinated with

type -p ${myloginshell}

Last but not least you can find some special informations with the shopt command (login_shell, restricted_shell, ...).

gmu
  • 74
  • 4