Languages
[Edit]
EN

Bash - pass variables from one script to second one

10 points
Created by:
Root-ssh
98350

In this short article we would like to show how in Bash pass variables from one script to second one.

Quick solution (call child script following way):

. path/to/child/script.sh

Note: . exports all script variables to child script.

Practcal example

To see how it works we can use below example that needs to create in same directory 2 sh files.

Do following steps:

1. create scripts

a.sh file:

#!/bin/bash

TEST=5

echo "a.sh: ${TEST}"

. ./b.sh

b.sh file:

#!/bin/bash

echo "b.sh: ${TEST}"

2. add execution permissions for files

chmod a+x a.sh
chmod a+x b.sh

3. run a.sh script

./a.sh

Output:

a.sh: 5
b.sh: 5

Bash

Bash - pass variables from one script to second one
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join