GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing fishshell.com — Tutorial — fish-shell 4.9.3 documentation Tutorial¶Why fish?¶fish is a fully-equipped command line shell (like bash or zsh) that is smart and user-friendly. fish supports powerful features like syntax highlighting, autosuggestions, and tab completions that just work, with nothing to learn or configure. If you want to make your command line more productive, more useful, and more fun, without learning a bunch of arcane syntax and configuration options, then fish might be just what you’re looking for! Getting started¶Once installed, just type in You will be greeted by the standard fish prompt, which means you are all set up and can start using fish: > fish Welcome to fish, the friendly interactive shell Type help for instructions on how to use fish you@hostname ~> This prompt that you see above is the fish default prompt: it shows your username, hostname, and working directory. You can customize it, see how to change your prompt. From now on, we’ll pretend your prompt is just a Learning fish¶This tutorial assumes a basic understanding of command line shells and Unix commands, and that you have a working copy of fish. If you have a strong understanding of other shells, and want to know what fish does differently, search for the magic phrase unlike other shells, which is used to call out important differences. Or, if you want a quick overview over the differences to other shells like Bash, see Fish For Bash Users. For the full, detailed description of how to use fish interactively, see Interactive Use. For a comprehensive description of fish’s scripting language, see The Fish Language. Running Commands¶fish runs commands like other shells: you type a command, followed by its arguments. Spaces are separators: > echo hello world hello world This runs the command > mkdir My\ Files # Makes a directory called "My Files", with a space in the name > cp ~/Some\ File 'My Files' # Copies a file called "Some File" in the home directory to "My Files" > ls "My Files" Some File Getting Help¶Run > man set set - handle shell variables Synopsis... To open this section, use This only works for fish’s own documentation for itself and its built-in commands (the “builtins”). For any other commands on your system, they should provide their own documentation, often in the man system. For example Syntax Highlighting¶You’ll quickly notice that fish performs syntax highlighting as you type. Invalid commands are colored red by default: > /bin/mkd A command may be invalid because it does not exist, or refers to a file that you cannot execute. When the command becomes valid, it is shown in a different color: > /bin/mkdir Valid file paths are underlined as you type them: > cat ~/somefi This tells you that there exists a file that starts with These colors, and many more, can be changed by running For example, if you want to disable (almost) all coloring: fish_config theme choose none This picks the “none” theme. To see all themes: fish_config theme show Just running Autosuggestions¶As you type fish will suggest commands to the right of the cursor, in gray. For example: > /bin/hostname It knows about paths and options: > grep --ignore-case And history too. Type a command once, and you can re-summon it by just typing a few letters: > rsync -avze ssh . [email protected]:/some/long/path/doo/dee/doo/dee/doo To accept the autosuggestion, hit right ( If you don’t like autosuggestions, you can disable them by setting set -g fish_autosuggestion_enabled 0 Tab Completions¶A rich set of tab completions work “out of the box”. Press tab and fish will attempt to complete the command, argument, or path: > /pritab => /private/ If there’s more than one possibility, it will list them: > ~/stuff/stab ~/stuff/script.sh (command) ~/stuff/sources/ (directory) Hit tab again to cycle through the possibilities. The part in parentheses there (that “command” and “directory”) is the completion description. It’s just a short hint to explain what kind of argument it is. fish can also complete many commands, like git branches: > git merge prtab => git merge prompt_designer > git checkout btab builtin_list_io_merge (Branch) builtin_set_color (Branch) busted_events (Tag) Try hitting tab and see what fish can do! Variables¶Like other shells, a dollar sign followed by a variable name is replaced with the value of that variable: > echo My home directory is $HOME My home directory is /home/tutorial This is known as variable substitution, and it also happens in double quotes, but not single quotes: > echo "My current directory is $PWD" My current directory is /home/tutorial > echo 'My current directory is $PWD' My current directory is $PWD Unlike other shells, fish has an ordinary command to set variables: > set name 'Mister Noodle' > echo $name Mister Noodle (Notice the quotes: without them, Unlike other shells, variables are not further split after substitution: > mkdir $name > ls Mister Noodle In bash, this would have created two directories “Mister” and “Noodle”. In fish, it created only one: the variable had the value “Mister Noodle”, so that is the argument that was passed to You can erase (or “delete”) a variable with > set -e MyVariable > env | grep MyVariable (no output) For more, see Variable expansion. Exports (Shell Variables)¶Sometimes you need to have a variable available to an external command, often as a setting. For example many programs like To give a variable to an external command, it needs to be “exported”. This is done with a flag to > set -x MyVariable SomeValue > env | grep MyVariable MyVariable=SomeValue It can also be unexported with This works the other way around as well! If fish is started by something else, it inherits that parents exported variables. So if your terminal emulator starts fish, and it exports Exported variables can be local or global or universal - “exported” is not a scope! Usually you’d make them global via For more, see Exporting variables. Lists¶The Some variables, like Other variables, like > echo $PATH /usr/bin /bin /usr/sbin /sbin /usr/local/bin Variables whose name ends in “PATH” are automatically split on colons to become lists. They are joined using colons when exported to subcommands. This is for compatibility with other tools, which expect $PATH to use colons. You can also explicitly add this quirk to a variable with Lists cannot contain other lists: there is no recursion. A variable is a list of strings, full stop. Get the length of a list with > count $PATH 5 You can append (or prepend) to a list by setting the list to itself, with some additional arguments. Here we append /usr/local/bin to $PATH: > set PATH $PATH /usr/local/bin You can access individual elements with square brackets. Indexing starts at 1 from the beginning, and -1 from the end: > echo $PATH /usr/bin /bin /usr/sbin /sbin /usr/local/bin > echo $PATH[1] /usr/bin > echo $PATH[-1] /usr/local/bin You can also access ranges of elements, known as “slices”: > echo $PATH[1..2] /usr/bin /bin > echo $PATH[-1..2] /usr/local/bin /sbin /usr/sbin /bin You can iterate over a list (or a slice) with a for loop: for val in $PATH echo "entry: $val" end # Will print: # entry: /usr/bin/ # entry: /bin # entry: /usr/sbin # entry: /sbin # entry: /usr/local/bin One particular bit is that you can use lists like Brace expansion. If you attach another string to a list, it’ll combine every element of the list with the string: > set mydirs /usr/bin /bin > echo $mydirs/fish # this is just like {/usr/bin,/bin}/fish /usr/bin/fish /bin/fish This also means that, if the list is empty, there will be no argument: > set empty # no argument > echo $empty/this_is_gone # prints an empty line If you quote the list, it will be used as one string and so you’ll get one argument even if it is empty. For more, see Lists. For more on combining lists with strings (or even other lists), see cartesian products and Variable expansion. Wildcards¶fish supports the familiar wildcard > ls *.jpg lena.jpg meena.jpg santa maria.jpg You can include multiple wildcards: > ls l*.p* lena.png lesson.pdf The recursive wildcard > ls /var/**.log /var/log/system.log /var/run/sntp.log If that directory traversal is taking a long time, you can ctrl-c out of it. For more, see Wildcards. Pipes and Redirections¶You can pipe between commands with the usual vertical bar: > echo hello world | wc 1 2 12 stdin and stdout can be redirected via the familiar > grep fish < /etc/shells > ~/output.txt 2> ~/errors.txt To redirect stdout and stderr into one file, you can use > make &> make_output.txt For more, see Input and output redirections and Pipes. Command Substitutions¶Command substitutions use the output of one command as an argument to another. Unlike other shells, fish does not use backticks `` for command substitutions. Instead, it uses parentheses with or without a dollar: > echo In (pwd), running $(uname) In /home/tutorial, running FreeBSD A common idiom is to capture the output of a command in a variable: > set os (uname) > echo $os Linux Command substitutions without a dollar are not expanded within quotes, so the version with a dollar is simpler: > touch "testing_$(date +%s).txt" > ls *.txt testing_1360099791.txt Unlike other shells, fish does not split command substitutions on any whitespace (like spaces or tabs), only newlines. Usually this is a big help because unix commands operate on a line-by-line basis. Sometimes it can be an issue with commands like > printf '%s\n' (pkg-config --libs gio-2.0) -lgio-2.0 -lgobject-2.0 -lglib-2.0 > printf '%s\n' (pkg-config --libs gio-2.0 | string split -n " ") -lgio-2.0 -lgobject-2.0 -lglib-2.0 If you need a command substitutions output as one argument, without any splits, use quoted command substitution: > echo "first line second line" > myfile > set myfile "$(cat myfile)" > printf '|%s|' $myfile |first line second line| For more, see Command substitution. Separating Commands (Semicolon)¶Like other shells, fish allows multiple commands either on separate lines or the same line. To write them on the same line, use the semicolon (“;”). That means the following two examples are equivalent: echo fish; echo chips # or echo fish echo chips This is useful interactively to enter multiple commands. In a script it’s easier to read if the commands are on separate lines. Exit Status¶When a command exits, it returns a status code as a non-negative integer (that’s a whole number >= 0). Unlike other shells, fish stores the exit status of the last command in > false > echo $status 1 This indicates how the command fared - 0 usually means success, while the others signify kinds of failure. For instance fish’s There is also a For more, see The status variable. Combiners (And, Or, Not)¶fish supports the familiar > ./configure && make && sudo make install Here, fish also supports and, or, and not. The first two are job modifiers and have lower precedence. Example usage: > cp file1 file1_bak && cp file2 file2_bak; and echo "Backup successful"; or echo "Backup failed" Backup failed As mentioned in the section on the semicolon, this can also be written in multiple lines, like so: cp file1 file1_bak && cp file2 file2_bak and echo "Backup successful" or echo "Backup failed" Conditionals (If, Else, Switch)¶Use if and else to conditionally execute code, based on the exit status of a command. if grep fish /etc/shells echo Found fish else if grep bash /etc/shells echo Found bash else echo Got nothing end To compare strings or numbers or check file properties (whether a file exists or is writeable and such), use test, like if test "$fish" = "flounder" echo FLOUNDER end # or if test "$number" -gt 5 echo $number is greater than five else echo $number is five or less end # or # This test is true if the path /etc/hosts exists # - it could be a file or directory or symlink (or possibly something else). if test -e /etc/hosts echo We most likely have a hosts file else echo We do not have a hosts file end Combiners can also be used to make more complex conditions, like if command -sq fish; and grep fish /etc/shells echo fish is installed and configured end For even more complex conditions, use begin and end to group parts of them. There is also a switch command: switch (uname) case Linux echo Hi Tux! case Darwin echo Hi Hexley! case FreeBSD NetBSD DragonFly echo Hi Beastie! case '*' echo Hi, stranger! end As you see, case does not fall through, and can accept multiple arguments or (quoted) wildcards. For more, see Conditions. Functions¶A fish function is a list of commands, which may optionally take arguments. Unlike other shells, arguments are not passed in “numbered variables” like function say_hello echo Hello $argv end say_hello # prints: Hello say_hello everybody! # prints: Hello everybody! Unlike other shells, fish does not have aliases or special prompt syntax. Functions take their place. [2] You can list the names of all functions with the functions builtin (note the plural!). fish starts out with a number of functions: > functions N_, abbr, alias, bg, cd, cdh, contains_seq, dirh, dirs, disown, down-or-search, edit_command_buffer, export, fg, fish_add_path, fish_breakpoint_prompt, fish_clipboard_copy, fish_clipboard_paste, fish_config, fish_default_key_bindings, fish_default_mode_prompt, fish_git_prompt, fish_hg_prompt, fish_hybrid_key_bindings, fish_indent, fish_is_root_user, fish_job_summary, fish_key_reader, fish_md5, fish_mode_prompt, fish_npm_helper, fish_opt, fish_print_git_action, fish_print_hg_root, fish_prompt, fish_sigtrap_handler, fish_svn_prompt, fish_title, fish_update_completions, fish_vcs_prompt, fish_vi_cursor, fish_vi_key_bindings, funced, funcsave, grep, help, history, hostname, isatty, kill, la, ll, ls, man, nextd, open, popd, prevd, prompt_hostname, prompt_pwd, psub, pushd, realpath, seq, setenv, suspend, trap, type, umask, up-or-search, vared, wait You can see the source for any function by passing its name to > functions ls function ls --description 'List contents of directory' command ls -G $argv end For more, see Functions. There is a function called alias, but it’s just a shortcut to make functions. fish also provides abbreviations, through the abbr command. Loops¶While loops: while true echo "Loop forever" end # Prints: # Loop forever # Loop forever # Loop forever # yes, this really will loop forever. Unless you abort it with ctrl-c. For loops can be used to iterate over a list. For example, a list of files: for file in *.txt cp $file $file.bak end Iterating over a list of numbers can be done with for x in (seq 5) touch file_$x.txt end For more, see Loops and blocks. Prompt¶Unlike other shells, there is no prompt variable like You can define your own prompt from the command line: > function fish_prompt; echo "New Prompt % "; end New Prompt % _ Then, if you are happy with it, you can save it to disk by typing Multiple lines are OK. Colors can be set via set_color by passing it named ANSI colors, or hex RGB values: function fish_prompt set_color purple date "+%m/%d/%y" set_color FF0000 echo (pwd) '>' (set_color --reset) end This prompt would look like: 02/06/13 /home/tutorial > _ See also Writing your own prompt. You can choose among sample prompts by running $PATH¶
fish takes care to set To prepend /usr/local/bin and /usr/sbin to > set PATH /usr/local/bin /usr/sbin $PATH To remove /usr/local/bin from > set PATH (string match -v /usr/local/bin $PATH) For compatibility with other shells and external commands, $PATH is a path variable, and so will be joined with colons (not spaces) when you quote it: > echo "$PATH" /usr/local/sbin:/usr/local/bin:/usr/bin and it will be exported like that, and when fish starts it splits the $PATH it receives into a list on colon. You can do so directly in A faster way is to use the fish_add_path function, which adds given directories to the path if they aren’t already included. It does this by modifying the > fish_add_path /usr/local/bin The advantage is that you don’t have to go mucking around in files: just run this once at the command line, and it will affect the current session and all future instances too. You can also add this line to config.fish, as it only adds the component if necessary. Or you can modify $fish_user_paths yourself, but you should be careful not to append to it unconditionally in config.fish, or it will grow longer and longer. Startup (Where’s .bashrc?)¶fish starts by executing commands in It is possible to directly create functions and variables in > cat ~/.config/fish/config.fish set -x PATH $PATH /sbin/ function ll ls -lh $argv end However, it is more common and efficient to use autoloading functions and universal variables. If you want to organize your configuration, fish also reads commands in .fish files in Autoloading Functions¶When fish encounters a command, it attempts to autoload a function for that command, by looking for a file with the name of that command in For example, if you wanted to have a function > cat ~/.config/fish/functions/ll.fish function ll ls -lh $argv end This is the preferred way to define your prompt as well: > cat ~/.config/fish/functions/fish_prompt.fish function fish_prompt echo (pwd) "> " end See the documentation for funced and funcsave for ways to create these files automatically, and $fish_function_path to control their location. Universal Variables¶A universal variable is a variable whose value is shared across all instances of fish, now and in the future - even after a reboot. You can make a variable universal with > set -U EDITOR vim Now in another shell: > echo $EDITOR vim You only need to set universal variables once interactively. There is no need to add them to your config files. For more details, see Universal Variables. Ready for more?¶If you want to learn more about fish, there is lots of detailed documentation, the official gitter channel, an official mailing list, and the github page. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |