0

How can I get just the filenames into an array using the cat command?

How I've been trying:

array=()
while IFS= read -r -d $'\0'; do
  array+=("$REPLY")
done < <(cat /proc/swaps | grep "swap")

This either grabs all the information from the output into an array, or just doesn't work. How can I successfully get my expected output of [/swapfile, /dev/hda1, /some/other/swap] into an array form using the cat command?

2 Answers 2

1
readarray array < <(awk '/swap/{print $1}' /proc/swaps)
Sign up to request clarification or add additional context in comments.

Comments

0

Bash introduced readarray in version 4 which can take the place of the while read loop. readarray is the solution you want.

here is the syntax

readarray variable < inputfile
echo "${variable[0]}" ' to print the first element in array

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.