Languages
[Edit]
EN

Bash - pass variables from one script to second one

10 points
Created by:
Root-ssh
177650

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

Alternative titles

  1. Bash - export variables from one script to second one
  2. Bash - share variables to child script
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

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