Find array length in zsh script












1















Is there a way to find the length of the array *(files names) in zsh without using a for loop to increment some variable?



I naively tried echo ${#*[@]} but it didn't work. (bash syntax are welcome as well)










share|improve this question









New contributor




Cristiano is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 1





    By "length of the array" do you mean its number of elements? And do you want to get this without actually defining this array (* suggest you want to use shell globing mechanism here)?

    – jimmij
    37 mins ago













  • Oops, you're right i should have asked the other way around, I'll edit it.

    – Cristiano
    25 mins ago













  • @Cristiano: zsh doesn't have anything to do with it. * is not an array in the way you are using it, it is a shell glob. Arrays have nothing to do with your question unless you create an array as Jeff did in his answer. Your question is "How do I find how many files are in the current directory"

    – Jesse_b
    24 mins ago













  • @Jess_b But it acts like an array don't you think? echo *[0] in zsh prints the 1st file name...

    – Cristiano
    5 mins ago


















1















Is there a way to find the length of the array *(files names) in zsh without using a for loop to increment some variable?



I naively tried echo ${#*[@]} but it didn't work. (bash syntax are welcome as well)










share|improve this question









New contributor




Cristiano is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 1





    By "length of the array" do you mean its number of elements? And do you want to get this without actually defining this array (* suggest you want to use shell globing mechanism here)?

    – jimmij
    37 mins ago













  • Oops, you're right i should have asked the other way around, I'll edit it.

    – Cristiano
    25 mins ago













  • @Cristiano: zsh doesn't have anything to do with it. * is not an array in the way you are using it, it is a shell glob. Arrays have nothing to do with your question unless you create an array as Jeff did in his answer. Your question is "How do I find how many files are in the current directory"

    – Jesse_b
    24 mins ago













  • @Jess_b But it acts like an array don't you think? echo *[0] in zsh prints the 1st file name...

    – Cristiano
    5 mins ago
















1












1








1








Is there a way to find the length of the array *(files names) in zsh without using a for loop to increment some variable?



I naively tried echo ${#*[@]} but it didn't work. (bash syntax are welcome as well)










share|improve this question









New contributor




Cristiano is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












Is there a way to find the length of the array *(files names) in zsh without using a for loop to increment some variable?



I naively tried echo ${#*[@]} but it didn't work. (bash syntax are welcome as well)







bash shell-script zsh bash-array






share|improve this question









New contributor




Cristiano is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Cristiano is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 24 mins ago







Cristiano













New contributor




Cristiano is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 46 mins ago









CristianoCristiano

63




63




New contributor




Cristiano is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Cristiano is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Cristiano is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 1





    By "length of the array" do you mean its number of elements? And do you want to get this without actually defining this array (* suggest you want to use shell globing mechanism here)?

    – jimmij
    37 mins ago













  • Oops, you're right i should have asked the other way around, I'll edit it.

    – Cristiano
    25 mins ago













  • @Cristiano: zsh doesn't have anything to do with it. * is not an array in the way you are using it, it is a shell glob. Arrays have nothing to do with your question unless you create an array as Jeff did in his answer. Your question is "How do I find how many files are in the current directory"

    – Jesse_b
    24 mins ago













  • @Jess_b But it acts like an array don't you think? echo *[0] in zsh prints the 1st file name...

    – Cristiano
    5 mins ago
















  • 1





    By "length of the array" do you mean its number of elements? And do you want to get this without actually defining this array (* suggest you want to use shell globing mechanism here)?

    – jimmij
    37 mins ago













  • Oops, you're right i should have asked the other way around, I'll edit it.

    – Cristiano
    25 mins ago













  • @Cristiano: zsh doesn't have anything to do with it. * is not an array in the way you are using it, it is a shell glob. Arrays have nothing to do with your question unless you create an array as Jeff did in his answer. Your question is "How do I find how many files are in the current directory"

    – Jesse_b
    24 mins ago













  • @Jess_b But it acts like an array don't you think? echo *[0] in zsh prints the 1st file name...

    – Cristiano
    5 mins ago










1




1





By "length of the array" do you mean its number of elements? And do you want to get this without actually defining this array (* suggest you want to use shell globing mechanism here)?

– jimmij
37 mins ago







By "length of the array" do you mean its number of elements? And do you want to get this without actually defining this array (* suggest you want to use shell globing mechanism here)?

– jimmij
37 mins ago















Oops, you're right i should have asked the other way around, I'll edit it.

– Cristiano
25 mins ago







Oops, you're right i should have asked the other way around, I'll edit it.

– Cristiano
25 mins ago















@Cristiano: zsh doesn't have anything to do with it. * is not an array in the way you are using it, it is a shell glob. Arrays have nothing to do with your question unless you create an array as Jeff did in his answer. Your question is "How do I find how many files are in the current directory"

– Jesse_b
24 mins ago







@Cristiano: zsh doesn't have anything to do with it. * is not an array in the way you are using it, it is a shell glob. Arrays have nothing to do with your question unless you create an array as Jeff did in his answer. Your question is "How do I find how many files are in the current directory"

– Jesse_b
24 mins ago















@Jess_b But it acts like an array don't you think? echo *[0] in zsh prints the 1st file name...

– Cristiano
5 mins ago







@Jess_b But it acts like an array don't you think? echo *[0] in zsh prints the 1st file name...

– Cristiano
5 mins ago












1 Answer
1






active

oldest

votes


















1














files=(*)
printf 'There are %d filesn' "${#files[@]}"


or



set -- *
printf 'There are %d filesn' "$#"


You have to name the array first (as I did above with files) or use the built-in array $@ by populating it with the wildcard, as I did in the second example. In the former, the "length" (number of files) of the array is done with the ${#arrayname[@]} syntax. The number of elements inn the built-in array is in $#.






share|improve this answer
























  • So the * acts as a regex expression? I thought that it was a special array... although surprise me that there is no such array...

    – Cristiano
    14 mins ago











  • wait, in zsh *[0] or *[1] gives me some file name... now I'm puzzled.

    – Cristiano
    11 mins ago











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
});


}
});






Cristiano is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f499595%2ffind-array-length-in-zsh-script%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









1














files=(*)
printf 'There are %d filesn' "${#files[@]}"


or



set -- *
printf 'There are %d filesn' "$#"


You have to name the array first (as I did above with files) or use the built-in array $@ by populating it with the wildcard, as I did in the second example. In the former, the "length" (number of files) of the array is done with the ${#arrayname[@]} syntax. The number of elements inn the built-in array is in $#.






share|improve this answer
























  • So the * acts as a regex expression? I thought that it was a special array... although surprise me that there is no such array...

    – Cristiano
    14 mins ago











  • wait, in zsh *[0] or *[1] gives me some file name... now I'm puzzled.

    – Cristiano
    11 mins ago
















1














files=(*)
printf 'There are %d filesn' "${#files[@]}"


or



set -- *
printf 'There are %d filesn' "$#"


You have to name the array first (as I did above with files) or use the built-in array $@ by populating it with the wildcard, as I did in the second example. In the former, the "length" (number of files) of the array is done with the ${#arrayname[@]} syntax. The number of elements inn the built-in array is in $#.






share|improve this answer
























  • So the * acts as a regex expression? I thought that it was a special array... although surprise me that there is no such array...

    – Cristiano
    14 mins ago











  • wait, in zsh *[0] or *[1] gives me some file name... now I'm puzzled.

    – Cristiano
    11 mins ago














1












1








1







files=(*)
printf 'There are %d filesn' "${#files[@]}"


or



set -- *
printf 'There are %d filesn' "$#"


You have to name the array first (as I did above with files) or use the built-in array $@ by populating it with the wildcard, as I did in the second example. In the former, the "length" (number of files) of the array is done with the ${#arrayname[@]} syntax. The number of elements inn the built-in array is in $#.






share|improve this answer













files=(*)
printf 'There are %d filesn' "${#files[@]}"


or



set -- *
printf 'There are %d filesn' "$#"


You have to name the array first (as I did above with files) or use the built-in array $@ by populating it with the wildcard, as I did in the second example. In the former, the "length" (number of files) of the array is done with the ${#arrayname[@]} syntax. The number of elements inn the built-in array is in $#.







share|improve this answer












share|improve this answer



share|improve this answer










answered 30 mins ago









Jeff SchallerJeff Schaller

41.1k1056131




41.1k1056131













  • So the * acts as a regex expression? I thought that it was a special array... although surprise me that there is no such array...

    – Cristiano
    14 mins ago











  • wait, in zsh *[0] or *[1] gives me some file name... now I'm puzzled.

    – Cristiano
    11 mins ago



















  • So the * acts as a regex expression? I thought that it was a special array... although surprise me that there is no such array...

    – Cristiano
    14 mins ago











  • wait, in zsh *[0] or *[1] gives me some file name... now I'm puzzled.

    – Cristiano
    11 mins ago

















So the * acts as a regex expression? I thought that it was a special array... although surprise me that there is no such array...

– Cristiano
14 mins ago





So the * acts as a regex expression? I thought that it was a special array... although surprise me that there is no such array...

– Cristiano
14 mins ago













wait, in zsh *[0] or *[1] gives me some file name... now I'm puzzled.

– Cristiano
11 mins ago





wait, in zsh *[0] or *[1] gives me some file name... now I'm puzzled.

– Cristiano
11 mins ago










Cristiano is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















Cristiano is a new contributor. Be nice, and check out our Code of Conduct.













Cristiano is a new contributor. Be nice, and check out our Code of Conduct.












Cristiano is a new contributor. Be nice, and check out our Code of Conduct.
















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%2f499595%2ffind-array-length-in-zsh-script%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

Entries order in /etc/network/interfaces

新発田市

Grub takes very long (several minutes) to open Menu (in Multi-Boot-System)