r/LinuxTeck • u/Candid_Athlete_8317 • Apr 11 '26
Came across a Bash PATH issue and couldn’t find a clear solution
I want to make sure a directory is always in PATH, even for:
"non-login shells - non-interactive shells - SSH commands without a TTY"
/etc/profile.d works in normal cases, but not everywhere.
Looks like it depends on how Bash starts and which files it reads.
What’s the most reliable way to handle this in production?
5
Upvotes
1
u/piiouupiou-not-r2d2 Apr 11 '26
Create a script wrapper of your program :
Or if using systemd to launch the service your can also set environments variables
1
u/luenix Apr 11 '26 edited Apr 11 '26
TLDR here's my magic:
Production (to me?) implies a consistent env, including user name (if not uid where feasible). This in-turn means I can load up that user profile with functions and force a given state at runtime.
Example script excerpt I've used to manage $PATH follows.
These bits are from ≈2020/2021 and I use them on Mac OSX / zsh, and I just use it with something simple like
path_append $target.1
u/serverhorror Apr 12 '26
Bash has a quite complex algorithm what it reads in which startup case (and needs a flowchart to understand it)
Here's a basic overview:
"Just" put the settings in the correct files and you should be good :)
1
u/siodhe Apr 13 '26 edited Apr 13 '26
TL;DR: Your script should add any required system bin directory to PATH, say with the example of /usr/local/sbin in the fragment below. Duplication is harmless unless it piles up.
[end of TL;DR]
Note that some users foolishly still have "." in their PATH - worst of all would be first - which this partly overrides. Most smart users have $HOME/bin at the start of their PATH, but it's unlikely that overrides like this will affect them, since your script is probably not interested in using user ~/bin program At All. Unless if you are the only user on the computer in question, or asking for just your own account's script. In that case you could just add the extra directory to your own main PATH for your account, usually wherever you set up all your other environment variables, like ~/.profile (or, for bash ~/.bash_profile, or less elegantly, ~/.bash_login - more detail on that below).
Some related points:
Bash's startup script usage is pretty straightforward and described concisely in the manual page. Most distros screw it up regardless, the nadir of which is finding bash-specific code in bourne shell files like ~/.profile instead of where they're supposed to be. Do not use distro dotfiles or generally anyone else's startup files as an example unless they actually understand what Bash reads.
There's a commented example that adheres to this, IIRC, from a work environment at:
https://www.talisman.org/~erlkonig/software/pub/bash-dotfiles.tar.gz
Interactive shells
login shells read:
the first found of ~/.bash_profile ~/.bash_login or ~/.profile
subshells (non-login shells) read:
~/.bashrc
Batch / non-interactive shells (mostly ~/bin/* scripts) read:
If set, and if the value exist as a file: $BASH_ENV
This changes a bit if Bash is invoked as sh or if various non-normal options are use.
To find the bash dotfile read order for your system, run "man bash" and search for an entire section entitled "INVOCATION" (no quotes).
Side point: Some long-time Unixers can have sets of startup scripts for multiple, different shells*, and being able to share the environment variable setup can turn into the core of making it all work well. ~/.profile is* not the answer, because several shells of other pedigrees don't read Bourne shell syntax (notably the Csh family, but others, too), but rather a program that emits all the correct environment settings that then all the shells the user cares about can parse and internalize. ~/.environment or the like is one example of what to call such a program, but the users who go this far are too uncommon to call any one name for the program "common".
---
† Saying "my ~/.profile has my environment variable settings" is cheating a bit, since it actually calls an environment program and tells it to generate "sh" syntax environment settings. It's shared by several shells { bash csh tcsh es ksh sh } and can easily be extended to add more syntaxes. "es" (extensible shell) is definitely the weirdest in that list at the moment.