2

On the parent shell (root) executing the following:

SESSION_RECORD=started

Output:

echo $SESSION_RECORD
started

However, when I check the status from a shell script, the value is not getting assigned. I am aware it creates a child and executes the script, which is why it doesn't take the variable.

Is there a way I can import that from the parent shell to the child shell? I don't want to use export as the root variable is dynamic.

script for ref:

#!/bin/bash

session=`echo $SESSION_RECORD`
echo $session
if [ "$session" == "started" ]
then
echo "screen records"
else
echo "screen not started"
fi

Basically, my script should check the value of the variable and if it matches it should get into the loop and execute commands further.

The value of the variable SESSION_RECORD changes dynamically, so based on the value, I need to run a script.

6
  • Can you clarify what you mean with I don't want to use export as the root variable is dynamic?
    – jlliagre
    Aug 24, 2020 at 11:12
  • The value of the variable: SESSION_RECORD is written whenever a user logs in. So its dynamic value and I won't be able to export that. Basically my script should check the value of the variable and if it matches it should get into the loop and execute commands further.
    – arun kumar
    Aug 24, 2020 at 11:14
  • Still unclear. What make you believe a "written variable" can't be exported (or an exported variable can't be written)?
    – jlliagre
    Aug 24, 2020 at 11:17
  • the value of the variable SESSION_RECORD changes dynamically, so based on the value, I need to run a script. Can you try with the script above and give me the snippet if possible?
    – arun kumar
    Aug 24, 2020 at 11:19
  • A variable value changes when it is set. There doesn't look to be anything specific in your use case.
    – jlliagre
    Aug 24, 2020 at 11:22

2 Answers 2

4

You don't need to set the value of a variable when you export it. You can do:

SESSION_RECORD=started
echo "Now some other stuff"
export SESSION_RECORD && ./myscript

and it will export the current value of SESSION_RECORD to your script.

2
  • 5
    Even SESSION_RECORD=$SESSION_RECORD ./myscript would work and would avoid having to export the variable to the current script's environment.
    – Kusalananda
    Aug 24, 2020 at 12:17
  • thanks it worked.
    – arun kumar
    Aug 24, 2020 at 13:14
0

If you make your script a function, then it won't create a subshell while executing, so accessing the variable should work just fine. Maybe that would work for you?

Example:

$ cat test.sh
#!/bin/bash

function foo() {
  echo $foo
}
$ source test.sh
$ foo=5
$ foo
5
$ foo=4
$ foo
4
$
3
  • I could see that you are using source command there instead of sh command to execute. Source by default doesnt create a subshell to execute, it runs on the parent shell itself. is there a way to run call the variable and run the script normally like sh script.sh ?
    – arun kumar
    Aug 24, 2020 at 11:12
  • You could also pass the variables as an argument to the script. Or have either a function or an alias with will do that for you.
    – Invidian
    Aug 24, 2020 at 11:14
  • it's actually a dynamic value that triggers only when a user logs in.. Basically my script should check the value of the variable and if it matches it should get into the loop and execute commands further
    – arun kumar
    Aug 24, 2020 at 11:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .