2

Is it possible to keep source from an executed script without sourcing it ?

Here is the script install.sh:

JJJ=bar

When i execute the script like ./install.sh, after execution, i got the following prompt JJJ :

user1@localhost% echo $JJJ

When i source the file, like source install.sh, i got the following value for $FOO :

user1@localhost% echo $JJJ
bar

For, now all is ok.

But i would like to know if there is a way to set the JJJ value and keep the value of JJJ after script execution, without sourcing the script. In other words, is a their a way that after ./install.sh, the following prompts gives :

user1@localhost% echo $JJJ
bar

marked as duplicate by Kusalananda bash Jan 3 at 8:34

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Look in that script for where it sets PS1, which is where your prompt is defined, and incorporate it in your own which is usually set in your profile. – DopeGhoti Jan 2 at 20:29
  • @DopeGhoti Just setting the prompt won't activate a virtualenv... – Kusalananda Jan 2 at 20:29
  • 1
    Why do you want to run rather than source? – l0b0 Jan 3 at 8:43
  • @l0b0 Hi, this example is just a simple example. The real probleme is that i execute a script that inside is sourcing another script. I would like that this source stay persistent. In the executed script, the command source virtualenv/bin/activate is triggered, and i would like to keep this source persistent. Is it possible ? – Alrick Jan 3 at 9:29
  • 1
    If you source the top level script, and that sources virtualenv/bin/activate, you're golden. – l0b0 Jan 3 at 9:32
0

I'd recommend having a look at this question. The selected answer is very thorough. To allow the variable to persist, you'll need to use the export function as your current deceleration is limited to the context of the script.

Change install.sh to:

export FOO=BAR

Then instead of ./install.sh you could try . install.sh

  • 1
    source and . are equivalent. – Kusalananda Jan 3 at 8:33
  • @Crypteya Thanks for link, it gives more clues. The real probleme is that i execute a script that inside source another script. I would like that this source stay persistent. In the executed script, the command source virtualenv/bin/activate is triggered, and i would like to keep this source persistent. Is it possible ? – Alrick Jan 3 at 8:39

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