2

There are several startup files in my ubuntu installation on Google compute engine.

Is there any command that tells me which startup file is being used by the shell I am currently using?

1

1 Answer 1

4

The list of files shells typically read on start-up is limited to only a few. But those in turn may source others, and that can end up being a large numbers like when complex completion systems are loaded.

An approach is to run your shell with:

strace -e open your-shell

(or the equivalent command on your system like truss/tusc/dtruss...)

That will list all the files the shell opens. That would also list files it opens and that are not startup files, and files that it attempts but fails to open though.

Another approach could be to use the xtrace mode of some shells where the shell outputs on stderr what it's going to do for each command. For that, you just call the shell with the -x option.

Some shells like zsh will print the paths of the file where the code being run comes from by default (though you may want to set PS4 to +%x> for instance for the name of the file to also be printed for code run in functions). For some others like bash or ksh93, you can make it print it by modifying the $PS4 variable.

PS4='+$BASH_SOURCE> ' bash -x
PS4='+${sh.file}> ' ksh93 -x
PS4='+%x> ' zsh -x

With bash, you can post-process the output so as to retain only unique paths of the files with something like:

PS4=' <:>$BASH_SOURCE<:>' BASH_XTRACEFD=3 3> >(
  awk -F'<:>' 'NF>2 && !seen[$2]++ {print $2}') bash -x

(assuming the paths of the startup files don't contain newlines or <:> and that there's no multi-line command being run in those startup file that contain <:>)

Those xtrace-based approaches won't list start-up files that are empty or consist only of comments or function definitions (unless those functions are being run later on), nor start-up files that the shell would process if they were there.

You can however retrieve the files from which functions have been defined with

eval "(shopt -s extdebug; declare() { typeset -F \"\$2\"; }
  $(typeset -F))" | cut -d ' ' -f 3- | sort -u

with bash or

type ${(k)functions} | sed -n 's/.*is a shell function from //p' | sort -u

with zsh.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .