-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?

3
  • 1
    (exec "$SUBSCRIPT1_PATH") is equivalent to "$SUBSCRIPT1_PATH" Commented Sep 12, 2019 at 17:23
  • 2
    The parentheses are not needed around your exec statement, by the way. Commented Sep 12, 2019 at 17:26
  • 1
    Use source command or export. Commented Sep 12, 2019 at 17:26

1 Answer 1

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")
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.