How do shells like bash first get the path and/or environment? Is it a file?

Here is what I have tried:

  • /etc/paths (Apparently exists on MacOS, cannot find on my Debian system)

  • Simply loading $path (Kind of a cheat for what I'm doing, but it is a option if it exists)

  • Google fu (Only pulls results about how to modify and set the path normally via the shell)

  • Please specify what Unix this question is aimed at. It happens in different ways depending on Linux distribution and it's different again on the BSDs. – Kusalananda May 4 '17 at 15:26
  • 2
    What question is left after you read the INVOCATION chapter of man bash? – Philippos May 4 '17 at 15:29
  • Philippos: Thanks, i was trying to find a close to crossplatform solution, however, because i want to avoid hardcoding a default 'backup' path into my program. Kusalananda: Its aimed at Debian and related systems, but information about other systems is welcome – moonheart08 May 4 '17 at 15:37
  • Well on most BSD systems, the path is set in the /etc/login.conf database. I don't know about Debian. – Kusalananda May 4 '17 at 15:50
  • 1
    @moonheart08, it's more of a question of "what are all the places that environment variables might get set in": there are a few. Of course Bash itself reads certain files, but some envvars might get inherited from the parent process. So, are you in it just out of academic interest, or is there some specific point you are looking for? – ilkkachu May 4 '17 at 16:52

Normally $PATH is inherited from the environment, and shell startup scripts may modify it.

But if the environment variable isn't set, it apparently has a hard-coded default, which depends on the version (or maybe configuration options set when compiling it).

On my OS X system running bash 3.2.57(1)-release

$ env -i bash --norc --noprofile -c 'echo $PATH'

On Debian running bash 4.2.37(1)-release

$ env -i bash --norc --noprofile -c 'echo $PATH'

You can confirm that these are hard-coded into the binary by using

strings /bin/bash | grep /usr/local/bin
|improve this answer|||||
  • Nice to know, makes me not feel like a cheat for using a hardcoded path as a last ditch resort. Thanks! – moonheart08 May 4 '17 at 19:04
  • Hardcoded defaults of last resort are a good defensive programming practice. – Barmar May 4 '17 at 19:05

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.