SWITCH(1)

✨ SWITCH(1) ✨

okay bestie, welcome to switch β€” the fish-shell era of "pick a lane and slay" πŸ’… this is like the conditional block queen of the shell, and yes she's iconic.


🌸 NAME 🌸

switch β€” conditionally execute a block of commands. lowkey it's a whole vibe check for your code, no cap πŸ’–


πŸ¦‹ SYNOPSIS πŸ¦‹

switch VALUE; [case [GLOB ...]; [COMMANDS ...]; ...] end

πŸŽ€ DESCRIPTION πŸŽ€

okay so switch performs one of several blocks of commands, depending on whether a specified value equals one of several globbed values. it's giving "choose your own adventure" but make it shell ✨. case is used together with the switch statement in order to determine which block should be executed β€” like, they're a package deal, iykyk 🫢.

each case command is given one or more parameters. the first case command with a parameter that matches the string specified in the switch command will be evaluated. periodt. case parameters may contain globs. these need to be escaped or quoted in order to avoid regular glob expansion using filenames β€” like, the shell will try to expand them against all your files otherwise, and that's a mess 😭. so quote them, bestie.

πŸ’€ note that fish does not fall through on case statements. only the first matching case is executed. no accidental cascade, we respect boundaries here.
🩷 note that break cannot be used to exit a case/switch block early like in other languages. it can only be used in loops. so don't try it, she will not hear you πŸ™ˆ.
πŸ“ note that command substitutions in a case statement will be evaluated even if its body is not taken. all substitutions, including command substitutions, must be performed before the value can be compared against the parameter. like, everyone gets evaluated upfront, no skipping the queue βœ‹.

🌈 EXAMPLE 🌈

if the variable $animal contains the name of an animal, the following code would attempt to classify it:

switch $animal
    case cat
        echo evil
    case wolf dog human moose dolphin whale
        echo mammal
    case duck goose albatross
        echo bird
    case shark trout stingray
        echo fish
    case '*'
        echo I have no idea what a $animal is
end

if the above code was run with $animal set to whale, the output would be mammal. slay πŸ‹πŸ”₯


fish-shell developers πŸ’–


and that's the whole page, bestie! this one's from the fish-shell developers, version 4.9, dated Sep 08, 2026 β€” go forth and switch responsibly πŸ’œπŸ§šβ­

πŸ“¦ 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