default shell for the user

for example, if we specify,

#!/usr/bin/ksh

then the script will be executed in korn shell.
If we don't specify that line, the script will be executed using the default shell.
So, how we can identify the default shell for the current user?
Will it be in .profile file ?

Thanks

grep "username" /etc/passwd

last field

Hello,

use echo $SHELL

Regards

use echo $0

$ echo $0
-bash

Thanks!!

pandeeswaran@ubuntu:~$grep "pandeeswaran" /etc/passwd|cut -d: -f7
/bin/bash

also works

---------- Post updated at 11:53 PM ---------- Previous update was at 11:51 PM ----------

i have one more query:

pandeeswaran@ubuntu:~$ echo $0
bash
pandeeswaran@ubuntu:~$ echo $SHELL
/bin/bash

Why echo $0 returns bash alone where as the second command returns /bin/bash?

Thanks

Echo $0 ==>will give you the shell type
Echo $SHELL ==> will give you the absolute path of the shell

thanks,
venkat

Thanks!
I have tried the below code for listing out all the shells:

#!/bin/bash
echo $SHELL
#!/bin/sh
echo $SHELL
#!/bin/csh
echo $SHELL
#!/bin/ksh
echo $SHELL
#!/bin/csh
echo $SHELL
#!/bin/zsh
echo $SHELL
#!/bin/ash
echo $SHELL

But i have got the result as:

/bin/bash
/bin/bash
/bin/bash
/bin/bash
/bin/bash
/bin/bash
/bin/bash

After that i have understood that

echo $0

is giving the subshell.
So i have changed the script to:

#!/bin/bash
echo $0
#!/bin/sh
echo ${0}
#!/bin/csh
echo ${0}
#!/bin/ksh
echo ${0}
#!/bin/csh
echo $0
#!/bin/zsh
echo $0
#!/bin/ash
echo $0

But i am getting :

pandeeswaran@ubuntu:~$ ./shells_1
./shells_1
./shells_1
./shells_1
./shells_1
./shells_1
./shells_1
./shells_1

Please help me to achieve to list out the shells.

Thanks

There is a lot of confusion and partial or plain wrong answers.

$0 is argv[0], the command given to start whatever shell or script you are currently running. The login process creates $0 (it becomes -bash for example). During login the getty code may define $SHELL (which is not always available in every type of shell).

That means when you first logon (using the above examples) $0 and $SHELL will match, sort of. When executing the script pandeesh wrote to test methods, running the script changed $0 for the duration of the script. Therefore, $0 is not reliable except when you are running in the login shell.

The SHELL variable is not changed by executing the shebang; exec looks up the magic word, like #!/bin/csh, from the first line of the script and executes the shell it points to
in order to run the script.

Next, /bin/sh is a special case. First off /bin is guaranteed to be available right when the system boots. So all boot up code depends on being able to run some shell. The shell is not specified by any standard, so /bin/sh has to be a symlink link or something called a POSIX-compliant shell. In the case of Solaris, /bin/sh is a POSIX-compliant Bourne shell. In Linux it is often a symlink to bash. Linux is not UNIX BTW. So /bin/sh may not always be POSIX-compliant.

Finally, as methyl said in another post to pandeesh - there is no one guaranteed single way to determine the shell, once it is running, in spite of all of the answers given above.

But. Best practices in shell scripting says: 'Always put a shebang at the top of the script, except in the case of a script meant to be sourced, ex: .profile'. Doing this solves most of the mysteries.

Any production environment that used all of the shells pandeesh looked for would be completely unmaintainable. Some shells like csh, should be avoided in spite of some deluded academic types who insist on teaching it.

Please stop posting more guaranteed ways to get the shell. There are none, just hacks that work sometimes, sometimes not.

Further to jim_macnamara and for further clarification.
The Shebang line only has special meaning if it is the very first line in a script. Anywhere else in the script and it is a just a comment line starting with a hash character.
Your test script therefore invoked /bin/bash then executed several identical "echo" statements.

If your homework today is about finding out what Shells are allowed on your system, there is system flat file containing a list of valid shells. This is referred to by commands that add or modify user accounts.

Simply you can see the default shell of the user which you logged in :

#echo $SHELL

but if you want to see the current shell :

#echo $0

Can you explain this? Where we can find out the flat file?

Oops. Did that from memory. The file /etc/shells is actually missing on many of my systems. The default values (when the file is missing) is in "man getusershell" , but this could well be different for Linux.

$0 ---->represent program name
so when u write $0 in script it will always give your program name.
But when u write echo $0 on prompt it will give SHELL name which is also a program

It's still worth looking at "man useradd" in some detail.
To see the defaults for a new user (including the default shell):

useradd -D

The behaviour of "useradd" can be altered with the parameter file:

/etc/default/useradd