The Computer Oracle

Is there a folder-specific .bashrc or .bash_profile?

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Dreaming in Puzzles

--

Chapters
00:00 Is There A Folder-Specific .Bashrc Or .Bash_profile?
00:18 Accepted Answer Score 14
00:33 Answer 2 Score 2
01:23 Answer 3 Score 0
01:41 Thank you

--

Full question
https://superuser.com/questions/915703/i...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#macos #bash #terminal #shell #shellscript

#avk47



ACCEPTED ANSWER

Score 14


Add this to your ~/.bashrc.

If .bashrc is located in current working directory:

PROMPT_COMMAND='if [[ "$bashrc" != "$PWD" && "$PWD" != "$HOME" && -e .bashrc ]]; then bashrc="$PWD"; . .bashrc; fi'



ANSWER 2

Score 2


Depending on your exact use case and constraints, ondir may suit your needs:

ondir is a small program to automate tasks specific to certain directories. It works by executing scripts in directories when you enter and leave them.

It does this by using a central ~/.ondirrc file for per-dir configuration. In contrast, the clever PROMPT_COMMAND setup that @Cyrus suggested allows for the config to reside in the individual directories themselves. Each approach is valid; it depends on the constraints and data you're dealing with.

Disclaimer: I've never used ondir personally. I came across it while looking for an automatic way to handle git user config per-dir. In that case, ondir didn't fit my needs—I ended up using a git alias passing --config options to git clone.




ANSWER 3

Score 0


addition from @Cyrus answer, if you use zsh, you need to use precmd() :

PROMPT_COMMAND='if [[ "$profile" != "$PWD" && "$PWD" != "$HOME" && -e .profile ]]; then profile="$PWD"; source .profile; fi'
precmd() { eval "$PROMPT_COMMAND" }

it's answered from this answer

nb: my prompt tests for .profile not .bashrc.