01-15-2012
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.
This User Gave Thanks to jim mcnamara For This Post:
10 More Discussions You Might Find Interesting
1. Shell Programming and Scripting
Hi,
I've just downloaded the following files and have been tinkering with using zsh on my NT box.
Z-shell executable:
ftp://ftp.blarg.net/users/amol/zsh/zsh.exe.gz
Unix Utilities:
http://homepage.mac.com/shadowboxer/unxutils.exe
First impressions are good but I have 2 questions.... (0 Replies)
Discussion started by: Damian Ibbotson
0 Replies
2. Shell Programming and Scripting
Hi all,
I am trying to eject the cdrom from a livecd after certain stage...
Now assuming that it is possible to eject,please consider my issue!!!
The OS boots into a regular user by default...so i am unable to use the eject command to push out the drive...
However if i try pfexec eject it... (3 Replies)
Discussion started by: wrapster
3 Replies
3. Virtualization and Cloud Computing
http://c.fsdn.com/fm/screenshots/70112_thumb.png Cloud User Shell (cush) is a multi-call executable bringing RESTful cloud control to the command line, combining many useful cloud computing utilities into a single executable. It follows the Unix philosophy of writing components that work together,... (0 Replies)
Discussion started by: Linux Bot
0 Replies
4. Shell Programming and Scripting
Hi All
I have written one shell script for GPRS route add is given below named GPRSRouteSet.sh
URL="www.google.com"
VBURL="10.5.2.211"
echo "Setting route for $URL for GPRS"
URL_Address=`nslookup $URL|grep Address:|grep -v "#"|awk -F " " '{print $2}'|head -1`
echo "Executing ... (3 Replies)
Discussion started by: mnmonu
3 Replies
5. UNIX for Dummies Questions & Answers
Hi,
I am new for solaris...
how can we change default shell for any user and how to check that which shall currently we are in...... (1 Reply)
Discussion started by: lalit21984
1 Replies
6. UNIX for Dummies Questions & Answers
Hello,
I have a problem with my FTP users.
I have create the ftp user on my AIX server, I set the default shell /bin/false or /usr/bin/false.
Its enough that the user can logon via FTP.
But when I try the user for example via ssh, the shell session is not killed, the session is defunct... (4 Replies)
Discussion started by: kalaso
4 Replies
7. Shell Programming and Scripting
Hi Every body,
I would need a shell script program to login as different user and perform some copy commands in the script.
example: Supppose ora_toms is the active user
ora_toms should be able to run a script where user: ftptomsp pass: XXX should login through and run the commands
... (9 Replies)
Discussion started by: ujjwal27
9 Replies
8. Shell Programming and Scripting
I would like to replicate the functionality of chsh (or passwd -e) by awk.
This is what I got so far, but I think there should be an easier way to search and replace field $7 only for lines beginning with user_name:
awk -v user_name="$user_name" -v new_shell="$new_shell" -F: '$1 == user_name {... (2 Replies)
Discussion started by: nomad84
2 Replies
9. UNIX for Dummies Questions & Answers
sxsaaas (3 Replies)
Discussion started by: VikrantD
3 Replies
10. Shell Programming and Scripting
Hi,
I need to switch from local user to root user in a shell script.
I need to make it automated so that it doesn't prompt for the root password.
I heard the su command will do that work but it prompt for the password.
and also can someone tell me whether su command spawns a new shell or... (1 Reply)
Discussion started by: Little
1 Replies
CHSH(1) User Commands CHSH(1)
NAME
chsh - change login shell
SYNOPSIS
chsh [options] [LOGIN]
DESCRIPTION
The chsh command changes the user login shell. This determines the name of the user's initial login command. A normal user may only change
the login shell for her own account; the superuser may change the login shell for any account.
OPTIONS
The options which apply to the chsh command are:
-h, --help
Display help message and exit.
-R, --root CHROOT_DIR
Apply changes in the CHROOT_DIR directory and use the configuration files from the CHROOT_DIR directory.
-s, --shell SHELL
The name of the user's new login shell. Setting this field to blank causes the system to select the default login shell.
If the -s option is not selected, chsh operates in an interactive fashion, prompting the user with the current login shell. Enter the new
value to change the shell, or leave the line blank to use the current one. The current shell is displayed between a pair of [ ] marks.
NOTE
The only restriction placed on the login shell is that the command name must be listed in /etc/shells, unless the invoker is the superuser,
and then any value may be added. An account with a restricted login shell may not change her login shell. For this reason, placing /bin/rsh
in /etc/shells is discouraged since accidentally changing to a restricted shell would prevent the user from ever changing her login shell
back to its original value.
FILES
/etc/passwd
User account information.
/etc/shells
List of valid login shells.
/etc/login.defs
Shadow password suite configuration.
SEE ALSO
chfn(1), login.defs(5), passwd(5).
shadow-utils 4.5 01/25/2018 CHSH(1)