Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the list of all files that are sourced by bash?

Tags:

bash

shell

Is there a way to find out all the files that are sourced by bash?

Alternately, is there a single point of entry (or a first point of entry) where I can go to follow and find this information by adding a set -x at the top?

(By single point of entry, I do not mean ~/.bashrc or ~/.bash_profile because some other file higher in the source chain tells bash to load these above files in the first place).

like image 972
hrs Avatar asked Nov 20 '25 08:11

hrs


2 Answers

Reviving this question because there is an automation for this:

Execute bash and carve it out of the output. -li is login interactively, -x prints out what bash is doing internally, and -c exit just tells bash to terminate immediately. Using sed to filter out the source command or the . alias.

/bin/bash -lixc exit 2>&1 | sed -n 's/^+* \(source\|\.\) //p'
like image 197
Shloim Avatar answered Nov 21 '25 21:11

Shloim


There is no easy catch-all answer here, it depends on the combination of login/interactive attributes.

A login shell will source /etc/profile, and only the first one it finds among ~/.bash_profile, ~/.bash_login, and ~/.profile. You could call these independent points of entry: /etc/profile doesn't need to explicitly source one of the others, it's bash that does it.

For non-login interactive, you have /etc/bash.bashrc and ~/.bashrc, again independent.

For non-login non-interactive, the single point of entry is $BASH_ENV, if defined.

You can find the official description at the GNU Bash manual under Bash startup files.

like image 32
Matei David Avatar answered Nov 21 '25 20:11

Matei David



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!