Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

View which Bash profile scripts were loaded

Tags:

bash

macos

Is there a way in Bash to view which "dot files" were loaded, and in what order they were loaded in?

I'm trying to authoritatively figure out which Bash profile files were loaded, and what order they were loaded in, on Mac OS X.


1 Answers

I wouldn't know how to determine this dynamically, on any given OS X system, without any preparatory work.

However, with preparatory work as an option, Etan Reisner and twalberg offer helpful options in their comments:

  • Trace the files that bash opens when a new shell is created - see below.

  • If you know the set of candidate files, edit each and put something like echo "Loading file <filename>..." at the top.

    • man bash should list all candidate files
    • The disadvantage of this approach, apart from being cumbersome, is that you'll also have to manually scan the contents of these files, to see if they load other files (and so on, transitively); for instance, the particular code in standard profile file /etc/profile on OS X loads non-standard file /etc/bashrc, which in turn (since OS X 10.11) loads /etc/bashrc_Apple_Terminal.

Tracing the files opened by a bash shell during startup:

opensnoop is a shell script wrapper around the dtrace utility that comes with OS X and that you can use as follows:

Caveat (tip of the hat to 4ae1e1): With the introduction of SIP (System Integrity Protection) in OS X 10.11, opensnoop no longer works with executables in system locations such as /bin/, /sbin/ and /usr/bin, which includes bash.
DTrace - and thus opensnoop - can be made to work in these locations, but only if you boot into the recovery partition, select Utilities > Terminal, and execute
csrutil enable --without dtrace there; note, however, that you'll get the following dire warning: csrutil: requesting an unsupported configuration. This is likely to break in the future and leave your machine in an unknown state.
A pragmatic workaround is to copy /bin/bash to a user-specific location and execute it from there - see below.

  • Run sudo opensnoop -n bash in an existing Terminal window.
  • Open a new Terminal window, which starts another bash instance.
    • OS X 10.11+ (SIP workaround):
      • Ignore the invalid user access in action ... error messages you're seeing initially.
      • Assuming you've (temporarily) copied your bash executable to a location other than /bin/, /sbin/ or /usr/bin - such as to ~ - launch it with ~/bash -l, which emulates how Terminal.app creates a new instance (it creates an interactive logon shell).
  • Examine the output from the opensnoop command:
    • Luckily, it even shows unsuccessful attempts to open files - indicated by value -1 in the FD column, which tells you which files bash tries to open, and in what order; note that in the case of the ~/.bash_profile, ~/.bash_login and ~/.profile troika only the first one that actually exists is opened.

Example output from a pristine OS X 10.10 system:

  UID    PID COMM          FD PATH                 
  501   5110 bash           3 /dev/dtracehelper    
  501   5110 bash           3 /dev/tty             
  501   5110 bash           3 /usr/share/locale/en_US.UTF-8/LC_COLLATE 
  501   5110 bash           3 /usr/share/locale/en_US.UTF-8/LC_CTYPE 
  501   5110 bash           3 /usr/share/locale/en_US.UTF-8/LC_MONETARY 
  501   5110 bash           3 /usr/share/locale/en_US.UTF-8/LC_NUMERIC 
  501   5110 bash           3 /usr/share/locale/en_US.UTF-8/LC_TIME 
  501   5110 bash           3 /usr/share/locale/en_US.UTF-8/LC_MESSAGES/LC_MESSAGES 
  501   5110 bash          -1 /etc/.mdns_debug     
  501   5110 bash           3 /etc/profile         
  501   5110 bash           3 /etc/bashrc          
  501   5110 bash          -1 /Users/jdoe/.bash_profile 
  501   5110 bash          -1 /Users/jdoe/.bash_login 
  501   5110 bash          -1 /Users/jdoe/.profile 
  501   5110 bash           3 /Users/jdoe/.bash_history 
  501   5110 bash           3 /Users/jdoe/.bash_history 
  501   5110 bash           3 /usr/share/terminfo/78/xterm-256color 
like image 67
mklement0 Avatar answered May 20 '26 08:05

mklement0



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!