I have an open terminal, and I call a bash function:



 export PATH="foobar:$PATH";

will change_path always run in the current shell and always change the $PATH of the current shell? So far in my testing, this seems to be the case, but hard to say for sure.


Yes, the function you are calling is running in the current shell.
That is assuming the function has been imported (defined) in the current shell. That could have been done by sourcing some file (like ~/.bashrc) or by writting the function in the current shell manually. You could confirm this assumption by executing:

$ type -a change_path

If that prints the function definition, the function has been defined in the present shell and, having access to the current shell variables, could modify any of them.

That does not mean that the function will allways modify the variables if the function is properly limited, like using (...) instead of {...}:

$ change_path()(
    export PATH="foobar:$PATH";

But that function is in fact still running in the current shell, just that it starts a sub-shell (...)

To confirm, read this from bash manual (emphasis mine):

A shell function, defined as described above under SHELL GRAMMAR, stores a series of commands for later execution. When the name of a shell function is used as a simple command name, the list of commands associated with that function name is executed. Functions are executed in the context of the current shell; no new process is created to interpret them (contrast this with the execution of a shell script).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.