IF(1)
π NAME π
okay so welcome to <pink>if</pink>, bestie β genuinely one of the most iconic little keywords in the whole fish-shell manual (this is page IF(1), section 1, and yes she has her own page, main character energy fr). what does she do? conditionally execute a command. that's it. that's the whole vibe. π¦
β¨ SYNOPSIS β¨
like, here's the shape of it, no cap:
if CONDITION; COMMANDS_TRUE ...;
[else if CONDITION2; COMMANDS_TRUE2 ...;]
[else; COMMANDS_FALSE ...;]
end
the square brackets are the "only if you feel like it" parts π
π DESCRIPTION π
okay so listen. if runs the command <italic>CONDITION</italic>. and then it does a little vibe check on the exit status:
- if the condition's exit status is
0β congrats bestie, it slayed β the commands<italic>COMMANDS_TRUE</italic>will execute πΈ - if the exit status is not
0andelseis given, then<italic>COMMANDS_FALSE</italic>gets executed instead π
you can also use not, and, and or in the condition β icons only. peep the second example below for that one, iykyk π
and huge tip: the exit status of the last foreground command to exit can always be accessed using the $status variable. always. forever. she's there for you π«Ά
oh and also β the -h or --help option displays help about using this command. cute little emergency exit if you forget stuff.
π₯ EXAMPLE π₯
here's the first one. this code will print foo.txt exists if the file foo.txt exists and is a regular file β otherwise it'll print bar.txt exists if the file bar.txt exists and is a regular file β otherwise it prints foo.txt and bar.txt do not exist. it's giving cascade. it's giving fallback. it's giving ate π
if test -f foo.txt
echo foo.txt exists
else if test -f bar.txt
echo bar.txt exists
else
echo foo.txt and bar.txt do not exist
end
and example two, the one with the and in it β this prints "foo.txt exists and is readable" if foo.txt is a regular file and readable. two conditions, one icon β¨
if test -f foo.txt
and test -r foo.txt
echo "foo.txt exists and is readable"
end
π SEE ALSO π
ngl, if is only as useful as the command used as the condition β she's the hype woman, not the whole show. fish ships a few for you, and they're all baddies:
testβ perform tests on files and text; can compare numbers, strings and check paths βstringβ manipulate strings; can perform string operations including wildcard and regular expression matches π§pathβ manipulate and check paths; can check paths for permissions, existence or type π¦containsβ test if a word is present in a list; can check if an element is in a list π
π Copyright π
fish-shell developers β kings, legends, the blueprint π
okay that's the whole page, go write some if blocks and make her proud ππ
and a lil sign-off from me to you: this was version 4.9, dated Sep 08, 2026 β screenshot it, remember it, tattoo it, whatever, love u bye π«Άβ¨
π¦ 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