Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to does a string array work in bash script?

I am new to scripts, and I want to monitor top 2 process with high memory usage, and keep lists of processes that are not mysqld.

I wrote a script below,

lines=$(ps aux --sort rss | head -n 3)
echo $lines
for line in $lines
do
    echo $line
done

and found out that it prints words instead of lines.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

How can I save a line in the variable line?

>Solution :

You are not creating an array at all. The syntax for that would look like

lines=($(ps aux --sort rss | head -n 3))

where the inner $(...) is a command substitution which simply expands to a sequence of tokens (not lines!) and the outer parentheses declare an array.

Probably a much better solution entirely is to just loop over the output from ps directly. Collecting the results in an array is only useful if you need to traverse the values repeatedly for some reason (like sort them by different criteria, or loop over them multiple times).

ps aux --sort rss | head -n 3

or if you want to decorate the output further, perhaps

ps aux --sort rss | head -n 3 |
while IFS= read -r line; do
    echo "the output was $line with proper quoting"
done

(though with a static decoration around the value, maybe simply prefer ps aux --sort rss | head -n 3 | sed 's/.*/the output was & with some decorations/').

With recent versions of Bash, you can read the values into an array with readarray -t.

readarray -t lines < <(ps aux --sort rss | head -n 3)
for line in "${lines[@]}"; do  # notice proper array syntax
   echo "$line"                # notice proper quoting
done

The array syntax is slightly odd, but needs to be backwards-compatible with the original Bourne shell, where $lines will always refer to a single string value.

(readarray is Bash v4+ so not available e.g. on MacOS out of the box currently.)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading