There are multiple ways you can call one shell script from another shell script and this depends on your requirements.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
## Create 1st shell script (child) cat << 'EOF' > child.sh #!/bin/bash a="hello" echo "inside child script" EOF ## Create 2nd shell script (parent) cat << 'EOF' > parent.sh #!/bin/bash CHILD_SCRIPT_PATH="./child.sh" ## Execute child script inside parent script source "$CHILD_SCRIPT_PATH" ## or ## "$CHILD_SCRIPT_PATH" ## or ## . "$CHILD_SCRIPT_PATH" ## or ## bash "$CHILD_SCRIPT_PATH" ## or ## OUTPUT=$("$CHILD_SCRIPT_PATH") ## echo $OUTPUT ## or ## OUTPUT=`"$CHILD_SCRIPT_PATH"` ## echo $OUTPUT ## or ## ("$CHILD_SCRIPT_PATH") ## or ## (exec "$CHILD_SCRIPT_PATH") b="world" echo "inside parent script" echo "variable from child script: $a" echo "variable from parent script: $b" EOF ## Execute the parent script ./parent.sh ## Returns ## inside child script ## inside parent script ## variable from child script: hello ## variable from parent script: world |