1

I am inside of an debian trixie lxc in proxmox.

This is how PATH looks when i query it from root:

# echo $PATH
/sbin:/bin:/usr/sbin:/usr/bin

In my /etc/login.defs my PATH configuration looks like this:

#
# *REQUIRED*  The default PATH settings, for superuser and normal users.
#
# (they are minimal, add the rest in the shell startup files)
ENV_SUPATH      PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
ENV_PATH        PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games

This is how my /etc/profile looks

if [ "$(id -u)" -eq 0 ]; then
  PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
else
  PATH="/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games"
fi
export PATH

To my understanding my root user PATH should include /usr/local/bin. What is the mechanism that is getting applied into my shell?

In my understanding according to INVOCATION in the bash man page there is a difference between interactive login shell and interactive shell. The interactive login shell loading /etc/profile, the interactive shell ~/.bashrc.

So i am assuming since my /etc/profile looks right, that i am in an interactive shell and not in an interactive login shell.

Is there a way how i can check the kind of shell i am in, or am i totally looking in the wrong place? What am i missing here?

Solution: Looks like exactly what i described:

root@pihole:~# echo $PATH
/sbin:/bin:/usr/sbin:/usr/bin
root@pihole:~# su -
root@pihole:~# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

Forcing opening a login shell, makes the right PATH appear. Before that i am in an interactive shell but not in a login shell.

Note: I don't want to discuss, if it is a good idea to have /usr/local/bin in the root PATH. I want to know what is happening here.

2
  • To avoid accidental installation into it or run from under the root? Just a thought. Commented Jan 8 at 10:19
  • What is the output of echo "$0"? Commented Jan 8 at 12:42

1 Answer 1

0

From https://dimasmaulana.dev/posts/development/bash-check-if-shell-on-interactive-or-login/

Bash Check if Shell on Interactive or Login

In Bash, you can check whether the shell is running in an interactive or login mode using the provided commands. Here’s an explanation of each command and what it checks for:

  1. [[ $- == *i* ]] && echo 'Interactive' || echo 'Not interactive':

    • This command checks the value of the special shell variable $-, which contains a string of options and flags that are currently set for the shell.

    • The *i* pattern is used to check if the letter ‘i’ appears anywhere in the value of $-. If it does, it indicates that the shell is running in interactive mode.

    • If ‘i’ is found, it echoes ‘Interactive’, otherwise, it echoes ‘Not interactive’.

  2. shopt -q login_shell && echo 'Login shell' || echo 'Not login shell':

    • This command uses the shopt built-in command to check the status of a shell option called login_shell.

    • If shopt -q login_shell returns true (exit status 0), it means that the shell is a login shell, so it echoes ‘Login shell’.

    • If shopt -q login_shell returns false (exit status non-zero), it means that the shell is not a login shell, so it echoes ‘Not login shell’.

From https://blog.vandenakker.xyz/posts/whats-the-difference-between-a-login-and-a-nonlogin-shell/

Is My Current Shell a Login Shell?

There are two ways to check if your current shell is a login shell: First, you can check the output of echo $0: if it starts with a dash (like -bash), it’s a login shell. Be aware, however, that you can start a login shell with bash --login, and echo $0 will output just bash without the leading dash, so this is not a surefire way of find out if you are running a login shell.

Secondly, the Unix StackOverflow offers this way of finding out:

$ shopt -q login_shell && echo login || echo nonlogin

Why You Sometimes Want a Login Shell

When you switch users using su you will take the environment of the calling user with you. To prevent this, you should use su - which is short for su --login. This acts like a clean login for a new user, so the environment will not be cluttered with values from the calling user. Just as before, a login shell will read /etc/profile and the .bash_profile of the user you are switching to, but not its .bashrc. This post on StackOverflow shows why you might want to prefer to start with a clean environment (spoiler: your $PATH might be “poisoned”).

1
  • 1
    This answer mentions /etc/profile & .bash_profile (presumably ~/.bash_profile) & .bashrc (presumably ~/.bashrc). There may be others like /etc/bash.bashrc & ~/.profile (all mentioned in bash man page). What I like to do is use ~/.profile and /etc/skel/.profile so it works most commonly, even with other shells. Actually what I usually do is cp ~/.profile ~/.proforig then have .profile have two lines: [ -r ~/.proforig ] && . ~/.proforig and [ -r ~/.proflcl ] && . ~/.proflcl Then I customize ~/.proflcl like crazy, and if an OS install ever replaces my ~/.profile then I can revert easily Commented Jan 8 at 23:16

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.