sh[it] and What am I missing here?

Polytropon freebsd at edvax.de
Sun Jun 5 20:13:31 UTC 2016


I'm not sure if it has been mentioned in an explicit way, so
allow me to pick up some lines and clarify:

On Sun, 5 Jun 2016 12:46:46 -0400, Baho Utot wrote:
> login in as a user ( non root )
> set
> SHELL is set to SHELL=/bin/sh
> Ok now try
> su - (give password)
> set
> SHELL=/bin/csh Ok that makes sense
> Now
> /bin/sh
> set
> SHELL=/bin/csh WTF?

This is to be expected. The variable $SHELL corresponds to the
"shell" field of /etc/passwd, which sets the login shell. When
you use "su -", you perform a login operation, so root gets
assigned the login shell as specified by /etc/passwd.

Additionally, "su -" sets the environmental variables for the
root user and therefore overwrites possibly different settings
of the initial user; "su -m" preserves that user's environment.



> login as root
> set
> SHELL=/bin/csh Ok that makes sense
> /bin/sh
> set
> SHELL=/bin/csh WTF?

Again, the same thing happens. If you execute /bin/sh, it will
inherit the environment previously set up by /bin/csh, which
also contains the $SHELL variable.

Remember, $SHELL does not state which shell you are currently
using - instead, it contains the name of the login shell.



> It looks to me ( if I am not missing something here )
> That I can only get to sh by loging in as a user...Again WTF

If you want to interactively run sh (the Bourne shell equivalent
of FreeBSD, which is not a good dialog shell, but the system's
default scripting shell, as well as the dialog shell for the
limited maintenance mode / single user mode), just execute it.

	% sh
	# _

For scripting use, /bin/sh is recommended over bash except you
really _really_ want to use bash-isms that sh does not implement.
In this case, make sure your script starts with "#!/usr/local/bin/bash"
(the default path after installation of bash from ports) or the
more convenient "#!/usr/bin/env bash" which will also work if you
force bash's install into /bin.

However, you don't need to execute a shell script written for sh
from inside sh. It's just important that the following conditions
are met: (1st) "#!/bin/sh" is the first line, (2nd) the script is
executable (use "chmod +x <name>" to set the x bit), and (3rd) the
script is at a location inside $PATH, the search path from where
executables are sourced. When you run csh, give the "rehash" command
after you've put something into a valid executable location, or just
specify the full path (use "./name" or "./name.sh" for the current
directory).



-- 
Polytropon
Magdeburg, Germany
Happy FreeBSD user since 4.0
Andra moi ennepe, Mousa, ...


More information about the freebsd-questions mailing list