4

I'm not giving the real example as the post becomes so long that no one would read it. So I'll give other examples I have a bash script name.sh and it is programmed as below-

#!/bin/bash
echo "Please Enter your name"
read name
echo "Hello $name"
sudo bash age.sh

And another script(say age.sh) like this-

#!/bin/bash
*echo "Hello $name"
echo "Please enter your age"
read age
echo "Thank you"

*Here in the second script I want the variable $name from the first script to be used with the same value in the second script so the user doesn't have to type his name again Of course if I'm declaring a variable then I can do it in the other script too but if the user is declaring it then I can't predict what the user will answer. So how do I transfer the same value over to the other script?
I have another idea to do it
It goes like this-

#!/bin/bash
echo "Please Enter your name"
read name
echo "Hello $name"
sudo bash age.sh --variable_value

Somehow can i make the 2nd script use the values given after -- and assign the same variables to them?

2
  • 1
    Do you really need to execute age.sh with sudo? That introduces an extra complication with respect to passing variables (since sudo resets its environment by default) – steeldriver May 18 at 20:32
  • Is *echo a typo, or is it really in your script? I don't think that would actually work in a real script.... – David Z May 19 at 1:19
4

Export the variable in the calling script:

read name
export name="$name"

This way, the variable will propagate to any called script and sub-shells.

To retrieve command line parameters in a script, use the variables "$1", "$2", ... These are populated with the different arguments you passed to the command. "$0" will retrieve the name of the command itself, "$@" will retrieve the entire parameter string at once.

3

There are at least two approaches that you can use:

  1. Pass the variable as a positional parameter to the script:

    ./script.sh $VARIABLE
    
  2. Export the variable, so subshells can use it:

    export VARIABLE
    ./script.sh
    

Let's say, in the normal case, when you run a script from another script or probably more correctly from a shell the subject script forks a subshell.

Here is an example wit two scripts called master.sh and slave.sh:

$ cat master.sh
#!/bin/bash
echo "Please Enter your name"
read THE_NAME
./slave.sh "$THE_NAME"
$ cat slave.sh
#!/bin/bash

# Test whether the $NAME is provided in some way. If not ask the user for the value.
if [[ -z $THE_NAME || $THE_NAME = '' ]]; then THE_NAME="$1"; fi
if [[ -z $THE_NAME || $THE_NAME = '' ]]; then echo "Please provide your name"; read THE_NAME; fi

echo "Your name is: ${THE_NAME}"
$ ./slave.sh
Please provide your name
SPAS Z. SPASOV
Your name is: SPAS Z. SPASOV
$ THE_NAME=Spas
$ export THE_NAME
$ ./slave.sh
Your name is: Spas
$ ./master.sh
Please Enter your name
Spas Spasov
Your name is: Spas Spasov

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.