-1
OS: Ubuntu 18.04
Bash

I am trying to get one shell script to pass variables to another script and execute it. Here's what I tried:

mainscript.sh

#!/bin/bash
# Master script
SUBSCRIPT1_PATH=~/subscript1.sh
test_string="The cat ate the canary"
(exec "$SUBSCRIPT1_PATH")

subscript1.sh:

#!/bin/bash
# subscript1.sh
echo $test_string

But, when I do:

bash mainscript.sh

I get nothing. Any ideas on how to do this?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
EastsideDev
  • 6,257
  • 9
  • 59
  • 116

1 Answers1

2

Shell variables are by default not visible in child processes. To pass them to children use the export keyword:

#!/bin/bash
# Master script
SUBSCRIPT1_PATH=~/subscript1.sh
export test_string="The cat ate the canary"
(exec "$SUBSCRIPT1_PATH")
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Pacifist
  • 3,025
  • 2
  • 12
  • 20