Fish Shell

Fish shell cheatsheet — functions, variables, abbreviations, aliases, config. set -x VAR val, function name; end, abbr -a, fish_config. The user-friendly shell.

6 min read

What it is

Fish (Friendly Interactive Shell) is a user-friendly command-line shell that aims to be interactive and easy to use, with features like autosuggestions, syntax highlighting, and tab completion out-of-the-box.

Installation

Linux

Debian/Ubuntu:

sudo apt update
sudo apt install fish

Fedora:

sudo dnf install fish

Arch Linux:

sudo pacman -S fish

macOS

Using Homebrew:

brew install fish

Windows

Fish can be installed on Windows via the Windows Subsystem for Linux (WSL).

  1. Install WSL.
  2. Open your WSL distribution (e.g., Ubuntu).
  3. Follow the Linux installation instructions for your chosen distribution.

To make Fish your default shell after installation:

chsh -s $(which fish)

(You may need to log out and log back in for this to take effect.)

Core Concepts

  • Autosuggestions: Fish suggests commands as you type, based on your history. These suggestions appear in a lighter color. Pressing Right Arrow or Ctrl+F accepts the suggestion.
  • Syntax Highlighting: Commands, arguments, and options are highlighted with different colors for better readability and to catch simple typos.
  • Tab Completion: Pressing Tab triggers context-aware completions for commands, files, directories, and arguments. Holding Tab or pressing it multiple times cycles through options.
  • Functions: Fish uses functions for commands and scripting. Shell built-ins and user-defined commands are all treated as functions.
  • Configuration: Fish configuration is typically done by creating files in ~/.config/fish/.

Commands / Usage

  • cd <directory>: Change the current directory.
    cd ~/Documents
    
  • ls: List directory contents.
    ls
    
  • ls -l: List directory contents in long format.
    ls -l
    
  • ls -a: List all files, including hidden ones.
    ls -a
    
  • pwd: Print the current working directory.
    pwd
    
  • mkdir <directory_name>: Create a new directory.
    mkdir new_project
    
  • rm <file_or_directory>: Remove files or directories.
    rm old_file.txt
    rm -r old_directory
    
  • cp <source> <destination>: Copy files or directories.
    cp ~/Downloads/report.pdf ~/Documents/
    cp -r ~/ProjectA ~/Backup/
    
  • mv <source> <destination>: Move or rename files or directories.
    mv old_name.txt new_name.txt
    mv ~/Downloads/image.jpg ~/Pictures/
    

Process Management

  • ps: List currently running processes.
    ps
    
  • top: Display dynamic real-time view of running processes.
    top
    
  • kill <pid>: Send a signal to a process (default is TERM).
    kill 1234
    kill -9 5678 # Force kill
    
  • bg: Put a stopped job in the background.
    bg
    
  • fg: Bring a background job to the foreground.
    fg
    

Text Manipulation and Viewing

  • cat <file>: Concatenate and display file content.
    cat ~/.bashrc
    
  • less <file>: View file content page by page (scrollable).
    less /var/log/syslog
    
  • head <file>: Display the beginning of a file.
    head -n 10 config.yaml
    
  • tail <file>: Display the end of a file.
    tail -f /var/log/nginx/access.log # Follow log file
    
  • grep <pattern> <file>: Search for patterns in files.
    grep "error" /var/log/messages
    grep -i "warning" app.log # Case-insensitive
    grep -r "TODO" . # Recursive search
    
  • sed <script> <file>: Stream editor for text transformation.
    sed 's/old_text/new_text/' input.txt > output.txt
    
  • awk '<pattern> { action }' <file>: Pattern scanning and processing language.
    awk '{ print $1, $3 }' data.txt # Print first and third columns
    
  • sort <file>: Sort lines of text files.
    sort names.txt
    sort -r numbers.txt # Reverse sort
    
  • uniq <file>: Report or omit repeated lines.
    uniq sorted_list.txt
    

Networking

  • ping <host>: Send ICMP ECHO_REQUEST to network hosts.
    ping google.com
    
  • curl <url>: Transfer data from or to a server.
    curl https://api.example.com/data
    curl -O https://example.com/file.zip # Download file
    
  • wget <url>: Non-interactive network downloader.
    wget https://example.com/another_file.tar.gz
    
  • ssh <user>@<host>: Connect to a remote machine using SSH.
    ssh user@server.example.com
    ssh -p 2222 user@remote.host # Specify port
    
  • scp <source> <destination>: Securely copy files between hosts.
    scp local_file.txt user@remote.host:/home/user/
    scp user@remote.host:/var/log/app.log . # Copy to current directory
    

Shell Configuration and Environment

  • fish_config: Open the Fish configuration web UI in your browser.
    fish_config
    
  • echo <string>: Display a line of text.
    echo "Hello, Fish!"
    
  • env: Display all environment variables.
    env
    
  • set <variable> <value>: Set an environment variable.
    set MY_VAR "some value"
    
  • set -x <variable> <value>: Export an environment variable to child processes.
    set -x EDITOR vim
    
  • history: Display command history.
    history
    
  • source <file>: Execute commands from a file in the current shell.
    source ~/.config/fish/functions/my_custom_function.fish
    

Common Flags and Options

  • -l, --long: Often used with ls for long listing format.
  • -a, --all: Often used with ls to show hidden files.
  • -r, --recursive: Used with cp, rm, grep for recursive operations.
  • -i, --interactive: Prompts before overwriting or removing files.
  • -f, --force: Force operation, e.g., ignore nonexistent files or arguments, never prompt.
  • -n <number>: Often used with head or tail to specify the number of lines.
  • -o: Used with curl to save output to a file.
  • -p <port>: Specify a port number for network commands like ssh.
  • -v, --verbose: Increase verbosity.
  • -h, --help: Display help message for a command.

Common Patterns

Piping Output

  • List all running processes and filter for those containing "nginx":
    ps aux | grep nginx
    
  • Find all .log files in the current directory and its subdirectories, and count them:
    find . -name "*.log" | wc -l
    
  • Display the last 100 lines of access.log and search for "404 errors":
    tail -n 100 access.log | grep "404"
    
  • Sort a list of files by size (human-readable) and display the top 10 largest:
    ls -lh | sort -h -k 5 | tail -n 10
    

Command Substitution

  • Use the output of date as part of a filename:
    tar czf backup_$(date +%Y-%m-%d).tar.gz ~/Documents
    
  • Pass a list of files found by find to another command:
    grep "TODO" (find . -name "*.py")
    

Backgrounding Processes

  • Run a long-running script in the background:
    ./my_long_script.sh &
    
  • Stop a foreground process (Ctrl+Z) and then resume it in the background:
    # Run process
    ./my_script.sh
    # Press Ctrl+Z to suspend
    bg
    

Scripting Basics

  • Conditional execution:
    if test -f "my_file.txt";
        echo "File exists.";
    else
        echo "File does not exist.";
    end
    
  • Looping through files:
    for f in *.txt;
        echo "Processing $f";
        wc -l $f;
    end
    
  • Defining a custom function:
    function greet
        echo "Hello, $argv"
    end
    greet World
    
    (Save this in ~/.config/fish/functions/greet.fish)

Gotchas

  • Ctrl+C vs Ctrl+Z: Ctrl+C sends an interrupt signal (SIGINT) to terminate the foreground process. Ctrl+Z sends a stop signal (SIGTSTP) to suspend the foreground process, allowing you to return to the shell or put it in the background with bg.
  • Variable expansion: Fish uses $variable for variable expansion. Unlike Bash, it doesn’t typically require $ for simple variable names within commands, but it’s good practice for clarity and especially when using array elements or special variables. set -x is the equivalent of Bash’s export.
  • Quoting: Fish’s quoting rules can be slightly different from Bash. Single quotes (') prevent all expansion, while double quotes (") allow variable and command substitution.
  • Default Shell: After installing Fish, it’s not automatically set as your default shell. You need to run chsh -s $(which fish) and then log out/in.
  • History Search: Pressing Ctrl+R initiates an incremental reverse search through your command history.
  • Autosuggestions vs. Completion: Autosuggestions are based on history and appear as a greyed-out suffix to your current command. Tab completion offers explicit options for the current word or command.
  • cd behavior: Fish has a unique cd behavior where simply typing a directory name often suffices if it’s unambiguous and the directory is in your PATH or current directory. However, explicit cd <directory> is always reliable.