Split string by space in ZSH

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












1















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.










share|improve this question
























  • Can you use the cut binary? It is not a shell built-in, but it will do what you want via cut -d ' ' -f 1 file.txt.

    – Jeremy Dover
    Feb 14 at 5:10











  • @JeremyDover It would work for this case, but I want something more robust, hence asking for an array.

    – user137369
    Feb 14 at 5:17
















1















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.










share|improve this question
























  • Can you use the cut binary? It is not a shell built-in, but it will do what you want via cut -d ' ' -f 1 file.txt.

    – Jeremy Dover
    Feb 14 at 5:10











  • @JeremyDover It would work for this case, but I want something more robust, hence asking for an array.

    – user137369
    Feb 14 at 5:17














1












1








1








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.










share|improve this question
















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.







bash zsh array read






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 14 at 5:07







user137369

















asked Feb 14 at 4:49









user137369user137369

17718




17718












  • Can you use the cut binary? It is not a shell built-in, but it will do what you want via cut -d ' ' -f 1 file.txt.

    – Jeremy Dover
    Feb 14 at 5:10











  • @JeremyDover It would work for this case, but I want something more robust, hence asking for an array.

    – user137369
    Feb 14 at 5:17


















  • Can you use the cut binary? It is not a shell built-in, but it will do what you want via cut -d ' ' -f 1 file.txt.

    – Jeremy Dover
    Feb 14 at 5:10











  • @JeremyDover It would work for this case, but I want something more robust, hence asking for an array.

    – user137369
    Feb 14 at 5:17

















Can you use the cut binary? It is not a shell built-in, but it will do what you want via cut -d ' ' -f 1 file.txt.

– Jeremy Dover
Feb 14 at 5:10





Can you use the cut binary? It is not a shell built-in, but it will do what you want via cut -d ' ' -f 1 file.txt.

– Jeremy Dover
Feb 14 at 5:10













@JeremyDover It would work for this case, but I want something more robust, hence asking for an array.

– user137369
Feb 14 at 5:17






@JeremyDover It would work for this case, but I want something more robust, hence asking for an array.

– user137369
Feb 14 at 5:17











1 Answer
1






active

oldest

votes


















3














From man zshbuiltins, zsh's read uses -A instead.



read [ -rszpqAclneE ] [ -t [ num ] ] [ -k [ num ] ] [ -d delim ]
[ -u n ] [ name[?prompt] ] [ name ... ]
...
-A The first name is taken as the name of an array
and all words are assigned to it.


Hence the command is



while IFS=' ' read -A args; do
echo "$args[1]"
done < file.txt


N.B. by default, zsh array numbering begins with 1, whereas bash's begins with 0.



$ man zshparam
...
Array Subscripts
...
The elements are numbered beginning with 1, unless the
KSH_ARRAYS option is set in which case they are numbered from zero.





share|improve this answer




















  • 1





    You are correct. I had tried -A but it did not seem to be working. It was a combination of the 1-based index plus the differences in slicing that were tripping me up.

    – user137369
    Feb 14 at 5:29










Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f500537%2fsplit-string-by-space-in-zsh%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














From man zshbuiltins, zsh's read uses -A instead.



read [ -rszpqAclneE ] [ -t [ num ] ] [ -k [ num ] ] [ -d delim ]
[ -u n ] [ name[?prompt] ] [ name ... ]
...
-A The first name is taken as the name of an array
and all words are assigned to it.


Hence the command is



while IFS=' ' read -A args; do
echo "$args[1]"
done < file.txt


N.B. by default, zsh array numbering begins with 1, whereas bash's begins with 0.



$ man zshparam
...
Array Subscripts
...
The elements are numbered beginning with 1, unless the
KSH_ARRAYS option is set in which case they are numbered from zero.





share|improve this answer




















  • 1





    You are correct. I had tried -A but it did not seem to be working. It was a combination of the 1-based index plus the differences in slicing that were tripping me up.

    – user137369
    Feb 14 at 5:29















3














From man zshbuiltins, zsh's read uses -A instead.



read [ -rszpqAclneE ] [ -t [ num ] ] [ -k [ num ] ] [ -d delim ]
[ -u n ] [ name[?prompt] ] [ name ... ]
...
-A The first name is taken as the name of an array
and all words are assigned to it.


Hence the command is



while IFS=' ' read -A args; do
echo "$args[1]"
done < file.txt


N.B. by default, zsh array numbering begins with 1, whereas bash's begins with 0.



$ man zshparam
...
Array Subscripts
...
The elements are numbered beginning with 1, unless the
KSH_ARRAYS option is set in which case they are numbered from zero.





share|improve this answer




















  • 1





    You are correct. I had tried -A but it did not seem to be working. It was a combination of the 1-based index plus the differences in slicing that were tripping me up.

    – user137369
    Feb 14 at 5:29













3












3








3







From man zshbuiltins, zsh's read uses -A instead.



read [ -rszpqAclneE ] [ -t [ num ] ] [ -k [ num ] ] [ -d delim ]
[ -u n ] [ name[?prompt] ] [ name ... ]
...
-A The first name is taken as the name of an array
and all words are assigned to it.


Hence the command is



while IFS=' ' read -A args; do
echo "$args[1]"
done < file.txt


N.B. by default, zsh array numbering begins with 1, whereas bash's begins with 0.



$ man zshparam
...
Array Subscripts
...
The elements are numbered beginning with 1, unless the
KSH_ARRAYS option is set in which case they are numbered from zero.





share|improve this answer















From man zshbuiltins, zsh's read uses -A instead.



read [ -rszpqAclneE ] [ -t [ num ] ] [ -k [ num ] ] [ -d delim ]
[ -u n ] [ name[?prompt] ] [ name ... ]
...
-A The first name is taken as the name of an array
and all words are assigned to it.


Hence the command is



while IFS=' ' read -A args; do
echo "$args[1]"
done < file.txt


N.B. by default, zsh array numbering begins with 1, whereas bash's begins with 0.



$ man zshparam
...
Array Subscripts
...
The elements are numbered beginning with 1, unless the
KSH_ARRAYS option is set in which case they are numbered from zero.






share|improve this answer














share|improve this answer



share|improve this answer








edited Feb 14 at 5:18

























answered Feb 14 at 5:10









SparhawkSparhawk

10.2k74398




10.2k74398







  • 1





    You are correct. I had tried -A but it did not seem to be working. It was a combination of the 1-based index plus the differences in slicing that were tripping me up.

    – user137369
    Feb 14 at 5:29












  • 1





    You are correct. I had tried -A but it did not seem to be working. It was a combination of the 1-based index plus the differences in slicing that were tripping me up.

    – user137369
    Feb 14 at 5:29







1




1





You are correct. I had tried -A but it did not seem to be working. It was a combination of the 1-based index plus the differences in slicing that were tripping me up.

– user137369
Feb 14 at 5:29





You are correct. I had tried -A but it did not seem to be working. It was a combination of the 1-based index plus the differences in slicing that were tripping me up.

– user137369
Feb 14 at 5:29

















draft saved

draft discarded
















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f500537%2fsplit-string-by-space-in-zsh%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown






Popular posts from this blog

How to check contact read email or not when send email to Individual?

Displaying single band from multi-band raster using QGIS

How many registers does an x86_64 CPU actually have?