# Basic tutorial of using fish shell on Mac OS

Time：2019-11-2

English is a confusing language. For example, consider the words moon and good. For the layman, the two words seem to rhyme, but the pronunciation of the former is / Mun / (according to international pronunciation alphabet), and the pronunciation of the latter is / good /. It seems that the only rule in English is the exception.
UNIX shells are equally confusing. For example, in the Bourne shell (and most commonly used UNIX shells), ‘$var’, ‘var’ and ‘$var’ look similar, but they produce very different results. In the shell examples in this article, each cli is preceded by the name of the shell used and the command number.

Copy code

The code is as follows:

bash-1) # Demonstrate the differences between single-, double-, and back quotes
bash-2) var=ls
bash-3) echo ‘$var’$var
bash-4) echo “$var” ls bash-5) echo $var
Rakefile app bin components config db
doc lib log patches public script src
test tmp vendor

In the above command sequence, set the variable VaR to the two letter string ls. In the first echo command, single quotes prevent the variable from being interpreted, so the text in the quotes, the four letter string $VaR, is displayed as is. In line 4, double quotes interpret the variable, so the result is the string ls. Finally, the apostrophe interprets the variable and runs the intermediate result as a subshell. Therefore, ‘$var’ generates the intermediate string ls, which runs as a shell command to generate the content list of the current directory.
Of course, all three operators (single quotation mark, double quotation mark and apostrophe) have legitimate uses, but as with the exception of English, it’s a headache to remember and master these nuances. To further prove this, what’s the difference between $VaR and “$var”? (hint: suppose $var contains spaces.) Copy code The code is as follows: bash-1) # Create three files and try to remove two bash-2) touch three two one bash-3) var=”one two” bash-4) rm “$var”
rm: one two: No such file or directory
bash-5) rm $var bash-6) ls three If a variable contains spaces, double quotes expand the variable as is into a parameter. Otherwise, any spaces in the variable are interpreted as parameter separators. Shell syntax is a headache. This is bad because it makes cli, one of UNIX’s most powerful features, harder to master. These inconsistencies can even bother UNIX veterans. Fortunately, the emergence of fish (friendly interactive shell) has changed this chaotic situation. It provides concise syntax and significantly improves the user experience. Like other shells, fish provides redirection, shortcuts, globbing (the expansion of wildcards), subshell, tab completion, and variables. However, unlike other shells, fish also provides color coded cli, a rich command-line editor, and a large number of documents. In addition, fish provides only one way to do anything, which is very wise. If a UNIX utility can accomplish a certain task, fish will not provide this feature repeatedly through built-in commands. For example, fish uses system wide application / bin / kill to terminate the process. (in contrast, the Bourne shell implements its own kill version through a built-in application. You can access this version by typing / bin / kill at the Bourne shell command prompt). Fish prioritizes simplicity over flexibility as much as possible, which significantly simplifies its use. Next, let’s install fish and experience some of its features. Get fish Fish is an open source project created by Axel liljenbrantz. The license agreement adopted is GNU General Public License, version 2. As of this writing, the latest version of fish is 1.23.0, which was released on January 13, 2008. If you use UNIX or UNIX like systems, such as Linux ® or Mac OS X, it should be easy to build fish from source on your system. The following are the build steps, as shown in Listing 1: Download the latest source code package for the program. Decompression. Enter the source directory. Configure the build. Run make. Listing 1. Building fish from source Copy code The code is as follows: bash-1) wget http://www.fishshell.org/files/1.23.0/fish-1.23.0.tar.gz bash-2) tar xzvf fish-1.23.0.tar.gz bash-3) cd fish-1.23.0 bash-4) ./configure –without-xsel checking if autoconf needs to be run… no checking if autoheader needs to be run… no checking for /usr/pkg/include include directory… no bash-5) make gcc -c -o function.o function.c bash-6) sudo make install To use fish as your login shell: * add the line ‘/usr/bin/fish’ to the file ‘/etc/shells’. * use the command ‘chsh -s /usr/bin/fish’. If you use UNIX like systems, configure may not require more flags. However, in order to minimize dependencies and let fish use the same directory structure as a common shell, you can add — without xsel and — prefix = / usr, respectively. (if you use Mac OS X version 10.4 leopard, add the parameter ldflags = – liconv as well. If you omit the latter option on Mac OS X, you cannot compile the accompanying fish utility). In addition, if you use the popular UNIX version, you will probably find a pre built binary version that you can install directly on your system. For example, if you use Debian Linux, you can use the sudo apt get install fish command to install fish directly. Visit the home page of the fish project to see if there is a binary version for your system. Introduction Before discussing more complex topics, let’s take a look at how to complete some common shell tasks in fish: To redirect standard input and standard output, you should use the operators < and > respectively. To redirect standard errors, use the ^ operator, as shown in Figure 1. Use ^ ^ to append standard errors to a file. Figure 1. Standard error redirection with ^ operator In command 3, the error message generated by RM is redirected to the errors file. Command 4 displays the contents of this file. The fish shell provides various support for redirection, such as combining descriptors into a stream and an end descriptor. By the way, the color and underline of the text are not edited. The shell highlights the text in the CLI as you type. Green indicates that the command name is valid; invalid command names are indicated in red. An underscore indicates that the specified file exists. (the following section discusses shell feedback in detail.). Use parentheses (()) to run the subshell, as shown in Figure 2. The text in parentheses is interpreted as a series of commands that the shell replaces with execution results. Figure 2. Running a subshell with parentheses Create aliases (that is, shortcuts) by creating the fish function. The function can contain one or more commands, and the special variable$argv automatically expands to a list of parameters passed on the command line.
You can use the functions command to list all defined functions. Use functions — erase name to delete functions, such as functions — erase LL.
You can also immediately save any function written on the command line. When you are finished writing the code, enter funcsave name, for example, funcsave LL. After that, this function can be used by all currently running shells and all subsequent shells. You can use the function name command to edit existing functions interactively. The funced command provides syntax highlighting, tab completion, and automatic indentation; funcsave and funced make it easier for users to customize the shell.
Enter set variable namevalue to set the variable. As with the built-in command functions, you can delete a variable by entering set — erase variable name. Enter the dollar sign ($) and the variable name to get the value stored in the variable, as shown in Figure 3. Figure 3. Check if a variable exists Fish provides the — Query option to check if a variable is defined. If this variable has been set, set — query returns the status code 0, which means there is no error; otherwise, it returns 1. Statement 6 uses the or operator to connect two commands: the second command (echo) executes only if the first command fails. So, how does fish deal with$VaR, ‘$var’, “$var” and ` $var ‘? It follows several simple rules: If the variable contains spaces, the spaces are preserved, and the variable is always a single parameter, as shown in Figure 4. Figure 4. Fish preserves the spaces embedded in the string as is If the outermost quotation mark is double, expand all variables. If the outermost quotation mark is a single quotation mark, the variable is not expanded. Let’s look at the practical application of these rules. Command 1 creates four files, and the name of the last file contains spaces. Commands 3 and 4 delete the file specified by the file variable. Commands 6 and 7 remove the two files specified by the twofiles variable. Take a closer look at command 6: because values are not enclosed in quotation marks (single or double), spaces are not preserved. Therefore, command 7 expands this variable into two parameters and deletes two files. Commands 9 and 10 repeat the scenarios in commands 6 and 7. Commands 11 and 12 demonstrate the space rule. Although variables are not enclosed in double quotes in command 12, fish leaves spaces in command 11. Very nice. Commands 14 through 16 demonstrate the nested quote rule for fish. Now, take a look at commands 11, 15, and 16. The shell uses color coding to display matching quotes to ensure proper syntax. Look at commands 9 and 11 again. The latter command underlines the filename, which indicates that the file exists. There is no underline in command 9, which prompts the user that something went wrong. The initial of fish stands for friendship, and user friendliness is its main goal. A very helpful feature for novices When it comes to user friendliness, we can’t help mentioning fish’s tab completion feature, which is extremely helpful to new UNIX users and experts. To experience tab completion, follow the example below. Press tab at the end of each line. If you are not sure how to spell a command name, you can press tab after entering several letters to see a list of possible complete commands, as shown in Figure 5. (the list of commands displayed on your system may not be the same as shown here. This list depends on the path environment variable and the contents of your Unix System). Figure 5. Press tab to complete the command name Note the red text in the CLI. If fish doesn’t know a command name, display it in red. Press tab to display all application names (with a short description) that begin with the currently entered letter. Press tab on the empty prompt line to see all the applications in the path. If you want to know the options available for a command, press tab after the hyphen (-) or double hyphen (-), as shown in Figure 6. Figure 6. You can also complete an option by pressing tab Fish displays the available options. The shell maintains an index of many common commands and options, and you are likely to get the help you need. However, custom or more complex utilities may lack this data. You can read the fish documentation to learn more about writing your own completion features. You can also press tab after entering a few letters of an option, as shown in Figure 7. The shell displays all matching options. Figure 7. Part of the options can also be entered If you don’t know the type of operand a command handles, fish can help in many cases, but not all. For example, if you type set (or vared, the fish variable editor) and a space, and then press tab, fish displays a list of available variables. The operand of set is a variable. Similarly, if you type in type and a space, and then press tab, fish displays a list of built-in functions that extend the utilities available on the file system. In general, all built-in functions in fish have context sensitive operand completion. Please try CD, see Figure 8. Figure 8. Many commands are context sensitive and display the appropriate parameters CD function is a fish function whose operand is an existing directory. After entering CD, press tab, and fish displays all the existing directories contained in each directory in the cdpath. Another intelligent completion is related to SSH. Enter SSH and a space, and then press tab to see the list of known hostnames obtained from the known host file of secure shell (usually in ~ /. SSH / known_hosts): Copy code The code is as follows: fish-1) ssh login.example.com (Hostname) host1.example.com (Hostname) The fish shell also complements the filename and directory name. Again, it highlights the correct element when you enter the pathname. An important difference between fish and other shells is that it does not provide historical shortcuts, such as!,!, and!$.

If you like fish and want to use it as a login shell, add the path of fish to the official shell list (/ etc / shells), and then run Chsh:

Copy code

The code is as follows:

bash-1) type fish
fish is /usr/bin/fish
bash-2) sudo vi /etc/shells
Add the line /usr/bin/fish to the file if it’s missing, and save the file
bash-3) cat /etc/shells
/bin/bash
/bin/csh
/bin/ksh
/bin/sh
/bin/tcsh
/bin/zsh
/usr/bin/fish
bash-4) chsh -s /usr/bin/fish
Changing shell for strike
Last login: Wed Oct 8 15:02:21 on ttys000
Welcome to fish, the friendly interactive shell
Type help for instructions on how to use fish
fish-1) echo \$SHELL
/usr/bin/fish

Concluding remarks
There are many useful features in fish that are worth studying. “Fish is very nutritious.”
You can adjust the color used for syntax highlighting. You can customize the startup process by editing ~ /. Config / fish / config.fish. You can use common variables and fishd to share variables across shell instances. This shell also has excellent historical search features, interactive variable editor and interactive command line editor.
Best of all, fish itself provides a lot of documentation. If you need help, just type help at the command prompt.
The doctor’s opinion is right: eating “fish” is good for you.

## How to search files or directories under CentOS

Search for files or directories: The search for files is great! Because we often need to know where that file is, let’s talk about how to search it! There are also quite excellent ones under Linux Search system! Usually find is not very common! Because the speed is slow, but also very hard disk! Usually […]