Give this file.txt
:
first line
second line
third line
This works in bash
:
while IFS=' ' read -a args; do
echo "${args[0]}"
done < file.txt
To produce
first
second
third
That is to say, we were able to read the file line by line, and on each one we split the line further into an array using space as a delimiter. But in zsh
, the result is an error: read: bad option: -a
.
How can we achieve in zsh
the same as in bash
? I’ve tried several solutions, but I was never able to split a string into an array using spaces as the delimiter.
cut
binary? It is not a shell built-in, but it will do what you want viacut -d ' ' -f 1 file.txt
.