1

Hello and thanks in advance for any assistance anyone can offer me

I've been trying to improve my understanding of the shell login process and it seems to be a never ending rabbit hole with it's rules on what get started when. Currently, I'm trying to understand what variables a non-interactive & non-login shell would see in a minimal environment. (I'm using the Centos 7 distro btw)

I run the following script, and can verify my script is running in a non-interactive & non-login shell with the $- & shopt output... as well as the defined variables from the output of the printenv command

#!/bin/bash
#BASH_ENV=/home/kmg/environment

echo $-
shopt | grep login_shell
printenv
# echo $TEST1

This is the output from my script

hB
login_shell     off
XDG_SESSION_ID=407
HOSTNAME=centos7lab
TERM=xterm
SHELL=/bin/bash
HISTSIZE=1000
USER=kmg
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=01;36:*.au=01;36:*.flac=01;36:*.mid=01;36:*.midi=01;36:*.mka=01;36:*.mp3=01;36:*.mpc=01;36:*.ogg=01;36:*.ra=01;36:*.wav=01;36:*.axa=01;36:*.oga=01;36:*.spx=01;36:*.xspf=01;36:
PATH=/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/kmg/.local/bin:/home/kmg/bin
MAIL=/var/spool/mail/kmg
PWD=/home/kmg
LANG=en_US.UTF-8
HISTCONTROL=ignoredups
HOME=/home/kmg
SHLVL=2
TEST_ENV=this is a test of the /etc/environment file
LOGNAME=kmg
LESSOPEN=||/usr/bin/lesspipe.sh %s
XDG_RUNTIME_DIR=/run/user/1000
_=/bin/printenv

I'm not sure what to expect... But I'm getting more output from the printenv command that I would expect. I'm particularly confused by the where variable defined in the /etc/environment file is appearing (I would have expected it to be defined first because PAM reads it), and the fact the TERM, LESSOPEN & LS_COLORS variables are defined.

I've tried to read up on the subject but it doesn't seem like this is the best documented topic. Of the post I've read, it seems like people are using incorrect or conflicting terminology to describe what they're trying to say... And one post will say one thing while a different post will say another. And It's sort of screwed me up and confused me.

I would be extremely grateful for anyone that would be willing to spend a little time to help me straighten this topic out in my head!

Once again... thanks in advance for anyone that's taken the time to read this

1

Exported environment variables get inherited from the parent process to the child. So unless you ran your script in a context that actually removes environment variables (like via sudo, with the env_reset option in effect), you'll probably get some variables from the parent process.

Sudo's list of basic environment variables is documented in sudoers(5) as follows:

By default, the env_reset option is enabled. This causes commands to be executed with a new, minimal environment. On AIX (and Linux systems without PAM), the environment is initialized with the contents of the /etc/environment file. The new environment contains the TERM, PATH, HOME, MAIL, SHELL, LOGNAME, USER, USERNAME and SUDO_* variables in addition to variables from the invoking process permitted by the env_check and env_keep options. This is effectively a whitelist for environment variables.

By the way, through the /proc filesystem, you can take a peek at the environment of any process you own - or if you are root, any process on the system.

For example, if you want to know what variables the init process (process #1) has, you can become root and do this:

# strings /proc/1/environ
selinux=0
SHLVL=1
HOME=/
init=/sbin/init
TERM=linux
drop_caps=
BOOT_IMAGE=/vmlinuz-4.9.61-atom
PATH=/sbin:/usr/sbin:/bin:/usr/bin
crashkernel=384M-:128M
PWD=/
rootmnt=/root

(This example was from a Debian 9 system with a custom kernel.)

So, there are some fundamentals, probably provided by either the initramfs or the kernel itself, like SHLVL, HOME, TERM, PATH and PWD. There are also some variables related to the boot process and the various optional security features.

  • Thanks for the reply! I ran the script in cron and it produced less output – Bodisha Nov 21 '17 at 21:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.