sanjeewa
sanjeewa

Reputation: 604

Pass specific variables from one shell script to another?

I have a bash script named test.sh with:

#!/bin/bash

var1=hello 
sh test2.sh $var1="/var/log"

My test2.sh looks like this:

#!/bin/bash
function jumpto
{
    label=$1
    cmd=$(sed -n "/$label:/{:a;n;p;ba};" $0 | grep -v ':$')
    eval "$cmd"
    exit
}


start=${1:-"start"}

jumpto $start

start:
echo "variable -- " $var1

This does not work due to I used jumpto function. When execution, always $var1 assigned to the $1 variable in jumpto function.

Is there a different way to do this.?

Upvotes: 1

Views: 1997

Answers (1)

Dudi Boy
Dudi Boy

Reputation: 4865

We suggest to learn about environment variable and export command and their scope.

Try modify your test.sh

#!/bin/bash

export var1=hello 
sh test2.sh $var1="/var/log"

Upvotes: 2

Related Questions