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).
- Install WSL.
- Open your WSL distribution (e.g., Ubuntu).
- 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 ArroworCtrl+Faccepts the suggestion. - Syntax Highlighting: Commands, arguments, and options are highlighted with different colors for better readability and to catch simple typos.
- Tab Completion: Pressing
Tabtriggers context-aware completions for commands, files, directories, and arguments. HoldingTabor 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
Navigation and File Management
cd <directory>: Change the current directory.cd ~/Documentsls: List directory contents.lsls -l: List directory contents in long format.ls -lls -a: List all files, including hidden ones.ls -apwd: Print the current working directory.pwdmkdir <directory_name>: Create a new directory.mkdir new_projectrm <file_or_directory>: Remove files or directories.rm old_file.txt rm -r old_directorycp <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.pstop: Display dynamic real-time view of running processes.topkill <pid>: Send a signal to a process (default is TERM).kill 1234 kill -9 5678 # Force killbg: Put a stopped job in the background.bgfg: Bring a background job to the foreground.fg
Text Manipulation and Viewing
cat <file>: Concatenate and display file content.cat ~/.bashrcless <file>: View file content page by page (scrollable).less /var/log/sysloghead <file>: Display the beginning of a file.head -n 10 config.yamltail <file>: Display the end of a file.tail -f /var/log/nginx/access.log # Follow log filegrep <pattern> <file>: Search for patterns in files.grep "error" /var/log/messages grep -i "warning" app.log # Case-insensitive grep -r "TODO" . # Recursive searchsed <script> <file>: Stream editor for text transformation.sed 's/old_text/new_text/' input.txt > output.txtawk '<pattern> { action }' <file>: Pattern scanning and processing language.awk '{ print $1, $3 }' data.txt # Print first and third columnssort <file>: Sort lines of text files.sort names.txt sort -r numbers.txt # Reverse sortuniq <file>: Report or omit repeated lines.uniq sorted_list.txt
Networking
ping <host>: Send ICMP ECHO_REQUEST to network hosts.ping google.comcurl <url>: Transfer data from or to a server.curl https://api.example.com/data curl -O https://example.com/file.zip # Download filewget <url>: Non-interactive network downloader.wget https://example.com/another_file.tar.gzssh <user>@<host>: Connect to a remote machine using SSH.ssh user@server.example.com ssh -p 2222 user@remote.host # Specify portscp <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_configecho <string>: Display a line of text.echo "Hello, Fish!"env: Display all environment variables.envset <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 vimhistory: Display command history.historysource <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 withlsfor long listing format.-a,--all: Often used withlsto show hidden files.-r,--recursive: Used withcp,rm,grepfor 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 withheadortailto specify the number of lines.-o: Used withcurlto save output to a file.-p <port>: Specify a port number for network commands likessh.-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
.logfiles in the current directory and its subdirectories, and count them:find . -name "*.log" | wc -l - Display the last 100 lines of
access.logand 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
dateas part of a filename:tar czf backup_$(date +%Y-%m-%d).tar.gz ~/Documents - Pass a list of files found by
findto 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:
(Save this infunction greet echo "Hello, $argv" end greet World~/.config/fish/functions/greet.fish)
Gotchas
Ctrl+CvsCtrl+Z:Ctrl+Csends an interrupt signal (SIGINT) to terminate the foreground process.Ctrl+Zsends a stop signal (SIGTSTP) to suspend the foreground process, allowing you to return to the shell or put it in the background withbg.- Variable expansion: Fish uses
$variablefor 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 -xis the equivalent of Bash’sexport. - 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+Rinitiates 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.
cdbehavior: Fish has a uniquecdbehavior where simply typing a directory name often suffices if it’s unambiguous and the directory is in yourPATHor current directory. However, explicitcd <directory>is always reliable.