View Full Version : Run another bash from bash extending variables
pedrommone
April 16th, 2013, 02:24 AM
My scripts are like those:
#!/bin/bash
SERVER_ID=1
SERVER_NAME="Test One"
#!/bin/bash
echo "$SERVER_ID"
I want to include the second script inside the first and extend the first script variables. But I dont know how, thanks in advance!
prodigy_
April 16th, 2013, 04:30 AM
http://ubuntuforums.org/showthread.php?t=2124843
tgalati4
April 16th, 2013, 04:30 AM
echo $SERVER_ID
Returns:
1
What is the problem? Don't use quotes around variable names.
The variables will remain defined as long as the script continues to run.
tgalati4@Mint14-Extensa ~ $ echo "Server Number " $SERVER_ID "Server Name " $SERVER_NAME
Server Number 1 Server Name Test One
I don't see any problems with
#!/bin/bash
SERVER_ID=1
SERVER_NAME="Test One"
echo "Server Number: " $SERVER_ID "Server Name: " $SERVER_NAME
exit 0
pedrommone
April 16th, 2013, 02:27 PM
http://ubuntuforums.org/showthread.php?t=2124843
Thanks mate, as soon as get home gonna check that!
echo $SERVER_ID
Returns:
1
What is the problem? Don't use quotes around variable names.
The variables will remain defined as long as the script continues to run.
tgalati4@Mint14-Extensa ~ $ echo "Server Number " $SERVER_ID "Server Name " $SERVER_NAME
Server Number 1 Server Name Test One
I don't see any problems with
#!/bin/bash
SERVER_ID=1
SERVER_NAME="Test One"
echo "Server Number: " $SERVER_ID "Server Name: " $SERVER_NAME
exit 0
I want run the second bash and the second has to read the variables from second.
steeldriver
April 16th, 2013, 02:54 PM
You can export the variables from one shell (script) to the other e.g.
$ cat script1
#!/bin/bash
export SERVER_ID=1
export SERVER_NAME="Test One"
./script2
exit 0
$
$ cat script2
#!/bin/bash
echo "Server Number: $SERVER_ID" "Server Name: $SERVER_NAME"
exit 0
$
$ $ ./script1
Server Number: 1 Server Name: Test One
or you can set the environment explicitly on the line that calls your 2nd script e.g.
$ cat script1
#!/bin/bash
SERVER_ID=1
SERVER_NAME="Test One"
SERVER_ID="$SERVER_ID" SERVER_NAME="$SERVER_NAME" ./script2
exit 0
$
$ ./script1
Server Number: 1 Server Name: Test One
If you're not calling script2 from script1, then you can put all the exports in the first file and source it from the second e.g.
$ cat script1
#!/bin/bash
export SERVER_ID=1
export SERVER_NAME="Test One"
$
$ cat script2
#!/bin/bash
source ./script1
echo "Server Number: $SERVER_ID" "Server Name: $SERVER_NAME"
exit 0
$
$ ./script2
Server Number: 1 Server Name: Test One
$
pedrommone
April 16th, 2013, 07:43 PM
Thanks! It is not working for now, I got another idea, if someone can help-me, here's the thread: http://ubuntuforums.org/showthread.php?t=2135204
Powered by vBulletin® Version 4.2.2 Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.