Andrew Drozdov
Andrew Drozdov

Reputation: 2183

How to modify "source" shell builtin?

I'd like to modify the "source" shell builtin so that every time source is called the command "echo ${file I am sourcing}" is called.

I'd like to do this so that I can always tell which files have been sourced when I open a new bash instance.

Upvotes: 0

Views: 232

Answers (2)

dogbane
dogbane

Reputation: 274632

You can define a new function:

mysource() { echo "sourcing file: $1" && source "$@"; }

But, if you really must call it source:

source() { echo "sourcing file: $1" && builtin source "$@"; }

Note that I am using "$@" so that any arguments after the filename are passed to the builtin source command too.

Upvotes: 2

William Pursell
William Pursell

Reputation: 212268

Define a function in your shell:

source() { echo "$1"; . "$1"; }

This will only be valid for the shell in which the function is defined. Put it in the appropriate startup file if you want it to be defined in all new shells. (eg ~/.bashrc)

Upvotes: 1

Related Questions