jorgebucaran/cookbook.fish
{ "createdAt": "2016-01-17T16:11:22Z", "defaultBranch": "main", "description": "From Shell to Plate: Savor the Zest of Fish đŚ", "fullName": "jorgebucaran/cookbook.fish", "homepage": "", "language": null, "name": "cookbook.fish", "pushedAt": "2023-09-04T02:25:59Z", "stargazersCount": 2104, "topics": [ "cookbook", "fish", "tutorial" ], "updatedAt": "2025-11-23T21:22:33Z", "url": "https://github.com/jorgebucaran/cookbook.fish"}The Fish Cookbook
Section titled âThe Fish CookbookâFrom Shell to Plate: Savor the Zest of Fish đŚ
Welcome to The Fish Cookbook, your ultimate guide to unlocking the full potential of shell scripting with the delightful Fish. With its user-friendliness and convenient features, Fish stands out as a true gem among shells.
Master programming challenges with our curated collection of Fish recipes. Glide through your code with ease, whether youâre a seasoned pro or a shell scripting newbie.
This cookbook is licensed under CC BY-NC-SA 4.0, ensuring knowledge and inspiration flow while respecting the rights of others.
Whatâs on the Menu?
Section titled âWhatâs on the Menu?â- [Setting Sail]!(#setting-sail)
- [How the Shell do I Install Fish?]!(#how-the-shell-do-i-install-fish)
- [Making Fish Your Default Shell, Ahoy!]!(#making-fish-your-default-shell-ahoy)
- [Discovering Fishâs Installation Location]!(#discovering-fishs-installation-location)
- [Where to Seek Help]!(#where-to-seek-help)
- [Foundations]!(#foundations)
- [Decoding Prompts: What Are These Wiggly Characters?]!(#decoding-prompts-what-are-these-wiggly-characters)
- [Where Am I in Fish? Discovering Your Current Path]!(#where-am-i-in-fish-discovering-your-current-path)
- [Searching and Running Commands in Fish: How Do I Do That?]!(#searching-and-running-commands-in-fish-how-do-i-do-that)
- [Did My Command Succeed in Fish?]!(#did-my-command-succeed-in-fish)
- [Whatâs the Fish Shebang All About? Making Scripts Executable]!(#whats-the-fish-shebang-all-about-making-scripts-executable)
- [How Do I Debug My Fish Scripts Interactively? Navigating with Breakpoints]!(#how-do-i-debug-my-fish-scripts-interactively-navigating-with-breakpoints)
- [Where to find Fishâs equivalent of
.bash_profileor.bashrc?]!(#where-to-find-fishs-equivalent-of-bash_profile-or-bashrc)
- [All About Variables]!(#all-about-variables)
- [How Do I Set Variables in Fish? A Quick Guide to Variable Scopes]!(#how-do-i-set-variables-in-fish-a-quick-guide-to-variable-scopes)
- [How Do I Export a Variable in Fish?]!(#how-do-i-export-a-variable-in-fish)
- [How Do I List All Environment Variables in Fish?]!(#how-do-i-list-all-environment-variables-in-fish)
- [How Do I Set the
$PATHPersistently in Fish?]!(#how-do-i-set-the-path-persistently-in-fish) - [How Do I Remove a Path from the
$PATHin Fish?]!(#how-do-i-remove-a-path-from-the-path-in-fish) - [How Do I Remove a Path Persistently from the
$PATHin Fish?]!(#how-do-i-remove-a-path-persistently-from-the-path-in-fish) - [How Do I Check if a Path Exists in the
$PATHin Fish?]!(#how-do-i-check-if-a-path-exists-in-the-path-in-fish)
- [All About Functions]!(#all-about-functions)
- [How Do I Whip Up a Function in Fish?]!(#how-do-i-whip-up-a-function-in-fish)
- [How Do I Craft a âPrivateâ Function in Fish?]!(#how-do-i-craft-a-private-function-in-fish)
- [Should Function Names and File Names Be a Perfect Match?]!(#should-function-names-and-file-names-be-a-perfect-match)
- [Can I Fit Multiple Functions in a Single File?]!(#can-i-fit-multiple-functions-in-a-single-file)
- [How Do I Reveal a Functionâs Definition in Fish?]!(#how-do-i-reveal-a-functions-definition-in-fish)
- [Exploring Functions, Builtins, and Commands in Fish]!(#exploring-functions-builtins-and-commands-in-fish)
- [Displaying the Fish Function Roster]!(#displaying-the-fish-function-roster)
- [Verifying a Functionâs Existence in Fish]!(#verifying-a-functions-existence-in-fish)
- [All About Arguments]!(#all-about-arguments)
- [Accessing Function Arguments in Fish]!(#accessing-function-arguments-in-fish)
- [Accessing Script Arguments in Fish]!(#accessing-script-arguments-in-fish)
- [Parsing Command Line Arguments in Fish]!(#parsing-command-line-arguments-in-fish)
- [All About Aliases]!(#all-about-aliases)
- [The Art of Defining Aliases in Fish]!(#the-art-of-defining-aliases-in-fish)
- [The Problem with Aliases in
config.fish]!(#the-problem-with-aliases-in-configfish)
- [IO]!(#io)
- [Reading a File with Fish]!(#reading-a-file-with-fish)
- [Reading from stdin and Redirecting Like a Pro in Fish]!(#reading-from-stdin-and-redirecting-like-a-pro-in-fish)
- [Mastering Redirection in Fish]!(#mastering-redirection-in-fish)
- [Concurrency: Making Your Fish Swim Faster]!(#concurrency-making-your-fish-swim-faster)
- [Run Commands in the Background with Fish]!(#run-commands-in-the-background-with-fish)
- [Checking Background Jobs in Fish]!(#checking-background-jobs-in-fish)
- [Synchronize Background Tasks in Fish Like a Pro]!(#synchronize-background-tasks-in-fish-like-a-pro)
- [Glossary]!(#glossary)
- [Contributing]!(#contributing)
Setting Sail
Section titled âSetting SailâAhoy there, matey! Prepare to embark on a grand adventure with our trusty mate, Fish. In this section, weâll guide you through the step-by-step setup process to ensure youâre ready to navigate the command line seas like a true captain. Letâs get your shipshape and ready for the voyage ahead!
How the Shell do I Install Fish?
Section titled âHow the Shell do I Install Fish?âIf youâre ready to dive into the wonderful world of Fish, hereâs how you can get your hands wet. You can either follow the official website for instructions, or stick around and sail through the directions provided here for your OS.
For macOS with Homebrew:
brew update && brew install fishFor Debian:
wget http://download.opensuse.org/repositories/shells:fish:release:2/Debian_8.0/Release.keyapt-key add - < Release.keyecho 'deb http://download.opensuse.org/repositories/shells:/fish:/release:/2/Debian_8.0/ /' >> /etc/apt/sources.list.d/fish.listapt-get updateapt-get install fishFor Ubuntu:
sudo apt-add-repository ppa:fish-shell/release-2sudo apt-get updatesudo apt-get install fishFor CentOS:
cd /etc/yum.repos.d/wget http://download.opensuse.org/repositories/shells:fish:release:2/CentOS_7/shells:fish:release:2.repoyum install fishFor Fedora:
dnf install fishFor Arch Linux:
pacman -S fishFor Gentoo:
emerge fishFor FreeBSD:
pkg install fishFor NetBSD:
pkgin install fishFor OpenBSD:
pkg_add fishFor the brave who want to build from source:
sudo apt-get -y install git gettext automake autoconf ncurses-dev build-essential libncurses5-dev
git clone -q --depth 1 https://github.com/fish-shell/fish-shellcd fish-shellautoreconf && ./configuremake && sudo make installWith Fish installed, a thrilling journey lies ahead. Gear up to explore the command line, unearthing Fishâs core treasures and secrets. Hereâs to abundant and joyous fishing ventures! đť
Making Fish Your Default Shell, Ahoy!
Section titled âMaking Fish Your Default Shell, Ahoy!âOnce youâve installed Fish and itâs safely located in your $PATH, for example, at /usr/local/bin, you can make it your default shell for a smooth sailing command line experience.
echo /usr/local/bin/fish | sudo tee -a /etc/shellschsh -s /usr/local/bin/fishDiscovering Fishâs Installation Location
Section titled âDiscovering Fishâs Installation LocationâCurious about where Fish is installed? Utilize the handy which command.
which fish/usr/local/bin/fishWhere to Seek Help
Section titled âWhere to Seek HelpâIf you find yourself in need of assistance or have burning questions about Fish, fret not. Thereâs a treasure trove of resources awaiting you:
- Official Repository â Explore the official Fish repository on GitHub, where you can find the source code and contribute to the project.
- Tutorial â Set sail with the Fish tutorial, a step-by-step guide to help you master the shellâs features and functionality.
- Documentation â Dive into the official documentation for Fish, where youâll discover a wealth of information and guidance.
r/fishshellâ Join the community on Reddit, where fellow Fish enthusiasts gather to share their knowledge and adventures.- Gitter Channel â Hop aboard the Gitter Channel, a chat platform where you can engage with other Fish users and seek live assistance.
- StackOverflow
#fishâ Cast your questions into the StackOverflow sea, specifically in the#fishtag, to receive guidance from the wider programming community. - awsm.fish â Discover a curated collection of prompts, plugins, and other hidden treasures to enhance your Fish journey. đ
Foundations
Section titled âFoundationsâDecoding Prompts: What Are These Wiggly Characters?
Section titled âDecoding Prompts: What Are These Wiggly Characters?âThe prompt is where you type commands and interact with Fish. Read more about the UNIX prompt here.
You might encounter something like this:
jb@mbp ~/C/cookbook>The tilde ~ represents the home directory in a compact form, instead of /users/jb/home, /Users/jb, and so on.
The @ symbol is an arbitrary character chosen to separate the username jb from the computerâs name mbp. You can learn to tailor your prompt to display only whatâs relevant to you.
The forward slash / serves as the path delimiter. At a glance, you can see that the current directory is inside the home directory, under Code/cookbook. The abbreviated path displays just C.
To customize the length of the abbreviated path, use:
set fish_prompt_pwd_dir_length NUMBEROr, if you prefer not to have abbreviated paths:
set fish_prompt_pwd_dir_length 0Then, your prompt will look like this:
jb@mbp ~/Code/cookbook>The greater-than symbol > signifies the end of the prompt.
Not a fan of these conventions? Feel free to craft your own prompt to suit your preferences.
Where Am I in Fish? Discovering Your Current Path
Section titled âWhere Am I in Fish? Discovering Your Current PathâTo find your current location in Fish, simply refer to the read-only environment variable $PWD.
echo $PWD/Users/jb/Code/cookbookAlternatively, you can use the pwd builtin to determine the current directory.
pwd/Users/jb/Code/cookbookIn Fish, both $PWD and pwd always resolve symbolic links. This means that if youâre inside a directory thatâs a symbolic reference to another, youâll still get the path to the actual directory.
Interactively, typing pwd is more convenient. For scripting, $PWD is less expensive in terms of function calls.
Dazzle me!
echo "The current working directory is "(pwd)
# Versus
echo "The current working directory is $PWD"Searching and Running Commands in Fish: How Do I Do That?
Section titled âSearching and Running Commands in Fish: How Do I Do That?âTo run a command, type the commandâs name and press return.
lsAlternatively, begin typing the command youâre looking for and press tab. Fish will use the built-in pager, allowing you to browse and select the command interactively.
Fish determines which commands are available by examining the $PATH environment variable. This variable contains a list of paths, and every binary file inside any of those paths can be run by their name.
Display your $PATH contents with:
printf "%s\n" $PATH/usr/local/bin/usr/bin/binTo list every command in your system and display them in columns, use:
ls $PATH | columnIf the list is truncated, try:
ls $PATH | column | lessNavigate the list using k and j to move down/up, and press q to exit.
The $PATH variable is created at the beginning of the Fish process during environment initialization. You can modify, prepend, or append this variable yourself, for example, in ~/.config/fish/config.fish.
In addition to the type, builtin, and functions built-ins mentioned earlier, *nix systems often include one or more shell-agnostic alternatives such as which, apropos, whatis, and others.
These commands have overlapping functionality but also possess unique features. Consult your systemâs manpage for more information.
Did My Command Succeed in Fish?
Section titled âDid My Command Succeed in Fish?âEvery command returns an exit code to indicate whether it succeeded or not. An exit code of 0 means success, while any other value signifies failure. Different commands use various integers to represent possible errors.
In Fish, you can check the exit code of any command using the read-only variable $status.
my_commandecho $statusWhen working with pipes, Fish provides a helpful variable called $pipestatus that allows you to check the exit codes of all commands in a pipeline.
Suppose you have a pipeline like this:
command1 | command2 | command3To find the exit codes of all commands in the pipeline, use $pipestatus:
echo $pipestatusThis will print an array of exit codes, one for each command in the pipeline. For example, if command1 succeeded, command2 failed with an exit code of 2, and command3 failed with an exit code of 1, the output would look like this:
0 2 1With $pipestatus, you can conveniently keep an eye on the success and failure of commands in your pipelines.
Whatâs the Fish Shebang All About? Making Scripts Executable
Section titled âWhatâs the Fish Shebang All About? Making Scripts ExecutableâThe [shebang]!(https://en.wikipedia.org/wiki/Shebang_(Unix)) is a special comment that instructs the shell to run a script using a specific program, such as node or python.
To run a script with fish by default, add a shebang to the first line of your file:
#!/usr/bin/env fishGive me the scoop!
#!/usr/bin/env fish
echo "Check this out, ma'! I'm a Fish on wheels đź"Save that to a file and mark it as executable.
chmod +x my_scriptThe system above allows you to run the script directly by using its path:
./my_scriptinstead of:
fish my_scriptHow Do I Debug My Fish Scripts Interactively? Navigating with Breakpoints
Section titled âHow Do I Debug My Fish Scripts Interactively? Navigating with BreakpointsâTo interactively debug your Fish scripts, use the breakpoint builtin. Simply drop it anywhere in your script where you want to pause execution and launch an interactive debugging prompt.
function some_function set --local files (ls ~) breakpoint # when this is executed, an interactive prompt will be launched and you will be able to inspect $files # ... rest of function is not executed until you exit from debuggerendWith breakpoint, you can take a closer look at your scriptâs execution, inspect variables, and navigate through the script.
Where to find Fishâs equivalent of .bash_profile or .bashrc?
Section titled âWhere to find Fishâs equivalent of .bash_profile or .bashrc?âIn Fish shell, your configuration is stored in the config.fish file located at ~/.config/fish/config.fish. This file serves as Fishâs equivalent to .bash_profile or .bashrc in Bash. It allows you to customize and configure your Fish shell environment by adding functions, environment variables, and other personalized settings.
All About Variables
Section titled âAll About VariablesâHow Do I Set Variables in Fish? A Quick Guide to Variable Scopes
Section titled âHow Do I Set Variables in Fish? A Quick Guide to Variable ScopesâTo set variables in Fish, use the set builtin.
set foo 42The set builtin accepts the following flags to explicitly declare the scope of the variable:
-l,--local: available only to the innermost block-g,--global: available outside blocks and by other functions-U,--universal: shared between all fish sessions and persisted across restarts of the shell-x,--export: available to any child process spawned in the current session
If no scope modifier is used, the variable will be local to the current function; otherwise, it will be global.
If the variable has already been defined, the previous scope will be used.
Local Variables
Section titled âLocal VariablesâThe variable foo will not be available outside of the if block.
if true set -l foo 42end
echo "foo=$foo" # foo=Global Variables
Section titled âGlobal VariablesâThe variable foo will be available outside the if block.
if true set -g foo 42end
echo "foo=$foo" # foo=42Universal Variables
Section titled âUniversal VariablesâThe variable foo will be preserved and available to future shell sessions.
set -U foo 42fishecho "foo=$foo" # foo=42Exported Variables
Section titled âExported VariablesâThe variable foo will be local and exported, therefore available to the fish child process created inside the if block.
if true set -lx foo 42 fish -c 'echo "foo=$foo"' # foo=42endThe variable foo will be global, but since itâs not exported, it wonât be available to the fish child process.
set -g foo 42fish -c 'echo "foo=$foo"' # foo=The variable GPG_AGENT_INFO will be universal and exported, therefore preserved across future shell sessions and child processes.
set -Ux GPG_AGENT_INFO /Users/jb/.gnupg/S.gpg-agent:12345:2Now youâre set to sail through the variable seas of Fish!
How Do I Export a Variable in Fish?
Section titled âHow Do I Export a Variable in Fish?âTo export a variable in Fish, use the set builtin along with the scope modifier -x or --export.
set -x foo 42fish -c 'echo "foo=$foo"' # foo=42By using the -x or --export option, youâre ensuring that the variable is available to any child process spawned in the current session.
How Do I List All Environment Variables in Fish?
Section titled âHow Do I List All Environment Variables in Fish?âTo list all environment variables in Fish, use the set builtin without any modifier flags.
setIf you prefer to see only the variable names without the values, employ the --names option:
set --namesAnd for a full view of each variable without truncating long lines, go with --long:
set --longHow Do I Set the $PATH Persistently in Fish?
Section titled âHow Do I Set the $PATH Persistently in Fish?âTo persistently add a path to your $PATH in Fish, make use of the $fish_user_paths variable.
set -U fish_user_paths $fish_user_paths my_pathIf youâre using Fish 3.2.1 or later, you can opt for the built-in fish_add_path function instead.
fish_add_path my_pathSee
$PATHin the Fish tutorial for juicy information.
How Do I Remove a Path from the $PATH in Fish?
Section titled âHow Do I Remove a Path from the $PATH in Fish?âTo remove a path from the $PATH in Fish, use the set builtin with the -e or --erase flag, combined with the contains builtin to find the index of the path youâd like to remove.
if set -l index (contains -i $my_path $PATH) set -e PATH[$index]endHow Do I Remove a Path Persistently from the $PATH in Fish?
Section titled âHow Do I Remove a Path Persistently from the $PATH in Fish?âTo remove a path persistently from the $PATH in Fish, use the set builtin with the -e or --erase flag, combined with the contains builtin to find the index of the path youâd like to remove from the $fish_user_paths variable.
if set -l index (contains -i $my_path $fish_user_paths) set -e -U fish_user_paths[$index]endHow Do I Check if a Path Exists in the $PATH in Fish?
Section titled âHow Do I Check if a Path Exists in the $PATH in Fish?âTo check if a path exists in the $PATH in Fish, use the contains builtin. This handy tool helps you verify whether your desired path is swimming along with the other paths in your $PATH variable. đ
if contains $my_path $PATH # $my_path is in $PATHendAll About Functions
Section titled âAll About FunctionsâHow Do I Whip Up a Function in Fish?
Section titled âHow Do I Whip Up a Function in Fish?âCrafting a function in Fish is a piece of cake! Just use the function builtin to start creating your shell masterpiece.
function mkdirp mkdir -p $argvendTo make sure this function stays fresh in future Fish sessions, save it to ~/.config/fish/functions/mkdirp.fish. A neat way to accomplish this is by using the funcsave function.
funcsave mkdirpAlternatively, you can use the functions builtin to write the function definition to a file, like a secret recipe for your custom shell commands.
functions mkdirp > ~/.config/fish/functions/mkdirp.fishHow Do I Craft a âPrivateâ Function in Fish?
Section titled âHow Do I Craft a âPrivateâ Function in Fish?âWhile Fish doesnât have built-in private functions, you can still be sneaky! Use a custom namespace to prefix any function you want to treat as private.
function _prefix_my_functionendYou can also simulate private scope using functions -e.
Show me the secret sauce!
function foo function _foo echo Foo functions -e _foo # Erase _foo end _fooendThis workaround helps you keep some Fish functions under wraps, like a top-secret ingredient in your shell recipe.
Should Function Names and File Names Be a Perfect Match?
Section titled âShould Function Names and File Names Be a Perfect Match?âAbsolutely! Fishâs lazy-loading / autoloading mechanism depends on this match made in heaven.
If you have a file ~/.config/fish/functions/foo.fish with a valid function definition bar:
- In a new shell, trying to run
barproduces an unknown-command error. - Typing
foowill highlight as a valid command, but produce an unknown-command error. - Trying to run
baragain now works like a charm.
Show me the magic!
Save bar to ~/.config/fish/functions/foo.fish.
function bar echo Barendfunctions bar > ~/.config/fish/functions/foo.fishCreate a new shell session.
fishTry to run bar, then foo, then bar again.
bar# fish: Unknown command 'bar'foo# fish: Unknown command 'foo'bar# BarCan I Fit Multiple Functions in a Single File?
Section titled âCan I Fit Multiple Functions in a Single File?âYes, indeed! You can define as many functions as you want in a single file. Just remember that Fish doesnât have private functions, so every function in the file will end up in the global scope when the file is loaded.
Since functions are eagerly loaded, defining multiple functions in a single file might not be as efficient as the one-function-per-file approach, which leverages Fishâs autoloading capabilities. So, while itâs possible to have multiple functions in one file, spreading them across separate files might give you a performance boost and a more organized structure.
How Do I Reveal a Functionâs Definition in Fish?
Section titled âHow Do I Reveal a Functionâs Definition in Fish?âIf youâre certain that the command youâre looking for is a function, you can use the functions builtin to display its definition:
functions my_functionOn the other hand, if youâre not sure whether the command is a function, a builtin, or a system command, you can rely on the type command to reveal its true nature:
type my_functionmy_function is a function with definition# ... function definition ...Using type, you can easily identify what kind of command youâre dealing with and get the information you need.
Exploring Functions, Builtins, and Commands in Fish
Section titled âExploring Functions, Builtins, and Commands in FishâIn Fish, commands, functions, and builtins each serve different purposes and are defined in different ways:
-
System commands are executable scripts, binaries, or symbolic links to binaries that are located in directories listed in your
$PATHvariable. When a command runs, it operates as a child process and can only access environment variables that have been exported. Example:fish. -
Functions are user-defined commands that can be created within Fish. Some functions are included as part of the Fish distribution and serve as predefined helpers. Examples include
alias,type, andnextd. -
Builtins are commands that have been compiled directly into the Fish executable. Like functions, builtins can access the environment, but they do not spawn a separate child process. This allows them to execute more quickly and with lower overhead. Examples of builtins include
functionsandset.
These different types of commands help make Fish a versatile and powerful shell, allowing for a wide range of customization and utility.
Displaying the Fish Function Roster
Section titled âDisplaying the Fish Function RosterâTo list all the functions defined in Fish, employ the functions builtin without any arguments. Note that hidden functionsâthose with names starting with an underscoreâwonât be displayed. To reveal everything, including hidden functions, use functions -a or functions --all.
For a different approach, launch the Fish web-based configuration and head to the /functions tab by running:
fish_config functionsVerifying a Functionâs Existence in Fish
Section titled âVerifying a Functionâs Existence in FishâThe type function is your go-to tool for checking whether a function exists in Fish. It provides information about commands, builtins, or functions.
if not type --quiet "$command_name" exit 1endFor builtins, use builtin --names.
if not contains -- "$command_name" (builtin --names) exit 1endFor functions, employ functions --query.
if not functions --query "$command_name" exit 1endFor other commands, utilize command --search.
if not command --search "$command_name" > /dev/null exit 1endIn Fish >= 2.5, itâs even easier:
if not command --search --quiet "$command_name" exit 1endAll About Arguments
Section titled âAll About ArgumentsâAccessing Function Arguments in Fish
Section titled âAccessing Function Arguments in FishâTo access the arguments passed to a function in Fish, simply use the $argv variable.
function Foo printf "%s\n" $argvend
Foo foo bar bazfoobarbazAccessing Script Arguments in Fish
Section titled âAccessing Script Arguments in FishâTo access the arguments passed to a script in Fish, use $argv.
fish ./my_script foo bar bazfoobarbazExample: my_script
#!/usr/bin/env fishprintf "%s\n" $argvParsing Command Line Arguments in Fish
Section titled âParsing Command Line Arguments in FishâTo parse command line arguments in Fish, you can use a for loop.
for option in $argv switch "$option" case -f --foo case -b --bar case \* printf "error: Unknown option %s\n" $option endendFor a more powerful parsing solution, you can explore Fishâs official argparse builtin. Another option is to consider using getopts, which is a different approach to CLI parsing.
All About Aliases
Section titled âAll About AliasesâThe Art of Defining Aliases in Fish
Section titled âThe Art of Defining Aliases in FishâFish offers the alias function to create a convenient alias:
alias rimraf "rm -rf"This creates a function like:
function rimraf --wraps='rm -rf' --description 'alias rimraf rm -rf' rm -rf $argv;endBe aware that aliases created with alias wonât be available in new shell sessions. To make them persistent, use:
alias -s ...This saves the alias to ~/.config/fish/functions/[alias-name].fish
The Problem with Aliases in config.fish
Section titled âThe Problem with Aliases in config.fishâDefining aliases inside ~/.config/fish/config.fish slows down your shell start since each alias/function is eagerly loaded.
To persist aliases across shell sessions, use alias -s, which creates a function and saves it to ~/.config/fish/functions. This leverages Fish function lazy-loading / autoloading mechanism.
Reading a File with Fish
Section titled âReading a File with FishâTo gracefully read a file line by line in Fish, employ the read builtin.
while read -la line echo $lineend < my_fileReading from stdin and Redirecting Like a Pro in Fish
Section titled âReading from stdin and Redirecting Like a Pro in FishâTo read from stdin in Fish, the read builtin is your best friend.
read --prompt "echo 'Name: ' " -l nameName: Flipperecho $nameFlipperTo read from an arbitrary input stream, combine read with the while builtin.
while read -la line echo $lineendMastering Redirection in Fish
Section titled âMastering Redirection in FishâTo redirect stderr to $my_file:
my_command 2> $my_fileTo redirect stdout to $my_file:
my_command > $my_fileTo redirect stdout to stderr:
my_command >&2To redirect stderr to stdout:
my_command 2>&1Concurrency: Making Your Fish Swim Faster
Section titled âConcurrency: Making Your Fish Swim FasterâRun Commands in the Background with Fish
Section titled âRun Commands in the Background with FishâTo run a command in the background in Fish, simply use &.
sleep 10 &Checking Background Jobs in Fish
Section titled âChecking Background Jobs in FishâTo see if background jobs are running in Fish, use the jobs builtin.
if jobs > /dev/null echo BusyendSynchronize Background Tasks in Fish Like a Pro
Section titled âSynchronize Background Tasks in Fish Like a ProâFish 3.0.0 introduced a dedicated wait builtin for easy synchronization of background tasks. But if youâre curious about how it was done before wait existed, hereâs a throwback recipe for you.
First, to check if tasks are running in the background, parse the output from the jobs builtin.
Parse by Job ID
function get_jobs jobs $argv | command awk -v FS=\t ' /[0-9]+\t/{ jobs[++nJobs] = $1 } END { for (i in jobs) { print(jobs[i]) } exit nJobs == 0 } 'endParse by Group ID
function get_jobs jobs -g | command awk 'NR > 0 { print; i++ } END { exit i == 0 }'endNext, block the foreground until all background jobs are finished.
function wait while true set -l has_jobs set -l all_jobs (get_jobs) or break
for j in $argv if contains -- $j $all_jobs set -e has_jobs break end end
if set -q has_jobs break end endendHereâs an example of our code in action.
set -l urls "https://"{google,twitter,youtube,facebook,github}".com"
for url in $urls fish -c "curl -Lw \"$url: %{time_total}s\n\" -o /dev/null -s $url" &end
wait (get_jobs)Glossary
Section titled âGlossaryâHere is a concise glossary of key terms and concepts mentioned in the Fish Shell Cookbook:
-
Fish: Fish, short for âFriendly Interactive Shell,â is a command-line shell for Unix-like operating systems. It is known for its user-friendly features, syntax highlighting, auto-suggestions, and powerful scripting capabilities.
-
Fish Installation: Installing Fish involves obtaining the Fish shell and setting it up on your system. You can install Fish using package managers or by building it from source.
-
Default Shell: The default shell is the shell that is automatically launched when you open a terminal or log in to your system. Setting Fish as the default shell ensures that Fish is the shell you interact with by default.
-
Fish Configuration: The Fish configuration involves customizing the behavior and appearance of the Fish shell. Configuration options can be set in the
config.fishfile, which is loaded when Fish starts. -
Variables: Variables in Fish are used to store and manipulate data. They can hold strings, numbers, or other types of values. Variables can be defined, assigned values, and accessed in Fish scripts and interactive sessions.
-
Functions: Functions in Fish allow you to group a series of commands together and give them a name. Functions can be defined with the
functionkeyword and called by their name. -
IO (Input/Output): IO refers to the input and output operations performed by the shell. Fish provides various commands and techniques for reading from files, redirecting output, and handling input streams.
-
Concurrency: Concurrency in Fish involves running commands or tasks simultaneously. Fish allows you to run commands in the background, check the status of background jobs, synchronize tasks, and wait for background processes to complete.
These terms provide a high-level understanding of the key concepts covered in the Fish Cookbook. Refer to the respective sections in this document for more detailed information and practical examples.
Contributing
Section titled âContributingâAhoy there! Weâre excited to have you on board for the Fish Cookbook adventure! Your contributions can make this Cookbook a valuable resource for the Fish Shell community. So, letâs dive in and follow the guidelines below.
You can contribute to the Cookbook in a few ways:
-
Adding new recipes: If you have a useful Fish Shell recipe or a clever solution to a pesky problem, you can share your knowledge by adding a new recipe to the repository. Just make sure your recipe follows the established structure and provides clear instructions. Letâs make this Cookbook even more diverse and flavorful!
-
Enhancing existing recipes: If you come across a recipe that could use some sprucing up, you can lend a hand and make it even better. Improve the content, add more information, fix any errors, or fine-tune the code. Together, weâll make those recipes shine!
-
Keeping the Cookbook current: The Fish shell is continuously evolving, with new features and updates being released regularly. Help us ensure the Cookbook stays relevant by identifying outdated content, highlighting new features or changes, and proposing updates to the existing recipes. By keeping the Cookbook in sync with the latest Fish shell developments, weâll maintain a valuable and current resource for everyone to enjoy.
To join our crew and contribute to the Cookbook, follow these guidelines:
- Set your sails and fork the repository. Then, create a new branch for your contributionsâa safe space for your changes.
- Work your magic in the new branch. Ensure that the content is accurate, clear, and follows the agreed-upon structure. Weâre all on the same ship here!
- If youâre adding a new recipe, choose a catchy and informative titleâone that grabs attention!
- Commit your changes and write a descriptive commit message to let us know what your changes are all about. Be as detailed as a well-crafted map!
- Push your branch to your forked repository, like a captain charting new waters.
- Open a pull request against the main repository. Share the story behind your changes and why theyâre valuable. The more we know, the better we can appreciate your work!
Once your pull request is submitted, our trusty crew of maintainers will review it. They may provide feedback or request some tweaks before we hoist the flag and merge your contribution.
You have our heartfelt gratitude for lending a hand in enhancing the Fish Cookbook. We value your efforts, and together, weâll create a fantastic resource for the Fish community. Ahoy!