fishshell

A better shell than bash

Install

# Installation methods for Ubuntu and Debian.
sudo apt-get install fish
# Mac installation method.
brew install fish

Startup and Help

Since the syntax of Fish is very different from Bash, Bash scripts are generally incompatible. Therefore, it is recommended not to make Fish the default Shell but to start it manually each time.

# After the installation is complete, you can start Fish.
$ fish
# During use, if you need help, you can enter the help command
$ help

Color display

# Invalid commands are red
$ mkd
# Valid commands are blue
$ mkdir
# Valid paths will be underlined. If there is no underscore, you know the path does not exist.
$ cat ~/somefi

Auto-suggestions

Fish will automatically suggest possible options behind the cursor, colored gray. If you follow the suggestion, you can press or Control + F. To adopt only part of it, press Alt + →.

$ /bin/hostname # Command suggestions
$ grep --ignore-case # Parameter suggestions
$ ls node_modules # Path suggestions

Autocomplete

When entering a command, Fish will automatically display the previous matching history record. If there is no matching history record, Fish will guess the possible results and automatically complete various inputs. For example, if you type pyt and then press Tab, it will be automatically completed into the python command.

Fish can also automatically complete Git branches.

Script syntax

if statement

if grep fish /etc/shells
     echo Found fish
else if grep bash /etc/shells
     echo Found bash
else
     echo Got nothing
end

switch statement

switch(uname)
case Linux
     echo Hi Tux!
case Darwin
     echo Hi Hexley!
case FreeBSD NetBSD DragonFly
     echo Hi Beastie!
case '*'
     echo Hi, stranger!
end

while loop

while true
     echo "Loop forever"
end

for loop

for file in *.txt
     cp $file $file.bak
end

Function

Fish functions are used to encapsulate commands, or to alias existing commands.

function ll
     ls -lhG $argv
end

The above code defines an ll function. After executing this function on the command line, you can use the ll command instead of ls -lhG. Among them, the variable $argv represents the parameters of the function.

functionls
     command ls -hG $argv
end

The above code redefines the ls command. Note that command must be added before ls in the function body, otherwise an error will be reported due to an infinite loop.

Prompt

The fish_prompt function is used to define the command line prompt (prompt).

function fish_prompt
   set_color purple
   date "+%m/%d/%y"
   set_color FF0
   echo (pwd) '>'
   set_color normal
end

After executing the above function, your command line prompt will become as follows.

02/06/13
/home/tutorial >

Configuration

The configuration file of Fish is ~/.config/fish/config.fish. This file will be automatically loaded every time Fish is started. Fish also provides a web interface to configure this file.

$ fish_config # Browser opens the Web interface configuration

Running Commands: Compatible with bash and other shell command execution methods Getting Help: help/man cmd -> browser/terminal Syntax Highlighting: Check whether the command is correct in real time Wildcards: supports abbreviation * recursive matching Pipes and Redirections: Use ^ for stderr Autosuggestions: Automatic suggestions, you can use Ctrl-f / -> to complete Tab Completions: More powerful tab completion Variables: set using set Exit Status: use echo $status instead of $? Exports (Shell Variables) Lists: all variables in fish are really lists Command Substitutions: Use (cmd) to execute commands instead of backticks or $() Combiners (And, Or, Not): The use of conjunction to represent logical operations is not supported Functions: use $argv instead of $1 Conditionals (If, Else, Switch) / Functions / Loops: more user-friendly writing (refer to py) Prompt: function fish_prompt implementation Startup (Where's .bashrc?): ~/.config/fish/config.fish, a better way is autoloading-function, universal-variables Autoloading Functions: ~/.config/fish/functions/. Universal Variables: a variable whose value is shared across all instances of fish

set name 'czl' #Set variable, replace name=czl
echo $name
echo $status # exit status, replace $?
env #environment variable
set -x MyVariable SomeValue # alternative to export
set -e MyVariable
set PATH $PATH /usr/local/bin # Use lists to record PATH
set -U fish_user_paths /usr/local/bin $fish_user_paths # takes effect permanently
touch "testing_"(date +%s)".txt" # command subtitution, replace `date +%s`
cp file.txt file.txt.bak; and echo 'back success'; or echo 'back fail' # combiner
functions # List functions defined under fish

References