0

For the below script, list of elements in bash array is printed as expected within the loop, but when the same is printed outside the loop, its not. Can you please provide pointers on what is being missed?

pid_arr=()
pid=10
pid_arr+=($pid)
pid_arr+=(29)

ls -1 | head -2 | while read file
do
    ls -1 1>/dev/null 2>/dev/null &
    pid=$!
    echo "Pid: $pid"
    pid_arr+=("${pid}")

    echo "Within loop"
    echo "${pid_arr[@]}"
done

echo "${pid_arr[@]}"

for pid in "${pid_arr[@]}"
do
   echo "Checking pid: $pid"
done

Output for the above script

Pid: 10817
Within loop
10 29 10817
Pid: 10818
Within loop
10 29 10817 10818
10 29
Checking pid: 10
Checking pid: 29

Was expecting to see all the 4 values : 10, 29, pid1, pid2 But only 10, 29 are listed and not the pid values.

2

1 Answer 1

1

Modified code based on recommendation (removed subshell usage) and working as expected.

pid_arr=()
pid=10
pid_arr+=($pid)
pid_arr+=(29)

tmp_file=abc-`date +%s`
ls -1 | head -2 > $tmp_file
while read file
do
    ls -1 1>/dev/null 2>/dev/null &
    pid=$!
    echo "Pid: $pid"
    pid_arr+=("${pid}")

    echo "Within loop"
    echo "${pid_arr[@]}"
done < $tmp_file

echo "Outside"
echo "${pid_arr[@]}"

for pid in "${pid_arr[@]}"
do
   echo "Checking pid: $pid"
done

rm $tmp_file

Output

Pid: 11122
Within loop
10 29 11122
Pid: 11123
Within loop
10 29 11122 11123
Outside
10 29 11122 11123
Checking pid: 10
Checking pid: 29
Checking pid: 11122
Checking pid: 11123
3
  • 3
    Note: you can avoid using a temporary file with ... done < <(ls -1 | head -2). Commented Dec 21, 2024 at 11:03
  • It's time to stop using the archaic backtick syntax for command substitution, and use $(...).
    – Barmar
    Commented Dec 21, 2024 at 22:18
  • It's also time to start checking your code with shellcheck.net - that would have solved your original problem and will tell you about problems in your new script that you may not be aware of yet.
    – Ed Morton
    Commented Dec 22, 2024 at 22:27

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.