General question about passing variables among shell scripts


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting General question about passing variables among shell scripts
# 1  
Old 05-09-2013
General question about passing variables among shell scripts

So this is something I've been wondering how to do for a while. Suppose I have two shell scripts a.sh and b.sh

script a does some function and outputs to a varable $x . I would then like to take $x into the second function, b.sh, and do a function on it and create some output. So how do you pass variables into a shell script?
# 2  
Old 05-10-2013
Quote:
So how do you pass variables into a shell script?
You pass variables from a.sh to b.sh by adding the variable after the script name, such as b.sh "$x". You receive variables in b.sh by using $1 for the first variable passed, $2 for the second, etc. Here is an example you can look at that I think makes it clearer:
Code:
$ cat a.sh
x=`date`
echo x in a.sh: $x
echo Passing x variable to b.sh script
b.sh "$x"

Code:
$ cat b.sh
echo Entering b.sh script
x="$1"
echo x in b.sh: $x
echo About to do function on x variable
y=`echo $x | cut -d " " -f 1`
echo "y (first field of x): $y"

Code:
$ ./a.sh
x in a.sh: Thu May 9 20:38:20 PDT 2013
Passing x variable to b.sh script
Entering b.sh script
x in b.sh: Thu May 9 20:38:20 PDT 2013
About to do function on x variable
y (first field of x): Thu

Note that the variable was passed as "$x" instead of $x, because without the quotes $1 would have just been "Thu".
These 2 Users Gave Thanks to hanson44 For This Post:
# 3  
Old 05-10-2013
You can also make use of export command Smilie

a.sh
Code:
#!/bin/sh
echo "Testing 1st script"
x="Came from a.sh"
export $x
b.sh

b.sh
Code:
echo "Testing 2nd script"
echo $x
x=$x"Added at b.sh"
echo $x

This will be useful when you have to deal with more number of variables Smilie
These 2 Users Gave Thanks to PikK45 For This Post:
# 4  
Old 05-10-2013
Wow, you guys are the best. I couldn't have asked for a better explanation. This is going to make so much of my work cleaner and easier. Thank you.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing variables between sub-scripts

I have written a program for some data analysis. It is gettin long. I would like to restructure it so that i can have a master file which controls multiple subscripts in order to make it easier to understand. I need to be able to define variables in the master script which are used by all three... (2 Replies)
Discussion started by: carlr
2 Replies

2. Shell Programming and Scripting

Passing awk variables to shell

Hi. I need to parse file and assign some values to variables, right now i do like below MYHOMEDIR=`awk '/Home/ {print $NF}' output.txt` MYSHELL=`awk '/Shell/ {print $NF}' output.txt` PRGRP=`awk '/Primary/ {print $NF}' output.txt` SECGRP=`awk '/Second/ {print $NF}' output.txt` In this... (10 Replies)
Discussion started by: urello
10 Replies

3. Homework & Coursework Questions

Passing shell variables to a webpage

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: This is my assignment as a whole - Use SVG to present certain dynamic (the raw data should change at least once... (5 Replies)
Discussion started by: ChedWick
5 Replies

4. Shell Programming and Scripting

Passing gnuplot variables to shell script

Hi, I need to pass a gnuplot value to a shell script. I have a main shell script (Main.sh) that has a bunch of gnuplot commands. Main.sh calls another gnuplot script (Child.gnu). A part of the code in Child.gnu is as follows: sp '</data/src/scripts/results/plot_data.sh $col' u (A):2:3 w pm3d... (8 Replies)
Discussion started by: annazpereira
8 Replies

5. Shell Programming and Scripting

Passing the nawk variables to the shell

nawk '($1 ~ "1000") && ($1 ~ "5665" ) { sub ($6,"89");flag =1;print }' old.txt >> new.txt I want to set a flag in awk , if the both conditions are met. I want to pass this flag to shell Can anyone please help me on this (1 Reply)
Discussion started by: prav076
1 Replies

6. Shell Programming and Scripting

Passing Shell Variables in ISQL

Hi.. I am passing a variable in my shell function. I need to access it for an isql comand in the shell script. However the isql o/p gives no results if i pass a variable in the command. The isql command works perfectly fine if i hardcore the table name. My script is : ... (0 Replies)
Discussion started by: dikki
0 Replies

7. Shell Programming and Scripting

Passing Arguments in Shell Scripts

Hello everybody! First time posting here:) Right, I am trying to pass arguments in my shell scripts using $1, $2 and $3 etc using if else statement........ This is my shell script which is based on serching the google website #!/bin/sh wget -t1 -E -e robots=off - -awGet.log -T 200 -H... (47 Replies)
Discussion started by: kev_1234
47 Replies

8. Shell Programming and Scripting

Passing variables between scripts

Hi all. I need to pass a value from a script that runs in a sub-shell, back into the calling shell like below (or into script 2 directly): outer_script export param=value1 script1 $param (in script1: export param=value2) script2 $param ($param is now value1, not value2... (4 Replies)
Discussion started by: bbergstrom74
4 Replies

9. UNIX for Dummies Questions & Answers

Passing variables between scripts...

Hey all, I'm wondering how you pass variable's that are defined in one script to another script that's been called by that first script..... Best regards, Jaz (1 Reply)
Discussion started by: Jazmania
1 Replies

10. Shell Programming and Scripting

passing two variables into a shell script?

Hello all, i have a infile.txt text file which contains such variables: aaa 123 asds 1323 asdsd 13434 lkjlkj 324324 23432 lkjlkj 24324 ljlkj 3j4lk 234kj3 and i want to pass them to my script such as: ./myscript $1 $2 where $1 is the first value in the first row and $2 is the second... (2 Replies)
Discussion started by: Bashar
2 Replies
Login or Register to Ask a Question