OSDN Git Service

Regular updates
[twpd/master.git] / fish-shell.md
1 ---
2 title: Fish shell
3 category: CLI
4 layout: 2017/sheet
5 prism_languages: [fish]
6 updated: 2018-01-31
7 weight: -1
8 ---
9
10 ### Keys
11
12 | Shortcut            | Description                           |
13 | ---                 | ---                                   |
14 | `^A ←`/`^E →`       | Move to the line beginning/end        |
15 | `Alt ←`/`Alt →`     | Jump to the previous/next word        |
16 | `↑`/`↓`             | Switch to the previous/next command   |
17 | `Alt ↑`/`Alt ↓`     | Switch to the previous/next arguments |
18 | ---                 | ---                                   |
19 | `^U`                | Delete to the beginning               |
20 | `^C`                | Cancel the line                       |
21 | ---                 | ---                                   |
22 | `Alt H`             | Show the command man page description |
23 | `Alt W`             | Show the short command description    |
24
25 ### Sample program
26
27 ```fish
28 #!/usr/bin/env fish
29
30 echo 'Hello from Fish!'
31 ```
32
33 ### Comments
34
35 ```fish
36 # my comment
37 ```
38
39 ### Printing text
40
41 ```fish
42 echo 'Hello from Fish!'
43 # or
44 printf '%s\n' 'Hello from Fish!'
45 ```
46
47 Print the string with a trailing `\n`.
48
49 ### Reading from stdin
50
51 ```fish
52 read my_variable
53 ```
54
55 Reads the string to a variable `my_variable`.
56
57 ### Loops
58
59 ```fish
60 for i in (seq 1 10)
61   ...
62 end
63 ```
64
65 ## Variables
66
67 ### Defining and erasing
68
69 ```fish
70 # Declare the global/local variable:
71 set my_variable 'Hello from Fish!'
72
73 i# Remove the variable:
74 set --erase my_variable
75 ```
76
77 ### Slicing
78
79 ```fish
80 echo $my_variable[1..10]
81 echo $my_variable[2..]
82 echo $my_variable[..-2]
83 ```
84
85 ## Numbers
86
87 ### Incrementing and decrementing
88
89 ```fish
90 set my_variable (math $my_variable + 1)
91 set my_variable (math $my_variable - 1)
92 ```
93
94 ### Arithmetic
95
96 ```fish
97 echo (math 1 + 2)
98 ```
99
100 | Operator            | Performs       |
101 | ---                 | ---            |
102 | `+`                 | Addition       |
103 | `-`                 | Subtraction    |
104 | `*`                 | Multiplication |
105 | `/`                 | Division       |
106 | `%`                 | Modulo         |
107 | `^`                 | Exponentiation |
108
109 ## Strings
110
111 ### Matching
112
113 Match the string against a regular expresion:
114
115 ```fish
116 string match --regex --entire 'Fish' 'Hello from Fish!'
117 ```
118
119 | Pattern             | Matches                   |
120 | ---                 | ---                       |
121 | `x?`                | Zero or one `x` chars     |
122 | `x*`                | Any count `x` chars       |
123 | `x+`                | One or more  `x` chars    |
124 | `x{n}`              | n times `x` chars         |
125 | `x{n,m}`            | n to m times `x` chars    |
126 | `x{n,}`             | n or more times `x` chars |
127 | `[xy]`              | `x` or y char             |
128 | `[^xy]`             | not `x` or y char         |
129 | ---                 | ---                 |
130 | `\w`                | Word character      |
131 | `\d`                | Digit character     |
132 | `\W`                | Not word character  |
133 | `\D`                | Not digit character |
134
135 Perl compatible regular expressions are described here.
136
137 ### Replacing
138
139 ```fish
140 # Replaces the first match
141 string replace --regex 'Fish' 'fish' 'Hello from Fish!'
142
143 # Replaces all matches
144 string replace --regex --all 'Fish' 'fish' 'Hello from Fish!'
145 ```
146
147 ## Conditionals
148
149 ### If/else
150
151 ```fish
152 if test $my_variable -lt $another_variable
153   ···
154 else if test $my_variable -eq $another_variable
155   ···
156 else
157   ···
158 end
159 ```
160
161 ### Comparisons
162
163 #### Numbers
164
165 | Number operator     | Meaning                                   |
166 | ---                 | ---                                       |
167 | `-lt`               | [L]ess [t]han                             |
168 | `-eq`               | [Eq]ual                                   |
169 | `-gt`               | [G]reater [t]han                          |
170 | `-le`               | [L]ess than or [e]qual to                 |
171 | `-ge`               | [G]reater than or [e]qual to              |
172 | `-ne`               | [N]ot [E]qual                             |
173
174 #### Strings
175
176 | String operator     | Meaning                                   |
177 | ---                 | ---                                       |
178 | `==`                | [Eq]ual                                   |
179 | `!=`                | [N]ot [E]qual                             |
180
181 #### Files
182
183 | File operator       | Meaning                                   |
184 | ---                 | ---                                       |
185 | `-f`                | [F]ile exists                             |
186 | `-d`                | [D]irectory exists                        |
187 | `-r`                | File or directory exists and [r]eadable   |
188 | `-w`                | File or directory exists and [w]ritable   |
189 | `-x`                | File or directory exists and e[x]ecutable |
190
191
192 ## Process communication
193
194 ### Writing to files
195
196 ```fish
197 # Overwrite file
198 echo 'Hello from Fish!' > my_file
199
200 # Append to file
201 echo 'Hello from Fish!' >> my_file
202 ```
203
204 ### Piping
205
206 ```fish
207 my_command | another_command
208 ```
209
210 Passes the first command stdout output as an input to a second command.
211
212 ### Command substitution
213
214 ```fish
215 echo (math $my_variable + 1)
216 ```
217
218 The `(...)` expression is substituted with the output of the command inside it.
219
220
221 ### Process substitution
222
223 ```fish
224 echo (math $my_variable + 1 | psub)
225 ```
226
227 The `(... | psub)` expression is substituted with a temporary file with the command's output.
228
229 ## Functions
230
231 ### Defining and erasing
232
233 ```fish
234 # Declare the function
235 function my_function --description 'My description'
236   ···
237 end
238
239 # Remove the function
240 functions --erase my_function
241 ```
242
243 ## Events
244
245 ### Emitting
246
247 ```fish
248 emit my_event
249 ```
250
251 Emits an event that can be picked up by other functions.
252
253 ### Event handling
254
255 ```fish
256 function my_hook --on-event my_event
257   ···
258 end
259 ```
260
261 Reacts to the `my_event` event.
262
263 ## Abbreviations
264
265 ### Defining and erasing
266
267 ```fish
268 # Declare the abbreviation
269 abbr --add grh "git reset --hard HEAD"
270 ```
271
272
273 ```fish
274 # Remove the abbreviation
275 abbr --erase grh
276 ```
277
278 ## Completions
279
280 ### Defining completions
281
282 ```fish
283 complete --command mycommand --arguments 'install uninstall'
284 complete --command mycommand --short-option 'h' --long-option 'help' --description 'Display help'
285 ```
286
287 | Option              | Description                                          |
288 | ---                 | ---                                                  |
289 | `--arguments`       | Arguments to the command itself or option            |
290 | `--short-option`    | Short option                                         |
291 | `--long-option`     | Long option                                          |
292 | `--no-files`        | Don't suggest files                                  |
293 | `--force-files`     | Suggest files                                        |
294 | `--condition`       | Display the hint only when a given condition is true |
295 | `--description`     | Description                                          |
296
297 Declares the completion for a command.
298
299 ### Removing completions
300
301 ```fish
302 complete --command mycommand --erase
303 ```
304
305 ## Useful built-in functions
306
307 | Function                              | Description                                                   |
308 | ---                                   | ---                                                           |
309 | `__fish_seen_argument`                | Check whether the specified argument is used                  |
310 | `__fish_seen_subcommand_from`         | Check whether the specified subcommand is used                |
311 | `__fish_use_subcommand`               | Check whether any subcommand is used                          |
312 | ---                                   | ---                                                           |
313 | `__fish_complete_directories`         | Complete directories with the specified letters in their name |
314 | `__fish_complete_suffix`              | Complete files with the specified suffix                      |
315 | ---                                   | ---                                                           |
316 | `__fish_complete_users`               | List all users                                                |
317 | `__fish_complete_groups`              | List all user groups                                          |
318 | `__fish_print_hostnames`              | List all host names                                           |
319 | `__fish_complete_pids`                | List all PIDs                                                 |
320 | `__fish_print_filesystems`            | List all known filesystems                                    |
321 | `__fish_print_interfaces`             | List all network interfaces                                   |