ASE
ASE

Reputation: 1850

Pass array variable to and from a nested bash script

I have a main script (main.bash)

#!/bin/bash
var=10                 #Variable to pass to the nested script
./nested.bash $var     #Pass 'var' to the nested script, which outputs the 'list' array
echo $list             #echo the output of the nested script

The nested script (nested.bash) is:

#!/bin/bash
if [ $1 == 10 ] ; then    
    list=(10-10 20-01 30-03 40-01 50-05)
fi
export list

I used export, but it is not working. How can 'nested.bash' pass list array to 'main.bash'? More generally, how can this be done?

Upvotes: 0

Views: 484

Answers (2)

John Kugelman
John Kugelman

Reputation: 361615

The simplest solution is to source the script with .. This runs it in the current shell process instead of in a child process, which means the outer script can see the changes made by the nested script.

main

#!/bin/bash
var=10
. ./nested.bash "$var"
echo "${list[@]}"

nested.bash

if [ "$1" == 10 ]; then    
    list=(10 20 30 40 50)
fi

Notes:

  • Executables should generally not have an extension. The caller doesn't need to know if the script is written in bash or python or is a compiled C program. Prefer main to main.bash.

  • Sourced scripts, on the other hand, should have an extension since they need to be executed in a particular interpreter. Name the inner script nested.bash.

  • Always quote variable expansions.

  • Array expansion is "${list[@]}". If you write $list you'll only get the first element.

  • You don't have to pass "$var" in and reference it as "$1" in the nested script. You can reference "$var" directly if you like.

Upvotes: 2

Barmar
Barmar

Reputation: 781004

You can't export variables to a parent process. export is only used to pass variables to child processes. It's also not possible to export arrays.

The way to send data back to the parent process is through standard output, using command substitution.

main.bash:

#!/bin/bash
var=10                 
list=($(./nested.bash "$var"))
echo "${list[@]}"

nested.bash:

#!/bin/bash
if [ $1 == 10 ] ; then    
    list=(10 20 30 40 50)
fi
echo "${list[@]}"

Upvotes: 1

Related Questions