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

Alrick
  • 253
  • 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 02 '19 at 20:29
  • @DopeGhoti Just setting the prompt won't activate a virtualenv... – Kusalananda Jan 02 '19 at 20:29
  • 1
    Why do you want to run rather than source? – l0b0 Jan 03 '19 at 08: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 03 '19 at 09:29
  • 1
    If you source the top level script, and that sources virtualenv/bin/activate, you're golden. – l0b0 Jan 03 '19 at 09:32
  • @l0b0 Ok, i was wondering if there was a way without sourcing the top level script, using set command line for example or something like that – Alrick Jan 03 '19 at 09:38

1 Answers1

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

Crypteya
  • 494
  • 1
    source and . are equivalent. – Kusalananda Jan 03 '19 at 08: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 03 '19 at 08:39