Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Run the bash shell in su mode UNIX
Ever tried to su to root in terminal? here's what happens under OSX 10.3:
$ su
Password:
su: Sorry
... and here's a quick fix ... add the following to your ~/.bash_profile file (in your home directory) using your favourite text editor:
alias su="sudo bash"
Restart terminal, and away you go!
$ su
Password: [type your password, you must be an administrator]
02:03:06 | compy386:~ root#
Be careful what you do, the slightest typo could zero your hard drive!

[robg adds: I don't run bash, but in my testing from the bash shell, su worked fine the first time I tried it. However, this hint is useful if you want to run the bash shell as root, instead of the default sh.]
    •    
  • Currently 2.33 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (3 votes cast)
 
[11,609 views]  

Run the bash shell in su mode | 12 comments | Create New Account
Click here to return to the 'Run the bash shell in su mode' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Run the bash shell in su mode
Authored by: jdera on May 12, '04 12:31:01PM

Okay. First, the reason the poster probably had problems using the su command is because they don't have their root account enabled. So, the first problem with this hint is its premise. The second thing wrong with it is that the sudo command has the -s switch specifically for opening a privileged interactive shell, so sudo bash is rather verbose (and might have other issues, for example, if bash weren't in your path for some reason. The third thing wrong with it is that it will only affect login shells because the settings have only been placed in ~/.bash_profile, while ~/.bashrc remains untouched.



[ Reply to This | # ]
Run the bash shell in su mode
Authored by: etrepum on May 12, '04 12:47:15PM
Without an enabled root account you can't use su. Duh. You should use sudo -s, which starts up a root shell with whatever shell you're currently using.

[ Reply to This | # ]
Run the bash shell in su mode
Authored by: rhowell on May 12, '04 01:12:44PM
This hint has already been published here.

The advantage of using sudo -s instead is that it keeps all of your environment variables from your original shell.

[ Reply to This | # ]
Run the bash shell in su mode
Authored by: evansj on May 12, '04 04:03:46PM
bash and sh are actually the same binary.

$ diff -s `which bash sh`
Files /bin/bash and /bin/sh are identical
Although for some reason it's two copies, rather than one symlinked or hard linked to the other:

$ ls -li `which bash sh`
2821956 -rwxr-xr-x  1 root  wheel  603488 16 Mar 10:08 /bin/bash
2821939 -r-xr-xr-x  1 root  wheel  603488 16 Mar 10:08 /bin/sh
(the first number is the inode number)

[ Reply to This | # ]
Run the bash shell in su mode
Authored by: rhowell on May 12, '04 04:27:52PM

If they're identical files then they should give identical output when they're run. They don't. They're doing something differently. How do they do that?



[ Reply to This | # ]
Run the bash shell in su mode
Authored by: wombat on May 12, '04 04:58:01PM

It looks at the name of what's being executed (argv[0]) and makes its decision appropriately.



[ Reply to This | # ]
Run the bash shell in su mode
Authored by: evansj on May 13, '04 09:53:28AM
Spot on. From the man page:
If bash is invoked with the name sh, it tries to mimic the startup behavior of historical versions of sh as closely as possible, while conforming to the POSIX standard as well.
There are a few specific behaviour changes documented in the man page, so man bash if you're interested.

[ Reply to This | # ]
broken hard links
Authored by: sjk on May 13, '04 12:49:34AM

I noticed that after installing one of the 10.3.x updates (.2 or .3) /bin/bash and /bin/sh were no longer hard linked, nor were other files in /bin and /usr/bin that previously had been. I'm convinced it's a side effect of installing certain dot-release updates (which I also noticed between some 10.2 releases), but it didn't happen with 10.3.1. My unconfirmed suspicion is that under certain conditions update_prebinding is responsible for making separate versions of system binaries that were originally hard linked.



[ Reply to This | # ]
Speaking of sh and bash...
Authored by: cynikal on May 12, '04 07:52:15PM

Am I the only one bothered when you see a unix system with a bash in sh's clothing? Meaning there's a bash in place of sh?

Yes I love my bash and prefer it to sh, but it tends to spoil you if you don't have the real sh available..

It might not be a big deal if you are only on systems using these faux sh's. But what about when you write a shell script and you think it's a sh syntas but you end up being used to bash syntax instead? Guess what, when it's distributed, it's not compatible on systems with a real "sh"! I've ran into countless people's "sh" scripts that use bash's $(..) command substitution expansion and it breaks things. (this is just one example on the difference in syntax).

People, please don't write bash scripts and pass em off as sh scripts.. There _IS_ a difference and the real sh is not forwards compatible to bash! Please check your syntax before claiming it's a sh-compatible script!!



[ Reply to This | # ]
Speaking of sh and bash...
Authored by: red6rick on May 12, '04 08:08:10PM

If you read the man page for bash, you'll find that it behaves differently when invoked as "sh" than when invoked as "bash". The behavior in "sh" invocation closely mimics the original sh.



[ Reply to This | # ]
Speaking of sh and bash...
Authored by: cynikal on May 13, '04 07:27:02PM

*sigh*.. fyi, i've rtfm before, have you?

if you paid more attention to what I wrote:

==<cynikal@cynsys>-<ttyp2>-<1919:50>==
--[~]-- $ sh
sh-2.05b$ ls -ld $(pwd)
drwxr-xr-x 49 cynikal cynikal 1666 13 May 00:32 /Users/cynikal/

this is NOT SH SYNTAX!!! it's BASH ONLY syntax.. with sh, only ls -ld `pwd` should work!

My original point is that people don't bother checking their scripts compatibility on systems with real sh's such as Solaris.. and please don't point me to /usr/xpg4/bin/sh, i don't want to have to modify all these broken scripts.



[ Reply to This | # ]
Speaking of sh and bash...
Authored by: LC on May 16, '04 04:13:59PM

I recall AIX utilizing a ksh as sh; I recall HP-UX providing restricted and Bourne or sh-Posix modes.
I believe that the Posix specs probably define what sh is required to do and that it's not specifically Bourne.
What era are we concerned with ... UNIX95, UNIX98, X/Open etc. Larry.



[ Reply to This | # ]