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 0 and else is 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 πŸ‘‘

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