Benches Uncategorized

14

trouble with a tricky prompt mod.

apple Verified

Imported from the Apple Support Forums via the Wayback Machine 🔗

Hello genii

I am trying to change my Terminal prompt as I had it in 10.3 where the standard user would be shown in green and if dropped down to root, that would be shown in red. I like that because the red is a good visual reminder of where you are. Example:

Standard ~/Documents $

root /etc #

However, this only partly works in Leopard . I get the green username and the directory listing in the prompt alright, but when I change to root with sudo -s, it defaults to the original Leopard bash prompt of bash-3.2#

Here is my bashrc script that worked well in Panther. The colour values are highlighted.

# System-wide .bashrc file for interactive bash(1) shells.
PS1='h:w u$ '
if [ "$TERM" != 'dumb' ] && [ -n "$BASH" ] && [ -n "$PS1" ]
then
if [ "$whoami" = 'root' ]
then
PS1='[33[22;31m]u [33[22;34m]w $ [33[00m]'

else
PS1='[33[22;32m]u [33[22;34m]w $ [33[00m]'
fi
fi
# Make bash check it's window size after a process completes
shopt -s checkwinsize

Notes: both if statements have '['square brackets']' around them but they got lost in the format.
Also, lines System-wide... and Make bash check... are commented lines that got lost too.

I have been trying so many things, but frustratingly to no avail. I also made a ~ .bash_profile, with bashrc back to orig. and it behaves the same, giving me the green standard user, but fails to process root. After each change I close Terminal and re-open it as I think that is needed.
I am at this stage willing to abandon such a fancy prompt and settle for just the 'else' format as a one line unconditional, and doing so does indeed give me the user as root, but in green not red. Maybe that is the best I can hope for, - just thought maybe I'm missing something, or maybe it is Leopard!
Any thoughts?

Here is a better view of the script.
http://farm4.static.flickr.com/3192/2523920928de4b7398d5o.png

Message was edited by: roam2

14 replies

Sign in or join Beachball Help to reply.

  1. 0

    Apple Support Community member

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Try replacing *"$whoami"* by *`whoami`* (or by *$(whoami)* ):


    if `whoami` = 'root'

    or


    if $(whoami) = 'root'

    PS
    You can use


    your script here

    to include your script in "Apple Discussion".

  2. 0

    Apple Support Community member

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    roam2 wrote:
    Hello genii

    I am trying to change my Terminal prompt as I had it in 10.3 where the standard user would be shown in green and if dropped down to root, that would be shown in red. I like that because the red is a good visual reminder of where you are. Example:

    Standard ~/Documents $

    root /etc #

    However, this only partly works in Leopard . I get the green username and the directory listing in the prompt alright, but when I change to root with sudo -s, it defaults to the original Leopard bash prompt of bash-3.2#

    Here is my bashrc script that worked well in Panther. The colour values are highlighted.

    # System-wide .bashrc file for interactive bash(1) shells.
    PS1='h:w u$ '
    if [ "$TERM" != 'dumb' ] && [ -n "$BASH" ] && [ -n "$PS1" ]
    then
    if [ "$whoami" = 'root' ]
    then
    PS1='[33[22;31m]u [33[22;34m]w $ [33[00m]'

    else
    PS1='[33[22;32m]u [33[22;34m]w $ [33[00m]'
    fi
    fi
    # Make bash check it's window size after a process completes
    shopt -s checkwinsize

    Notes: both if statements have '['square brackets']' around them but they got lost in the format.
    Also, lines System-wide... and Make bash check... are commented lines that got lost too.

    I have been trying so many things, but frustratingly to no avail. I also made a ~ .bash_profile, with bashrc back to orig. and it behaves the same, giving me the green standard user, but fails to process root. After each change I close Terminal and re-open it as I think that is needed.
    I am at this stage willing to abandon such a fancy prompt and settle for just the 'else' format as a one line unconditional, and doing so does indeed give me the user as root, but in green not red.

    What did you try exactly? Something like this:
    PS1='[33[22;32m]u [33[22;34m]w $ [33[00m]'
    - i.e. the "else" part? Wouldn't you need the "if" part to get it in red?

    I suspect the previous comment re. using `whoami` rather than $whoami will solve the problem. Another option is to have a separate bashrc etc. in root's home directory. I like this method because it is unlikely to get clobbered by a system update which might alter the scripts in /etc but there is a neatness to modifying the system scripts which this alternative lacks.

    - cfr

  3. 0

    Apple Support Community member

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Jun T, thanks for your reply.

    I have tried your suggestions and favour the second one, which was the original as it happens, but I have tried so many variations including $, I got a bit lost, but still it wont see the first condition as true.

    Playing around


    if [ `whoami` = 'root' ]
    then echo "root"
    else echo "standard"

    output is standard. change to root gives no output.

    Turning the expression false with the '!' and reversing the echoes, thus:


    if [ `whoami` != 'root' ]
    then echo "standard"
    else echo "root"

    output says standard because the condition is not true, because $USER=standard. This proves the first line does process. In this instance the else will not activate.

    When the script first runs, the condition is always false, and so I am beginning to think there may need to be some Return that needs to be involved, but at this stage I am still scratching my head.

    Thanks for the tip on how to display code in Apple Discussions.

    Message was edited by: roam2

  4. 0

    Apple Support Community member

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Clea, thanks for your reply.

    Yes there is an if to begin with:
    if condition = root then red (No 31)
    else green (No 32) (That should have been shown as green not blue. My mistake, but does not change the script.)

    Just that in Panther when I *sudo -s* and hence the condition is true, the $USER does go red, then exit turns $USER back to green. I can go in and out of the green, just don't get any red.
    I am starting to think that the time taken to solve this is exceeding its value but thanks for your time.

    Message was edited by: roam2

  5. 0

    Apple Support Community member

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Try the following. I spend a lot of time switching between Compose and Preview so it displayed the way I wanted it, and so that I could copy and paste into /etc/bashrc

    <pre>
    PS1='h:w u$ '
    if [[ "$TERM" != 'dumb' && -n "$BASH" && -n "$PS1" ]]; then
    if [[ $(whoami) = 'root' ]]; then
    PS1='e[22;31mu e[22;34mw $ e[0m'
    else
    PS1='e[22;32mu e[22;34mw $ e[0m'
    fi
    fi
    </pre>

    Oh yea. Put this is /etc/bashrc

    /etc/bashrc is the system wide bash initialization file.

  6. 0

    Apple Support Community member

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Sorry, of course you need a bracket. Create a file 'foo' with the following contents:


    #!/bin/bash
    if [ $(whoami) = 'root' ]
    then
    echo "ROOT"
    else
    echo "NORMAL"
    fi

    Then try


    chmod +x foo
    ./foo
    sudo ./foo

    Does this give expected results?

    To include your script in the discussions, do not copy/paste
    {code}
    from my post. Instead, type them by yourself.

    Message was edited by: Jun T.

  7. 0

    Apple Support Community member

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    output is standard. change to root gives no output.

    "no output" means the if...fi block is not executed at all.

    Where did you add the if...fi block? In your ~/.bashrc or /etc/bashrc?
    If you are using the default settings of Leopard, then /etc/bashrc is read only by a login shell (via /etc/profile).

  8. 0

    Apple Support Community member

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Jun T
    That little test script works as expected.

    I think the code is OK. Reversing the two conditions in the bashrc, by temporarily adding my normal Username still works, and proves the $(whoami) processes correctly.

    Something must have changed in Leopard so that the bashrc or bash_profile is not accessed if one uses sudo. Perhaps this is some security enhancement made to Leopard's bash that has changed this operation that worked in 10.3.

    I don't think there is a solution, and I thank you for your time.

  9. 0

    Apple Support Community member

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    BobHarris
    Thanks for your input. You have showed how to write that script a lot more economically that it was before, but I think the problem is with the bash shell not the script. Thanks anyway

  10. 0

    Apple Support Community member

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    I wonder if Tiger is the same as Leopard. In the version of bash I'm using, sudo -s reads /etc/bashrc but it does not change the prompt to the one I use in a root shell. sudo su (or su if root is enabled) does change the prompt.

    - cfr

  11. 0

    Apple Support Community member

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    I don't know about Tiger; jumped from Panther to Leopard.
    What you said made sense. I tried deleting those entries, (now edited) but it was still the same.

    sudo su does give a different prompt
    sh-3.2#
    but remains unaffected by the bashrc script.

    I am resigned to having handy, this line which I can paste in

    PS1='e[22;31mu e[22;34mw $ e[0m'

    Doing this is a poor workaround indeed, but I do like the effect it produces for a root prompt.

  12. 0

    Apple Support Community member

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    roam2 wrote:
    I don't know about Tiger; jumped from Panther to Leopard.
    What you said made sense. I tried deleting those entries, (now edited) but it was still the same.

    Sorry. I realised I had gotten confused as soon as I'd posted it and tried to delete the irrelevant bits. (For some reason, I got it into my head that "-n" was checking that the variable was not set as opposed to being of non-zero length which meant everything was the opposite way around.)

    sudo su does give a different prompt
    sh-3.2#
    but remains unaffected by the bashrc script.

    Huh. What if you copy /etc/bashrc to /var/root/.bashrc and/or /var/root/.bash_profile? (I think you need the latter or to edit /etc/profile - see below.)

    It seems to me as if sudo in Tiger is not using the defaults as given in the manual page. I'm almost sure this is a sudo thing rather than a bash thing...
    sudo -s
    echo $PS1
    ->
    echo $HOME
    -> /Users/myusername
    whoami
    -> root
    groups
    -> wheel daemon kmem sys tty certusers operator admin staff
    touch b
    ls -al b
    -> -rw-r--r-- 1 root myusername 0B May 27 13:45 b
    This strikes me as weird. Should be different on Leopard because default groups are different so it likely won't give such a very odd result.

    Have you tried sudo -i? (Note: I think this probably has the same effect as sudo su.)

    I don't know if this applies to you but, for me, this means it really matters a great deal how I get a root shell in ways that are not obvious just from looking at the screen. For example, rm is an alias for rm -i in the root shell on my machine but not otherwise. If I get the shell using sudo -s, rm has its ordinary meaning.

    The reason you don't get the expected prompt with sudo su is, I suspect, because you are not getting a bash shell. The default root shell is invoked instead and this is probably /bin/sh rather than /bin/bash. On my machine, /etc/bashrc is still read and I'm wondering if the reason it doesn't get read on yours is because the contents of /etc/profile is different. When invoked as sh, bash doesn't read /etc/bashrc. On my machine it does this only because /etc/profile explicitly sources it. I'm not sure if this is because I edited it (usually I'd have a record of this, but it is certainly possible I forgot to make one). It could also be a difference between Tiger and Leopard. Do you have the lines


    if [ "${BASH-no}" != "no" ]; then
    [ -r /etc/bashrc ] && . /etc/bashrc
    fi

    in /etc/profile? If not, you could either edit profile to add these lines or change the prompt directly in profile, or you could change root's default login shell if you wanted it to be bash. (I think there may be good reasons not to do this, though.)

    Or I could be even more confused now than before...

    - cfr

  13. 0

    Apple Support Community member

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Hi Clea

    sudo -s stays in bash, but sudo su invokes sh.
    The prompt format for both is:

    echo $PS1
    s-v$

    which is shell and version.

    The /etc/profile contains the same code as yours, plus one new thing in Leopard is the addition of pointing to path_helper which is where you can set additional paths.

    In Terminal preferences, both in Panther and Leopard the default login shell is the same; /usr/login/shell, though the Leopard Preferences are now more extensive. I don't know Tiger's pref. pane.

    All I know is that changing the user to root starts a new shell that does not reference either bashrc or bash_profile. That is, regardless of where you may be in path, the root always resets to Home. I think this is a bit of an indicator that it is ignoring any setting created at startup.

    So my developed workaround is setting the new (standard) prompt in .bash_profile, and having a small script I've called .rp

    #! /bin/bash
    #test and set prompt.

    if [[ $(whoami) = 'root' ]]; then
    PS1='e[22;31mu e[22;34mw $ e[0m'

    ...that I run after sudo -s. On exit it returns to the condition set in .bash_profile. As I said it is a workaround but that is the best I can do.

  14. 0

    Apple Support Community member

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    I'm a bit confused. In my case (both Tiger and Leopard), setting prompt in my ~/.bashrc works for 'sudo -s'.

    "sudo -s" or "sudo bash"
    does not change $HOME (= /Users/myname)
    reads /Users/myname/.bashrc

    "sudo bash -l"
    does not change $HOME (= /Users/myname)
    reads /etc/profile (which reads /etc/bashrc)
    reads /Users/myname/.bash_profile

    "sudo -i"
    changes $HOME to /var/root
    /bin/sh is started
    reads /etc/profile (which reads /etc/bashrc)
    reads /var/root/.profile
    (does NOT read /var/root/.bash_profile even if it exits)

    PS
    Have you modified /etc/sudoers?