I have seen many detailed explanations on how
.bash_profile and the various other profile files interact, so I'm not after an explanation of how they fit together (I've read many such explanations and yet none of them answer how to approach the below). What I would really appreciate knowing is the best way or best practice or most rational way or most efficient way (clarity, logic, and efficiency are best in my mind) to achieve the following scenarios:
If I want a line of bash code that only runs on opening any new graphical terminals, which file should I put that code into?
If I want a line of bash code that only runs at login for any new interactive shells, where should I put that code?
If I want a line of bash code that opens in both graphical terminals and interactive login shells, which file should I put that code into?
• Important point: To emphasise, I would like the above such that these lines of code only be run upon opening a shell or terminal, and not to run during invocation of any old one-line
echo "Hello World!" script that I startup. i.e. I want these to only apply when I choose to open a shell/terminal and then start typing and for other scripts to not include that. Should be simple, right?
I'm really confused by this because these really should be incredibly simple to answer, and yet, I've never seen an answer. I have seen lots of inscrutable answers about how we have to dotsource
.bashrc, or maybe it's the other way around (depending on the weather) and "you have to read document x, and document y, and then it will all be clear!" - the answers get more and more convoluted, and it is difficult to get an answer to the above three scenarios: simply / clearly / unambiguously. Would really appreciate knowing simply which file to put each piece of code into such that it will run in that scenario? I don't mind if there is minimal framework required in achieving that also. i.e. "For scenario 3, add the code to
file-x but to facilitate that you must also put the following
if-then-fi block into
file-y" but it would be a massive bonus if the answer for the three scenarios can be cross-platform (i.e. a one-for-all solution that applies to debian/ubuntu, centos/red hat, MacOS, suse, etc, and also to Gnome/KDE/MacOS etc for the graphical terminal). If there is no rational/logical way to achieve this, then that is disappointing to say the least, but hopefully I'm wrong! Hopefully someone is going to show me a clear, efficient, hopefully cross-platform (but I know that might be too much to ask), repeatable, simple, and unambiguous way to achieve the above.
man bash, the "
INVOCATION" section to start. For the rest, look at the environment(
env|sort) in each of the environments. E.g. graphical terminals have
man ttyfor more differences.
bashincludes IF-THEN-ELSE, so it's a SMOP. Put your code in
.bashrcthen it will run on any instance of anything (including an invocation of a one-line
echo "Hello World!"script!), right? That's pretty bizarre when you think about it, because if the point of the incomprehensible maze of profile files is not to give logical separation to such things, then what is the point?? Was the goal just to build as inscrutable a maze as possible with no clear approaches to anything? Would you say there is no clear approach / no way to answer my scenarios?