peoro
peoro

Reputation: 26060

Sourcing variables as local in bash

I've got a bash file where are defined a number of variables:

VAR1="value1"
VAR2="value2"
# ...

I need to import these variables in ~/.bashrc in order to customize PS1, PATH and so on, but don't want that the imported variables can be accessed outside of ~/.bashrc.

To make myself clear with an example, I'd like to do something like this:

function setPATH
{
    local . ~/bashvars.sh # this isn't legal, of course...
    PATH="$PATH:$VAR1"    # $VAR1 is defined in ~/bashvars.sh
    unset -f setPATH
}
setPATH

How can I do this?

Upvotes: 3

Views: 1691

Answers (1)

Samus_
Samus_

Reputation: 2992

use a subshell:

getPATH() (
    . ~/bashvars.sh
    echo "$PATH:$VAR1"
)
PATH=$(getPATH)

Upvotes: 5

Related Questions