fish

Личный сайт Go-разработчика из Казани

Fish (friendly interactive shell) is the name of an exotic shell. That is a shell with a syntax that is derived from neither the Bourne-Shell nor the C-Shell.

The advantage of fish is that many features that you want in a modern shell come out-of-the-box, so you don’t have to install additional software like zsh and oh-my-zsh.

Examples of these features are autosuggestions, 24-bit colors, Man Page Completions (meaning fish automatically parses your man pages and suggests additional options for your commands) or the ability to make options through a web page (when a GUI is installed).

It was released in February 2005.

Guide

Be sure you have the newest fish shell. This was made with version 3.3.0. To test, type:

> fish -v

To start the fish shell, type:

> fish

to exit, type:

> exit

or press Ctrl + D

Now, right out of the gate, there’s one annoying thing in fish. It’s the welcome message. Who needs that, right? When your shell is started, just type:

> set -U fish_greeting ""

If you want to execute a single command written in bash, without switching to that shell, you can type:

> bash -c 'echo "fish is better than bash"'

In fish, you can use single or double quotes. The escape character is a \

You can change your configuration of fish either by editing the config file

> vim ~/.config/fish/config.fish

or by opening the aforementioned web settings:

> fish_config

Adding something to your fish PATH Variable is easy:

> fish_add_path ~/cowsay

Can you do that with bash, huh? No, you always have to look it up… It’s just that easy!

But there’s more. Most fish-specific commands start, you guessed it, with ‘fish’. Just type in fish and press TAB. And there you have one of the many cool features of fish: The autocompletion that just works. Now you can navigate with TAB, Shift + TAB and your Arrow-Keys .

To get help, contact your local psychiatrist or type man. That will bring up the manual for that command, for example:

> man set

If you finally tried fish, you can see something other in fish that’s really cool. Everything has cool colors, if you type in something wrong, it is red, without even executing, if you put something in quotes, you see where it ends and why that quote doesn’t work, because there’s another quotation mark in the quote at position 26.

fish has even more cool things, like wildcards. For example, type

> ls *.fish

That will list all fish files in your current directory.

You can have multiple wildcards per command or even a recursive wildcard, **, which basically means it includes files and directories, that fit. For example the following command would return (in your case):

> ls ~/images/**.jpg

~/images/nudes/pewdiepie.jpg
~/images/nudes/peppa.jpg
~/images/screenshots/2020-42-69.jpg
~/images/omegalul.jpg

Of course, you can also pipe the output of a command to another command

>echo sick egg, nadia. no u do really goofy shit.   | grep [udense]

write to a file:

>echo This\ is\ text > file.txt

(noticed the escape character?) Add to a file:

>echo This\ is\ a\ line >> file.txt
>echo This\ is\ a\ second\ line >> file.txt

For Autocompletion, just always press TAB. You will be surprised how many things fish knows.

To use variables, just type $VAR, like in bash.

> echo "My home is $HOME"
My home is /home/myuser

Here comes a difference between single and double quotes. If you use a variable in single quotes, it will not substitute it.

> echo 'My home is $HOME'
My home is $HOME

More on variables later.

To execute two commands, separate them with ;

> echo Lol; echo this is fun

The status code of the last command is stored in $status

You can use && for two commands that depend on each other.

> set var lol && echo $var

You can also use and which executes if the previous command was successful, or which executes if the previous command was not successful, and not which inverts the exit status of a command.

For example:

> if not echo It's very late I should not waste my time with this  
      echo Nobody heard you  
  end

(You can of course do all of that in the shell)


Now let’s start with the scripting part of fish.

As with every shell, you can not only execute commands in the shell, but also as files, saved as a .fish file. (You can also execute .sh files with fish syntax, but I always use .fish for fish-syntax scripts to distinguish them from bash script files)

1# This is a comment in fish. 2# 3# If you execute a file without specifying an interpreter, 4# meaning the software that runs your script, you need to tell the shell, 5# where that interpreter is. 6# For fish you just add the following comment as the first line in your script: 7 8#!/bin/fish 9 10# When executing via e.g. fish /path/to/script.fish 11# you don't need that, because you specified fish as an interpreter 12 13# Let's start with variables. 14# for use inside a program, you can use the syntax 15set name 'My Variable' 16 17# Use... 18set -x name value 19# to eXport, or 20set -e name 21# to Erase 22 23# a variable set with a space doesn't get sent as two arguments, but as one, as you would expect it. 24set turtlefolder 'Turtle Folder' 25mkdir $turtlefolder 26 27# This will create one folder, as expected, not two, like in bash... 28# Who would even want that? tHiS iS a fEaTurE, nOt a bUg... 29 30# you can even have lists as variables. This actually makes sense, because if you want to have a variable that would create two folders, you just give mkdir a list of your foldernames. 31 32# you can then count the entries in that list with: 33count $PATH 34 35# Not only is everything awesome, but in fish, everything is also a list. 36# So $PWD for example is a list of length 1. 37# To make a list, just give the set command multiple arguments: 38set list entry1 entry2 entry3 39 40# that way you can also append something to an existing variable: 41set PATH $PATH ~/cowsay/ 42 43# But, as previously mentioned, we also have a simpler way to do that specifically in fish. 44# As with every Array/List, you can access it with 45$listvar[2] 46 47# there's also ranges with 48$listvar[1..5] 49 50# and you can use negative numbers like 51$listvar[-1] 52# e.g to access the last element. 53 54# You can also do fancy cartesian products when you combine two list variables: 55set a 1 2 3 56set 1 a b c 57echo $a$1 58# Will output : 1a 2a 3a 1b 2b 3b 1c 2c 3c 59 60# Of course, if you separate them, it will see them as two separate arguments and echo them one after the other. THAT is expected behavior @bash. 61 62# There are also other useful things, like command substitutions. For example, when you want to output the returns of two commands in one line. In bash you would do that with 63echo "`ls` is in $PWD" 64# or 65echo "$(ls) is in $PWD" 66 67# if you ask me, that's unnecessary. I always type in the wrong apostrophe. Why not just use two parenthesis, like in fish? 68echo (ls) is in $PWD 69 70# Yep, that easy. And thanks to fish's highlighting you can instantly see, if you typed it in correctly. 71 72# And, as you would expect, if you ask me, your commands don't work in quotes. I mean why bash? Ok I'll stop now. But in fish, just do: 73echo (ls)" is in $PWD" 74# or 75set myvar "The file"(ls -a)" is in the directory $PWD" 76# will make a List with the string and all files. Try it out. Isn't that cool? 77 78# And to separate these variables in separate arguments, just put a space between them: 79 80set myvar "The files" (ls -a) " are in the directory $PWD" 81 82# There's also if, else if, else 83if grep fish /etc/shells 84 echo Found fish 85else if grep bash /etc/shells 86 echo Found bash 87else 88 echo Got nothing 89end 90 91# A little weird is that you compare stuff with one = sign, of course because we don't need it to set variables, but still... and the keyword "test": 92if test $var = "test" 93 echo yes 94else 95 echo no 96end 97 98# Of course, there's also switch case with 99switch $OS 100case Linux 101 echo "you're good" 102case Windows 103 echo "install Gentoo" 104case Arch 105 echo "I use arch btw" 106case '*' 107 echo "what OS is $OS, please?" 108end 109 110 111# functions in fish get their arguments through the $argv variable. The syntax is following: 112 113function print 114 echo $argv 115end 116 117# There are also events, like the "fish_exit"-event (What may that be, hmm?). 118 119# You can use them by adding them to the function definition: 120 121function on_exit --on-event fish_exit 122 echo fish is now exiting 123end 124 125# find events with the command 126functions --handlers 127 128 129# You can use the functions command to learn more about, well, functions. 130# For example you can print the source code of every function: 131functions cd 132functions print 133# or get the names of all functions: 134functions 135 136# There's while Loops, of course 137while test $var = lol 138 echo lol 139end 140 141# for Loops (with wildcards, they are even cooler): 142for image in *.jpg 143 echo $image 144end 145 146# there's an equivalent to the range(0, 5) in Python, so you can also do the standard for loops with numbers: 147 148set files (ls) 149for number in (seq 10) 150 echo "$files[$number] is file number $number" 151end 152 153# Cool! 154 155# The bashrc equivalent is not fishrc, but the previously mentioned config.fish file in ~/.config/fish/ 156# To add a function to fish, though, you should create a simple .fish file in that directory. Don't just paste that function in the config.fish. That's ugly. 157# If you have more, just add it, but those are the most important basics.