cheat-sheets

Fish

Fish is an interactive shell with autosuggestions, syntax highlighting and sensible defaults out of the box, but its scripting syntax is not POSIX-compatible. Use it as a friendly login shell and keep portable scripts in Bash. Pick between shells with Shell Comparison.

Documentation: Fish Documentation Project: Fish shell

Checked against fish 4.2.1. Examples were run unless marked (unverified).

Not Bash #

BASHFISH
NAME=valueset NAME value
export NAME=valueset -x NAME value
unset NAMEset -e NAME
$(cmd)(cmd) or $(cmd)
cmd1 && cmd2cmd1; and cmd2 (&& also works in current fish)
cmd1 || cmd2cmd1; or cmd2 (|| also works in current fish)
if [[ -f f ]]; then ... fiif test -f f; ...; end
for i in 1 2; do ... donefor i in 1 2; ...; end
$?$status
${PIPESTATUS[@]}$pipestatus
"$@"$argv
{ cmd; }begin; cmd; end
alias ll='ls -l'alias ll 'ls -l' (creates a function)
Unquoted $var is split on whitespaceVariables are never word-split; every variable is a list
~/.bashrc~/.config/fish/config.fish

Fish has no $((...)) arithmetic and no <<EOF here-docs; use math and string (below) for arithmetic and string handling.

Variables #

Every fish variable is a list, indexed from 1.

COMMANDDESCRIPTION
set a x y zSet a list
set -l NAME valueLocal to the current block or function
set -g NAME valueGlobal to this session
set -U NAME valueUniversal: shared by all sessions and kept across restarts
set -x NAME valueExported to child processes (set -gx for global and exported)
set -e NAMEErase
set -q NAMETest whether set (exit status only)
set -S NAMEShow scope, export state and elements
$a[1], $a[-1]First and last element
$a[2..3]Range
count $aNumber of elements
set a $a wAppend
contains y $aExit status 0 if y is in the list; add -i to print its index
echo {a,b}cBrace expansion: ac bc

PATH #

COMMANDDESCRIPTION
fish_add_path /usr/local/binAdd to PATH persistently (stored in fish_user_paths, a universal variable); ignores directories that do not exist
echo $PATHPrints one element per entry, not colon-separated

Functions #

fish
function hi --description "say hi"
    echo hi $argv
end

hi there          # hi there
functions hi      # show the definition
funcsave hi       # save it to ~/.config/fish/functions/hi.fish
COMMANDDESCRIPTION
functionsList functions
functions -e NAMEErase a function
funcsave NAMEWrite a function to its own autoloaded file
funced NAMEEdit a function in $EDITOR
type NAMEWhat NAME is (also shows alias definitions)
command -q NAMEExit status 0 if the command exists (use with and)

Functions in ~/.config/fish/functions/NAME.fish are autoloaded on first use, so there is no need to source them from config.fish.

Abbreviations #

An abbreviation expands in place as you type, so history shows the full command.

COMMANDDESCRIPTION
abbr -a gco "git checkout"Add an abbreviation
abbr --showList them as abbr commands
abbr -e gcoErase one

Control Flow #

fish
if test -f /etc/passwd
    echo file
else if test -d /etc/passwd
    echo directory
end

for i in (seq 3)
    echo $i
end

while true
    break
end

switch $argv[1]
    case "f*"
        echo starts with f
    case "*"
        echo other
end

test supports the same operators as POSIX test (-f, -d, -z, -n, -eq, =). not, and and or are commands that combine exit statuses.

string and math #

string replaces most uses of sed, cut and tr on single values.

COMMANDDESCRIPTION
string upper hello, string lower ABCChange case
string replace -a l L helloReplace all matches (heLLo)
string match "*.txt" a.txtGlob match (prints the match)
string match -r "h(.)l" helloRegex match, prints the match then each capture group
string split , a,b,cSplit into a list, one item per line
string join - a b cJoin arguments with a separator (a-b-c)
string length helloLength in characters
string sub -s 2 -l 2 helloSubstring from position 2, length 2 (el)
string trim " x "Strip surrounding whitespace
string pad -w 6 abPad to width 6 (right-aligned by default)
string repeat -n 2 abRepeat (abab)
math 2 + 3 \* 4Arithmetic (escape * or quote the expression)
math -s0 7 / 2Integer result (3); -s sets the number of decimal places
math "sqrt 16"Functions available, e.g. sqrt
read -l xRead a line into a local variable (echo a | read -l x)

Redirection and Pipes #

COMMANDDESCRIPTION
cmd > file, cmd >> fileStdout to file, append
cmd 2> fileStderr to file
cmd &> fileStdout and stderr to a file
cmd 2>&1 | otherPipe both streams
cmd | tee fileShow and save output
diff (sort a | psub) (sort b | psub)Process substitution equivalent (psub)

Interactive Features #

KEY / COMMANDDESCRIPTION
→ or Ctrl+FAccept the autosuggestion
Alt+→Accept one word of the suggestion (unverified)
Ctrl+RHistory search
Alt+SPrepend sudo to the current command (or the previous one if the line is empty) (unverified)
fish_configBrowser-based theme and prompt picker
historyShow history
history search TEXTSearch history
history delete --contains TEXTDelete matching entries
fish_key_readerShow the key sequence a key press sends
set -U fish_greetingEmpty the welcome message (set it to nothing)
status is-interactiveExit status 0 in an interactive session (guard config.fish with it)
fish_vi_key_bindings, fish_default_key_bindingsSwitch to vi or back to the default bindings

Configuration #

fish
# ~/.config/fish/config.fish
if status is-interactive
    abbr -a gco "git checkout"
    fish_add_path ~/.local/bin
end

Cross-shell tools such as Starship Prompt support Fish: starship init fish | source. Other initialisers follow the same pattern, e.g. zoxide init fish | source (see Modern CLI Tools). Both init fish forms are from the tools’ documentation and were not run here (unverified).

See Also #

Search all sheets

Tip: / or Ctrl/Cmd+K to open, Esc to close.