alternative to readarray, because it does not work on mac os x

You could use a read loop.

while IFS=\= read var value; do
    vars+=($var)
    values+=($value)
done < VarsValues.txt

You can use declare builtin:

declare -a vars=( $(cut -d '=' -f1 varsValues.txt) )
declare -a values=( $(cut -d '=' -f2 varsValues.txt) )

Although, as commenters have pointed out declare -a is superfluous.

vars=( $(cut -d '=' -f1 varsValues.txt) )
values=( $(cut -d '=' -f2 varsValues.txt) )

Works just as well.


Here's the awk version. Note that NPars is not hardcoded.

vars=($(awk -F= '{print $1}' varsValues.txt))
values=($(awk -F= '{print $2}' varsValues.txt))

Npars=${#vars[@]}

for ((i=0; i<$Npars; i++)); do
    eval ${vars[$i]}=${values[$i]}
done

echo $people

Tags:

Macos

Bash

Perl

Awk