3 ms·
Just to illustrate for others, in fish that would be for i in (seq 10); echo $i; end or usually written as for i in (seq 10) echo $i end and that
by cube2222 3y ago
Just to illustrate for others, in fish that would be
for i in (seq 10); echo $i; end
or usually written as
for i in (seq 10)
echo $i
end
and that is already based on iteration, so for iterating over files, you'll similarly use
for f in (ls); echo $f; end
and it works the same for arrays.
- user982 3y agoYour example in fish: for i in (seq 10); echo $i; end directly translated to bash: for i in $(seq 10); do echo $i; done
- bravetraveler 3y agoNo need for seq :) for i in {1..10}; do echo $i ; done If, for whatever reason, you want leading zeroes - BASH will respect that. Do {01..10}
- shmerl 3y agoseq would work in bash too, but it's not part of the language, it's an external tool. C like syntax is more expressive I'd say for simple numerical increments.
- deleted 3y ago[deleted]
- paradox460 3y agoYou don't even need the (ls) for file iteration: for i in * echo $i end Fish also has globstar out of the box, so you can do for i in **/* echo $i end
- fuzztester 3y ago[You don't even need the (ls) for file iteration: for i in * echo $i end ] You don't even need the for for that (specific example): echo * at least in sh and bash.
- fuzztester 3y agoAlso, IIRC, and less commonly seen, you can do: for i do # your commands using $i here done which will iterate over all the command line arguments to the script containing that for loop. It is a shortcut for: for i in $* # note: not for i in * # the rest is the same above This is mentioned in either the shell man page or in the Kernighan & Pike book The Unix Programming Environment.
- j16sdiz 3y agoecho * does not have the newline after each file
- fuzztester 3y agoTrue, echo * will print all the filenames on one line, with a space between pairs, but for data processing purposes, the two snippets are equivalent, because as per shell behavior, newline, tab and space are all treated as space, unless quoted. Like the definition of whitespace in the K&R C book. After all, Unix is written in C.
- cdrt 3y agoYou wouldn’t iterate over `echo *` anyway. This works just fine in bash/sh: for file in *; do echo "$file" done