FOR(1)
β¨ FOR(1) β the fish-shell loop that eats π
okay bestie so this is for from the fish-shell manual set, page one of the iconic franchise, and honestly? it's giving "tiny syntax, huge power" π₯ no cap, this is the little loop that could and it's about to carry your whole script fr fr.
πΈ SYNOPSIS πΈ
for VARNAME in [VALUES ...]; COMMANDS ...; end
like, that's it. that's the whole vibe check. π
π DESCRIPTION π
so for is a loop construct β it runs the commands you give it in COMMANDS over and over, multiple times, once for each value. on every single iteration, the local variable named by VARNAME gets handed a brand-new value from VALUES. slay.
and if VALUES is empty? then COMMANDS literally does not get executed at all. not once. zero. π empty list = no iterations, periodt.
here's the part people always get confused about (iykyk): VARNAME is visible after the loop terminates, and it holds the last value that got assigned to it. the variable doesn't just vanish into the void when end hits, okay? π
and if VARNAME doesn't already exist? it gets set in the local scope. for our purposes though, the rules go like this:
- if the
forblock is inside a function, there must be a local variable with the same name. β¨ - if the
forblock is not nested inside a function, then global and universal variables with the same name will be used, if they exist. π¦
also much like set, for does not modify $status β but the evaluation of the commands underneath it totally can. so keep an eye on that bestie. π
and if you're ever stuck (no shame, we've all been there), the -h or --help option displays help about using this command. π«Ά
π EXAMPLE π
look at her go:
for i in foo bar baz; echo $i; end
# would output:
foo
bar
baz
iconic. ate and left no crumbs. π
π¦ NOTES π¦
okay so history lesson time and it matters, so listen up: the VARNAME was local to the for block in releases prior to 3.0.0. which means the variable did not survive the loop the way it does now. π
if you did something like this:
for var in a b c
if break_from_loop
break
end
end
echo $var
then the last value assigned to var when the loop terminated would not be available outside the loop. so what echo $var would write depended entirely on whatever var was set to before the loop was run. and in practice? likely nothing. π«
so yeah β old fish, different rules. mind the version, bestie. β
π COPYRIGHT π
fish-shell developers π§
and that's the whole page, bestie! this little beauty is from fish-shell β version 4.9, dated Sep 08, 2026 β so go forth, loop your hearts out, and remember: your VARNAME will still be there for you when it's over. πβ¨π
π¦ where this page comes from
- arch rolling: fish 4.9.3-1 Β· GPL-2.0-only AND BSD-3-Clause AND ISC AND MIT AND PSF-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms