Search code examples
arrayslinuxbashdebiandpkg

Debian output from installed packages in bash array


Sorry for maybe a noob question but I am pretty new to Linux. I am trying to get all output from following command in an array and loop through it (in bash).

packages=$(dpkg-query --show --showformat='${Package}' )

for p in "${packages[@]}"; do
        echo "Package: ${p}"
done

I don't know what I am doing wrong, but maybe someone can help me out.

Thanks in advance.

Greetings MC

Thanks for you help. my solution now is this:

while IFS= read  -r line; do
        echo "${line}"
done <<< "$(dpkg-query --show --showformat='${Package}\n')"

Solution

  • A number of things.

    First

    packages=$(dpkg-query --show --showformat='${Package};'
    

    doesn't create an array.

    It creates a string. You need () around the command to split it into an array.

    Second:

    Even if you were using an array there, you wouldn't want to do this this way.

    See Bash FAQ 001 for appropriate ways to read data from a file/command/etc. line-by-line.