Subject: ash and su/su -

From: Tobias Leupold <[email-masked]>
Date: Sat, 25 May 2024 11:49:31 +0200

Hi all :-)

I'm new to Alpine Linux. I replaced Devuan with Alpine for my Raspberry Pi 
yesterday, and now I stumbled upon one thing that may be due to the Busybox 
ash shell:

I wanted to keep the nice minimalism Alpine brings and keep using ash (on all 
my other machines, I have, like most of us, bash). I learned I could add 
~/.profile to add a nice PS1, aliases and such stuff there. I did that for 
both my normal user and root.

However, I noticed that when I run su to become root, this file is not sourced 
and I get the default prompt I had before, without my aliases. When I do su -, 
I see the shell as if I e.g. connected directly to root via ssh, but e.g. the 
current directory is changed to /root.

This is different from what I know from my other machines. On my Gentoo, 
Artix, Devuan and Ubuntu machines, I (apparently) see no difference to the 
shell, no matter if I login directly to root, or do an su.

So what's the background? What is the difference of "su" and "su -"? And can I 
make ~/.profile being sourced when I do a simple "su"?

Thanks for all clarification and help!

Cheers, Tobias

---

From: Steffen Nurpmeso <[email-masked]>
Date: Sun, 26 May 2024 01:07:08 +0200

Tobias Leupold wrote in
 <1926392.IobQ9Gjlxr@ginuog>:
 |I'm new to Alpine Linux. I replaced Devuan with Alpine for my Raspberry Pi 
 |yesterday, and now I stumbled upon one thing that may be due to the \
 |Busybox 
 |ash shell:
 |
 |I wanted to keep the nice minimalism Alpine brings and keep using ash \
 |(on all 
 |my other machines, I have, like most of us, bash). I learned I could add 
 |~/.profile to add a nice PS1, aliases and such stuff there. I did that for 
 |both my normal user and root.
 |
 |However, I noticed that when I run su to become root, this file is \
 |not sourced 
 |and I get the default prompt I had before, without my aliases. When \
 |I do su -, 
 |I see the shell as if I e.g. connected directly to root via ssh, but \
 |e.g. the 
 |current directory is changed to /root.
 |
 |This is different from what I know from my other machines. On my Gentoo, 
 |Artix, Devuan and Ubuntu machines, I (apparently) see no difference to the 
 |shell, no matter if I login directly to root, or do an su.
 |
 |So what's the background? What is the difference of "su" and "su -"? \
 |And can I 
 |make ~/.profile being sourced when I do a simple "su"?

One is a full login, the other only starts an interactive shell
i would think.  Look at the content of $- and source your profile
if you really want to.  (Or create a file to source with all the
things you really always want, and put in .profile only things
really needed for login sessions etc.)

 |Thanks for all clarification and help!

--steffen
|
|Der Kragenbaer,                The moon bear,
|der holt sich munter           he cheerfully and one by one
|einen nach dem anderen runter  wa.ks himself off
|(By Robert Gernhardt)

---

From: Tobias Leupold <[email-masked]>
Date: Sun, 26 May 2024 10:03:21 +0200

Hi Steffen!

> if you really want to.

$- is "smi" -- IIRC, this variable contains the options set for the current 
shell. But I don't think this helps here?

Of course, I could manually source my ~/.profile after an "su". But I would 
like to automate this. Mainly, it's about my PS1 ...

> (Or create a file to source with all the things you really always want, and
> put in .profile only things really needed for login sessions etc.)

Which file would that be?

Cheers, Tobias

---

From: Steffen Nurpmeso <[email-masked]>
Date: Mon, 27 May 2024 22:49:07 +0200

Hello.

Tobias Leupold wrote in
 <2207810.C4sosBPzcN@ginuog>:
 |> One is a full login, the other only starts an interactive shell
 |> i would think. Look at the content of $- and source your profile
 |> if you really want to.
 |
 |$- is "smi" -- IIRC, this variable contains the options set for the \
 |current 
 |shell. But I don't think this helps here?

Well, i have in my ~/.profile (that used to work with quite a lot
of shells) for example

  # Some (ksh(1), NetBSD sh(1)) correctly auto-include $ENV, but bash(1) doesn't
  # (unless invoked interactively as sh(1) not bash(1));
  # also, Heirloom sh(1) doesn't know about it at all, so do include it here and
  # let it protect itself against multiple inclusion in the same shell
  ENV="${HOME}/.shrc"
  export ENV
  if [ -f "${ENV}" ]; then
          # Also, for bash(1) to recognize this $ENV when started as $bash not $sh,
          # we need to create a ~/.bashrc symlink!
          if [ -f ./.bashrc ]; then
                  :
          elif [ -L ./.bashrc ] && [ "`readlink ./.bashrc`" = "${ENV}" ]; then
                  :
          else
                  rm -f ./.bashrc
                  ln -s "${ENV}" ./.bashrc
          fi
          # However, if we know for sure anything is fine, simply skip inclusion
          case ${0} in
          *ksh*|*bosh*)
                  ;;
          *)
                  . "${ENV}"
                  ;;
          esac
  fi

I do not say it is nice.
And in my ~/.shrc (which sets prompts, too)

  eval "___isinc=\$___SHRC$$"
  if [ -z "${___isinc}" ]; then
          eval "___SHRC${$}=YES"
          export ___SHRC${$}
          case ${-} in
          *i*|*m*) # {{{
                  # New bash(1) (4.3.39) seem to loose it somehow
                  : ${TMPDIR:=/tmp}
                  export TMPDIR

                  ___on_exit() {
                          [ -f "${HOME}/.bash_logout" ] && . "${HOME}/.bash_logout"
                  }
                  trap ___on_exit EXIT

                  # Determine shell type; aux while there
                  ps1s= ps1S= ps1e= ps1W=

                ;; # }}}
          esac
  fi
  unset ___isinc

 |Of course, I could manually source my ~/.profile after an "su". But \
 |I would 
 |like to automate this. Mainly, it's about my PS1 ...
 |
 |> (Or create a file to source with all the things you really always \
 |> want, and
 |> put in .profile only things really needed for login sessions etc.)
 |
 |Which file would that be?

It all depends on the actual shell.  If you have bash, then ensure
~/.bashrc exists, bash will read it.

Hope this helps,
Ciao!

--steffen
|

---

From: Tobias Leupold <[email-masked]>
Date: Wed, 29 May 2024 12:42:54 +0200

Hi Steffen!

> ~/.bashrc exists, bash will read it.

It's not about bash, but about the default shell (which is BusyBox's ash, 
no?).

I don't get where the prompt after "su" even comes from ... I left the default 
/etc/profile untouched, which is

    export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
    
    export PAGER=less
    umask 022
    
    # use nicer PS1 for bash and busybox ash
    if [ -n "$BASH_VERSION" -o "$BB_ASH_VERSION" ]; then
            PS1='\h:\w\$ '
    # use nicer PS1 for zsh
    elif [ -n "$ZSH_VERSION" ]; then
            PS1='%m:%~%# '
    # set up fallback default PS1
    else
            : "${HOSTNAME:=$(hostname)}"
            PS1='${HOSTNAME%%.*}:$PWD'
            [ "$(id -u)" -eq 0 ] && PS1="${PS1}# " || PS1="${PS1}\$ "
    fi
    
    for script in /etc/profile.d/*.sh ; do
            if [ -r "$script" ] ; then
                    . "$script"
            fi
    done
    unset script

When I do a plain "su", I get a prompt that would be defied as "\w #". Where 
is that defined?! It neither matches /etc/profile, nor the PS1 definition in 
my user's ~/.profile, nor the one in /root/.profile ...

---

From: Johannes Findeisen <[email-masked]>
Date: Wed, 29 May 2024 14:00:37 +0200

Hi Tobias,

On Wed, 29 May 2024 12:42:54 +0200
Tobias Leupold <[email-masked]> wrote:

> It's not about bash, but about the default shell (which is BusyBox's
> ash, no?).
> 
> I don't get where the prompt after "su" even comes from ... I left
> the default /etc/profile untouched, which is

since I actually do not know the behavior of /bin/ash, I don't know if
this helps.

As you can read from the "su" man page:

-, -l, --login
           Start the shell as a login shell with an environment similar
           to a real login.

I always do a "su -" to get a real login shell. I believe that when
only doing "su" (without the dash), you will get some default where
~/.profile is not being read...

I have no system to test it because my Zsh is configured the same way
for my user and for the root user on all my systems and maybe the Zsh's
behaviour is different.

Be sure that I really do not know it, but maybe this helps you a little
bit to figure out what is going on... Busybox's ash is very minimal so
it can be that there are some features missing...

Kind regards,
Johannes

-- 
Please do not top post! Answer inline or at the bottom...

---

From: juef <[email-masked]>
Date: Wed, 29 May 2024 15:20:57 +0300

Hi,

> my user's ~/.profile, nor the one in /root/.profile ...

It's probably here:
https://git.busybox.net/busybox/tree/shell/ash.c?id=c5a1be25ba6dd705382ce6c25d96a32f79974c04#n2316

also see:
https://git.busybox.net/busybox/tree/shell/ash.c?id=c5a1be25ba6dd705382ce6c25d96a32f79974c04#n14757

if you define ENV, than it will load, i.e.:
cat /tmp/test-env
PS1="\u@\h:\w # "

export ENV=/tmp/test-env

---

From: Tobias Leupold <[email-masked]>
Date: Wed, 29 May 2024 20:23:29 +0200

E-Mail von juef vom Mittwoch, 29. Mai 2024, 14:20:57 CEST:
> > When I do a plain "su", I get a prompt that would be defied as "\w #".
> > Where is that defined?! It neither matches /etc/profile, nor the PS1
> > definition in my user's ~/.profile, nor the one in /root/.profile ...
> 
> It's probably here:

Hey, thanks for the hint!

I managed to get what I want using the ENV variable:

I created /etc/profile.d/99local.sh, only containing

    export ENV="/etc/local-env"

and said file containing the aliases I want and the PS1 definition:

    if [[ "$(whoami)" == "root" ]]; then
        PS1="\[\033]0;\u@\h:\W\007\]\[\033[01;31m\]\h\[\033[01;34m\] \W #\
[\033[00m\] "
    else
        PS1="\[\033[01;32m\]\u@\h\[\033[01;34m\] \W \$\[\033[00m\] "
    fi
    
    alias ls="ls -h --color=auto"
    alias ll="ls -l"
    alias vi="vim"

Now, this file is both sourced when I log in, and also when I do a plain "su". 
Thus, I always get my nice PS1 and my aliases :-)

Nice! Thanks again!

Cheers, Tobias

---

From: juef <[email-masked]>
Date: Wed, 29 May 2024 22:48:15 +0300

>         PS1="\[\033[01;32m\]\u@\h\[\033[01;34m\] \W \$\[\033[00m\] "

Nitpicking ;)
both `=` and `==` are equal but
https://git.busybox.net/busybox/tree/coreutils/test.c?id=5353df91cba7b397b9407701681708d0a1518df6#n333

	{ /* "="  */ STREQ   , BINOP  },
	/* "==" is bashism, http://pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html
	 * lists only "=" as comparison operator.
	 */
	{ /* "==" */ STREQ   , BINOP  },

---

From: Tobias Leupold <[email-masked]>
Date: Thu, 30 May 2024 16:30:45 +0200

E-Mail von juef vom Mittwoch, 29. Mai 2024, 21:48:15 CEST:
> 	/* "==" is bashism,
> http://pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html * lists
> only "=" as comparison operator.
> 	 */

I didn't knew that! "Bashism" :-D -- that's even more funny as it almost 
sounds like "beschissen" in German, which translates to "crappy" or "shitty" 
:-D

It's very nice from the Busybox guys that they take care of us Bash folks ;-)