40
|
1 local Luan = require "luan:Luan.luan"
|
|
2 local error = Luan.error
|
|
3 local pairs = Luan.pairs or error()
|
|
4 local Io = require "luan:Io.luan"
|
|
5 local Http = require "luan:http/Http.luan"
|
|
6 local Shared = require "site:/lib/Shared.luan"
|
|
7 local head = Shared.head or error()
|
|
8 local header = Shared.header or error()
|
|
9
|
|
10
|
|
11 local content = {
|
|
12 intro = {
|
45
|
13 title = [[Introduction]]
|
40
|
14 content = function()
|
|
15 %>
|
46
|
16 <p>I really don't want to write this tutorial, but all the existing <a href="bash.html">Bash</a> tutorials are so horrible that I have no choice. I looked at books, websites, and YouTube - all horrible. They don't start with the basics. They include all kinds of useless crap. And they don't explain core concepts. So I have no choice but to write this damn thing for my <a href="http://localhost:8080/learn.html#bash">Learn Reactionary Programming</a> Bash lesson.</p>
|
40
|
17
|
45
|
18 <p>I will focus on Mac and Windows. I don't have Linux, and I hate Linux, so I won't discuss it. Most of Bash is the same on Mac and Windows, but where they differ, I will discuss both.</p>
|
40
|
19 <%
|
|
20 end
|
|
21 }
|
45
|
22 access = {
|
|
23 title = [[Running Bash]]
|
40
|
24 content = function()
|
|
25 %>
|
45
|
26 <p>How you access Bash depends on your operating system. If you are on a Mac then you access Bash through the Mac Terminal which is found in "Applications > Utilities > Terminal.app". Be sure to <a href="https://www.howtogeek.com/444596/how-to-change-the-default-shell-to-bash-in-macos-catalina/">set the default shell to Bash</a>. If you are on Windows then install <a href="https://www.msys2.org/">MSYS2</a>. The default terminal isn't so good, so I suggest <a href="https://www.msys2.org/docs/terminals/#windows-terminal">using the Windows Terminal</a>.</p>
|
40
|
27 <%
|
|
28 end
|
|
29 }
|
45
|
30 start = {
|
|
31 title = [[Getting Started]]
|
41
|
32 content = function()
|
|
33 %>
|
45
|
34 <p>When I start Bash on my Mac I see:</p>
|
|
35
|
|
36 <code block>
|
|
37 Last login: Thu Jan 4 23:25:35 on ttys004
|
|
38
|
|
39 The default interactive shell is now zsh.
|
|
40 To update your account to use zsh, please run `chsh -s /bin/zsh`.
|
|
41 For more details, please visit https://support.apple.com/kb/HT208050.
|
|
42 ~ $
|
44
|
43
|
45
|
44 </code>
|
|
45
|
|
46 <p>On Windows - MSYS2 I just see:</p>
|
44
|
47
|
45
|
48 <code block>
|
|
49 ~ $
|
|
50
|
|
51 </code>
|
|
52
|
|
53 <p>The line with the <b><code>$</code></b> is the command prompt. The cursor is at the end of it, and if I type, my text will go there. You may have different text before the <b><code>$</code></b> which is okay, but the line should end with <b><code>$</code></b>. If it doesn't, something is wrong.</p>
|
|
54
|
|
55 <p>Now type "qqq". When I say type "whatever", you should type return/enter at the end. Only when you type return/enter will Bash process what you typed. Now you should see:</p>
|
44
|
56
|
45
|
57 <code block>
|
|
58 ~ $ qqq
|
|
59 -bash: qqq: command not found
|
|
60 ~ $
|
|
61 </code>
|
|
62
|
|
63 <p>Bash doesn't know what "qqq" means and says so. Now try the following... Note that you type what is after the <b><code>$</code></b> and Bash should respond as shown.</p>
|
|
64
|
|
65 <code block>
|
|
66 ~ $ echo hi
|
|
67 hi
|
|
68 ~ $ echo how are you
|
|
69 how are you
|
|
70 ~ $ echo bye
|
|
71 bye
|
|
72 </code>
|
|
73
|
|
74 <p>The <code>echo</code> command just echos what comes after. Now press the up-arrow on your keyboard. This should put the previous command where your cursor is. Up-arrow again brings the command before that. Try down-arrow and left-arrow and right-arrow. You can use this to navigate through your command history. The delete key also works for editing lines. And of course you can type. When you press return/enter then Bash will get your edited command and process it.</p>
|
|
75
|
|
76 <p>When you enter <code>echo how are you</code>, <code>echo</code> is the command. This command has 3 arguments: <code>how</code>, <code>are</code>, and <code>you</code>. Commands and arguments are separated with spaces. It doesn't matter how many spaces, so:</p>
|
|
77
|
|
78 <code block>
|
|
79 ~ $ echo how are you
|
|
80 how are you
|
|
81 </code>
|
|
82
|
|
83 <p><code>echo</code> just returns the arguments separated by one space.</p>
|
41
|
84 <%
|
|
85 end
|
|
86 }
|
45
|
87 man = {
|
|
88 title = [[The "man" Command]]
|
41
|
89 content = function()
|
|
90 %>
|
45
|
91 <p>Enter:</p>
|
|
92 <code block>
|
|
93 ~ $ man echo
|
|
94 </code>
|
|
95
|
|
96 <p>You should get:</p>
|
|
97
|
|
98 <code block>
|
|
99
|
|
100 ECHO(1) BSD General Commands Manual ECHO(1)
|
|
101
|
|
102 NAME
|
|
103 echo -- write arguments to the standard output
|
|
104
|
|
105 SYNOPSIS
|
|
106 echo [-n] [string ...]
|
|
107
|
|
108 DESCRIPTION
|
|
109 The echo utility writes any specified operands, separated by single blank
|
|
110 (` ') characters and followed by a newline (`\n') character, to the stan-
|
|
111 dard output.
|
|
112
|
|
113 The following option is available:
|
|
114
|
|
115 -n Do not print the trailing newline character. This may also be
|
|
116 achieved by appending `\c' to the end of the string, as is done by
|
|
117 iBCS2 compatible systems. Note that this option as well as the
|
|
118 effect of `\c' are implementation-defined in IEEE Std 1003.1-2001
|
|
119 (``POSIX.1'') as amended by Cor. 1-2002. Applications aiming for
|
|
120 maximum portability are strongly encouraged to use printf(1) to
|
|
121 suppress the newline character.
|
|
122 :
|
|
123 </code>
|
|
124
|
|
125 <p>But if you are on Windows, you may not have <code>man</code> installed. In that case, do:</p>
|
|
126
|
|
127 <code block>
|
|
128 ~ $ pacman -S man-db
|
|
129 </code>
|
|
130
|
|
131 <p>to install <code>man</code> as described <a href="https://packages.msys2.org/package/man-db">here</a> and then try <code>man echo</code> again.</p>
|
|
132
|
|
133 <p>The <code>man</code> command shows documentation of commands. Unfortunately it has a silly user interface based on memorizing keys, so I will just tell you the few keys you need. Down-arrow and up-arrow move down and up by one line. The space key moves down by one page. And most importantly, typing "q" quits and takes you back to Bash. You just have to memorize this.</p>
|
|
134
|
|
135 <p>Now try entering <code>man man</code>. You don't need all the stuff shown, but you can see what a complicated man page looks like. You can use <code>man</code> to get the documentation of other commands as I discuss them.</p>
|
41
|
136 <%
|
|
137 end
|
|
138 }
|
46
|
139 dirs = {
|
|
140 title = [[Directories]]
|
|
141 content = function()
|
|
142 %>
|
|
143 <p>You should be familiar with Mac Finder or Windows File Explorer, and you should know from this that directories (also called "folders") are organized into a tree.</p>
|
|
144
|
|
145 <p>On Mac:</p>
|
|
146
|
|
147 <code block>
|
|
148 ~ $ pwd
|
|
149 /Users/fschmidt
|
|
150 ~ $ open .
|
|
151 ~ $
|
|
152 </code>
|
|
153
|
|
154 <p>On Windows:</p>
|
|
155
|
|
156 <code block>
|
|
157 ~ $ pwd
|
|
158 /home/fschmidt
|
|
159 ~ $ explorer .
|
|
160 ~ $
|
|
161 </code>
|
|
162
|
|
163 <p>When using Bash, you are always in some directory, called the current directory or the working directory. <code>pwd</code> shows you the full path to this directory. Do <code>man pwd</code> for details.<p>
|
|
164
|
|
165 <p>Continuing on my Mac:</p>
|
|
166
|
|
167 <code block>
|
|
168 ~ $ mkdir learn
|
|
169 ~ $ cd learn
|
|
170 ~/learn $ pwd
|
|
171 /Users/fschmidt/learn
|
|
172 </code>
|
|
173
|
|
174 <p><code>mkdir</code> makes a directory in the current directory. You should be able to see the created directory in Mac Finder or Windows File Explorer. <code>cd</code> stands for "change directory". This changes the current directory. <code>cd</code> is a built-in command (built into Bash), and <code>man</code> isn't useful with built-in commands, so instead of <code>man cd</code>, try <code>help cd</code>. Continuing...</p>
|
|
175
|
|
176 <code block>
|
|
177 ~/learn $ pwd
|
|
178 /Users/fschmidt/learn
|
|
179 ~/learn $ ls
|
|
180 ~/learn $ touch file1
|
|
181 ~/learn $ ls
|
|
182 file1
|
|
183 ~/learn $ touch file2
|
|
184 ~/learn $ touch file3
|
|
185 ~/learn $ ls
|
|
186 file1 file2 file3
|
|
187 ~/learn $ mkdir dir1
|
|
188 ~/learn $ ls
|
|
189 dir1 file1 file2 file3
|
|
190 ~/learn $ ls -F
|
|
191 dir1/ file1 file2 file3
|
|
192 ~/learn $ ls -a
|
|
193 . .. dir1 file1 file2 file3
|
|
194 ~/learn $ ls -a -F
|
|
195 ./ ../ dir1/ file1 file2 file3
|
|
196 ~/learn $ ls -aF
|
|
197 ./ ../ dir1/ file1 file2 file3
|
|
198 </code>
|
|
199
|
|
200 <p><code>ls</code> lists files and <code>touch</code> creates an empty file. Arguments that start with "-" are options. Do <code>man ls</code> to see what the options I used do. <code>-F</code> appends a "/" to directories, and <code>-a</code> shows files starting with "." which are usually hidden. Options can be combined.</p>
|
|
201
|
|
202 <code block>
|
|
203 ~/learn $ ls file1
|
|
204 file1
|
|
205 ~/learn $ ls qqq
|
|
206 ls: qqq: No such file or directory
|
|
207 ~/learn $ ls file1 qqq file2
|
|
208 ls: qqq: No such file or directory
|
|
209 file1 file2
|
|
210 ~/learn $ ls dir1
|
|
211 ~/learn $ touch dir1/d1file
|
|
212 ~/learn $ ls dir1
|
|
213 d1file
|
|
214 ~/learn $ ls -d dir1
|
|
215 dir1
|
|
216 ~/learn $ ls file1 file2 dir1
|
|
217 file1 file2
|
|
218
|
|
219 dir1:
|
|
220 d1file
|
|
221 ~/learn $ ls -d file1 file2 dir1
|
|
222 dir1 file1 file2
|
|
223 ~/learn $ ls -dF file1 file2 dir1
|
|
224 dir1/ file1 file2
|
|
225 </code>
|
|
226
|
|
227 <p>Without file arguments, <code>ls</code> lists files in the current directory. With file arguments, it lists those files if they exist. If the file is a directory, it will list what is in the directory unless the <code>-d</code> option is used.</p>
|
|
228
|
|
229 <code block>
|
|
230 ~/learn $ ls
|
|
231 dir1 file1 file2 file3
|
|
232 ~/learn $ ls .
|
|
233 dir1 file1 file2 file3
|
|
234 ~/learn $ ls -d .
|
|
235 .
|
|
236 ~/learn $ ls -dF .
|
|
237 ./
|
|
238 ~/learn $ ls ./file1
|
|
239 ./file1
|
|
240 ~/learn $ ls dir1
|
|
241 d1file
|
|
242 ~/learn $ ls ./dir1
|
|
243 d1file
|
|
244 ~/learn $ pwd
|
|
245 /Users/fschmidt/learn
|
|
246 ~/learn $ cd .
|
|
247 ~/learn $ pwd
|
|
248 /Users/fschmidt/learn
|
|
249 </code>
|
|
250
|
|
251 <p><code>.</code> is the current directory.</p>
|
|
252
|
|
253 <code block>
|
|
254 ~/learn $ pwd
|
|
255 /Users/fschmidt/learn
|
|
256 ~/learn $ cd dir1
|
|
257 ~/learn/dir1 $ pwd
|
|
258 /Users/fschmidt/learn/dir1
|
|
259 ~/learn/dir1 $ ls .
|
|
260 d1file
|
|
261 ~/learn/dir1 $ ls ..
|
|
262 dir1 file1 file2 file3
|
|
263 ~/learn/dir1 $ cd ..
|
|
264 ~/learn $ pwd
|
|
265 /Users/fschmidt/learn
|
|
266 ~/learn $ cd dir1
|
|
267 ~/learn/dir1 $ pwd
|
|
268 /Users/fschmidt/learn/dir1
|
|
269 ~/learn/dir1 $ cd ../..
|
|
270 ~ $ pwd
|
|
271 /Users/fschmidt
|
|
272 ~ $ cd learn
|
|
273 ~/learn $ pwd
|
|
274 /Users/fschmidt/learn
|
|
275 </code>
|
|
276
|
|
277 <p><code>..</code> is the parent directory.</p>
|
|
278
|
|
279 <code block>
|
|
280 ~/learn $ echo *
|
|
281 dir1 file1 file2 file3
|
|
282 ~/learn $ echo d*
|
|
283 dir1
|
|
284 ~/learn $ echo f*
|
|
285 file1 file2 file3
|
|
286 ~/learn $ echo *1
|
|
287 dir1 file1
|
|
288 ~/learn $ echo dir1/*
|
|
289 dir1/d1file
|
|
290 ~/learn $ echo */*
|
|
291 dir1/d1file
|
|
292 ~/learn $ echo qqq*
|
|
293 qqq*
|
|
294 </code>
|
|
295
|
|
296 <p><code>*</code> does wildcard matching of files. It is important to understand that Bash does the wildcard matching and then passes the resulting arguments to the command. <code>echo</code> never sees the "*" unless there is no match.</p>
|
|
297
|
|
298 <code block>
|
|
299 ~/learn $ ls *
|
|
300 file1 file2 file3
|
|
301
|
|
302 dir1:
|
|
303 d1file
|
|
304 ~/learn $ ls -dF *
|
|
305 dir1/ file1 file2 file3
|
|
306 ~/learn $ ls -dF d*
|
|
307 dir1/
|
|
308 ~/learn $ ls -dF f*
|
|
309 file1 file2 file3
|
|
310 ~/learn $ ls -dF *1
|
|
311 dir1/ file1
|
|
312 ~/learn $ ls dir1/*
|
|
313 dir1/d1file
|
|
314 ~/learn $ ls */*
|
|
315 dir1/d1file
|
|
316 ~/learn $ ls -dF qqq*
|
|
317 ls: qqq*: No such file or directory
|
|
318 </code>
|
|
319
|
|
320 <p>Should be self-explanatory.</p>
|
|
321
|
|
322 <code block>
|
|
323 ~/learn $ pwd
|
|
324 /Users/fschmidt/learn
|
|
325 ~/learn $ cd ~
|
|
326 ~ $ pwd
|
|
327 /Users/fschmidt
|
|
328 ~ $ cd learn/dir1
|
|
329 ~/learn/dir1 $ pwd
|
|
330 /Users/fschmidt/learn/dir1
|
|
331 ~/learn/dir1 $ cd
|
|
332 ~ $ pwd
|
|
333 /Users/fschmidt
|
|
334 ~ $ cd ~/learn
|
|
335 ~/learn $ pwd
|
|
336 /Users/fschmidt/learn
|
|
337 ~/learn $ echo ~
|
|
338 /Users/fschmidt
|
|
339 ~/learn $ echo .
|
|
340 .
|
|
341 ~/learn $ echo ..
|
|
342 ..
|
|
343 </code>
|
|
344
|
|
345 <p><code>~</code> means your home directory. <code>cd</code> without arguments is the same as <code>cd ~</code>. <code>~</code> is expanded into your home directory by Bash.</p>
|
|
346
|
|
347 <code block>
|
|
348 ~/learn $ ls -ltF
|
|
349 total 0
|
|
350 drwxr-xr-x 3 fschmidt staff 96 Jan 5 02:33 dir1/
|
|
351 -rw-r--r-- 1 fschmidt staff 0 Jan 5 02:21 file3
|
|
352 -rw-r--r-- 1 fschmidt staff 0 Jan 5 02:21 file2
|
|
353 -rw-r--r-- 1 fschmidt staff 0 Jan 5 02:21 file1
|
|
354 </code>
|
|
355
|
|
356 <p><code>-l</code> gives you this ugly techy format. You get the date that the file was last modified. Before the date is the file size. <code>-t</code> sorts by date descending.</p>
|
|
357
|
|
358 <p>Lastly I will describe autocompletion. I type <code>echo d</code> without enter/return but instead then press the tab key. It autocompletes to <code>echo dir1/</code>. I press tab again and it autocompletes to <code>echo dir1/d1file</code>. Pressing tab while entering a file or directory makes Bash try to autocomplete using matching file names. If I enter <code>echo f</code> and press tab, I get <code>echo file</code>. It doesn't know which to choose next. Another tab just beeps. And another tab shows me the options like this:</p>
|
|
359
|
|
360 <code block>
|
|
361 ~/learn $ echo file
|
|
362 file1 file2 file3
|
|
363 ~/learn $ echo file
|
|
364 </code>
|
|
365
|
|
366 <p>In general, you can press tab anytime while entering a file name and see what happens. Autocompletion saves a lot of typing.</p>
|
|
367 <%
|
|
368 end
|
|
369 }
|
|
370 files = {
|
|
371 title = [[Working with Files]]
|
|
372 content = function()
|
|
373 %>
|
|
374 <code block>
|
|
375 ~/learn $ ls -F
|
|
376 dir1/ file1 file2 file3
|
|
377 ~/learn $ cp file1 copied
|
|
378 ~/learn $ ls -F
|
|
379 copied dir1/ file1 file2 file3
|
|
380 ~/learn $ mv copied moved
|
|
381 ~/learn $ ls -F
|
|
382 dir1/ file1 file2 file3 moved
|
|
383 ~/learn $ rm moved
|
|
384 ~/learn $ ls -F
|
|
385 dir1/ file1 file2 file3
|
|
386 </code>
|
|
387
|
|
388 <p><code>cp</code> copies files or directories. <code>mv</code> moves files or directories. <code>rm</code> removes files or directories. See the <code>man</code> pages of these commands for details.</p>
|
|
389
|
|
390 <code block>
|
|
391 ~/learn $ ls -F
|
|
392 dir1/ file1 file2 file3
|
|
393 ~/learn $ mkdir dir2
|
|
394 ~/learn $ touch dir2/d2file
|
|
395 ~/learn $ ls -F
|
|
396 dir1/ dir2/ file1 file2 file3
|
|
397 ~/learn $ ls dir2
|
|
398 d2file
|
|
399 ~/learn $ rm dir2
|
|
400 rm: dir2: is a directory
|
|
401 ~/learn $ rm -d dir2
|
|
402 rm: dir2: Directory not empty
|
|
403 ~/learn $ rm dir2/d2file
|
|
404 ~/learn $ rm -d dir2
|
|
405 ~/learn $ ls -F
|
|
406 dir1/ file1 file2 file3
|
|
407 </code>
|
|
408
|
|
409 <code block>
|
|
410 ~/learn $ ls -F
|
|
411 dir1/ file1 file2 file3
|
|
412 ~/learn $ mkdir dir2
|
|
413 ~/learn $ touch dir2/d2file
|
|
414 ~/learn $ ls -F
|
|
415 dir1/ dir2/ file1 file2 file3
|
47
|
416 ~/learn $ rm -r dir2
|
46
|
417 ~/learn $ ls -F
|
|
418 dir1/ file1 file2 file3
|
|
419 </code>
|
|
420
|
|
421 <code block>
|
|
422 ~/learn $ ls -F
|
|
423 dir1/ file1 file2 file3
|
|
424 ~/learn $ cp dir1 dir2
|
|
425 cp: dir1 is a directory (not copied).
|
47
|
426 ~/learn $ cp -r dir1 dir2
|
46
|
427 ~/learn $ ls -F
|
|
428 dir1/ dir2/ file1 file2 file3
|
|
429 ~/learn $ ls dir2
|
|
430 d1file
|
|
431 ~/learn $ cp f* dir2
|
|
432 ~/learn $ ls dir2
|
|
433 d1file file1 file2 file3
|
47
|
434 ~/learn $ rm -r dir2
|
46
|
435 ~/learn $ ls -F
|
|
436 dir1/ file1 file2 file3
|
|
437 </code>
|
|
438
|
|
439 <code block>
|
|
440 ~/learn $ ls -F
|
|
441 dir1/ file1 file2 file3
|
|
442 ~/learn $ mkdir dir2
|
47
|
443 ~/learn $ cp -r dir1 dir2
|
46
|
444 ~/learn $ ls -F dir2
|
|
445 dir1/
|
|
446 ~/learn $ ls -F dir2/dir1
|
|
447 d1file
|
47
|
448 ~/learn $ rm -r dir2
|
46
|
449 ~/learn $ ls -F
|
|
450 dir1/ file1 file2 file3
|
|
451 </code>
|
|
452
|
|
453 <p>I could explain all this, but I won't. You should learn to understand commands and their options using <code>man</code> and by playing with them. Don't continue until you completely understand the above.</p>
|
|
454 <%
|
|
455 end
|
|
456 }
|
|
457 quote = {
|
|
458 title = [[Quoting]]
|
|
459 content = function()
|
|
460 %>
|
|
461
|
|
462 <code block>
|
|
463 ~/learn $ echo a b
|
|
464 a b
|
|
465 ~/learn $ echo "a b"
|
|
466 a b
|
|
467 ~/learn $ echo 'a b'
|
|
468 a b
|
|
469 ~/learn $ echo "a b" c
|
|
470 a b c
|
|
471 </code>
|
|
472
|
|
473 <p>Bash treats text in quotes as one argument. So in <code>echo a b</code>, <code>echo</code> has two arguments: "a" and "b". In <code>echo "a b"</code>, <code>echo</code> has one argument: "<span pre>a b</span>". In <code>echo 'a b'</code>, <code>echo</code> has one argument: "<span pre>a b</span>". In <code>echo "a b" c</code>, <code>echo</code> has two arguments: "<span pre>a b</span>" and "c".</p>
|
|
474
|
|
475 <code block>
|
|
476 ~/learn $ echo a\ \ \ b
|
|
477 a b
|
|
478 </code>
|
|
479
|
|
480 <p>Outside of quotes, <code>\ </code> is not treated as a separator, but rather is treated as a space character that is part of the argument.</p>
|
|
481
|
|
482 <%
|
|
483 end
|
|
484 }
|
|
485 vars = {
|
|
486 title = [[Variables]]
|
|
487 content = function()
|
|
488 %>
|
|
489
|
|
490 <code block>
|
|
491 ~/learn $ echo $X
|
|
492
|
|
493 ~/learn $ X="some text"
|
|
494 ~/learn $ echo $X
|
|
495 some text
|
|
496 ~/learn $ echo "X is: $X"
|
|
497 X is: some text
|
|
498 ~/learn $ echo 'X is: $X'
|
|
499 X is: $X
|
|
500 ~/learn $ X="$X and more"
|
|
501 ~/learn $ echo $X
|
|
502 some text and more
|
|
503 </code>
|
|
504
|
|
505 <p>Here <code>X</code> is a variable. You get its value with <code>$X</code>. This also works inside double-quotes but not inside single-quotes.</p>
|
47
|
506
|
|
507 <p>There are special variables called environment variables that are used by Bash.</p>
|
|
508
|
|
509 <code block>
|
|
510 ~/learn $ echo $PATH
|
|
511 /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/fschmidt/Dropbox/bin:/Users/fschmidt/hg/luan/scripts:/usr/local/opt/postgresql@9.5/bin
|
|
512 ~/learn $ which ls
|
|
513 /bin/ls
|
|
514 ~/learn $ cd /bin
|
|
515 /bin $ pwd
|
|
516 /bin
|
|
517 /bin $ ls
|
|
518 [ dd launchctl pwd test
|
|
519 bash df link rm unlink
|
|
520 cat echo ln rmdir wait4path
|
|
521 chmod ed ls sh zsh
|
|
522 cp expr mkdir sleep
|
|
523 csh hostname mv stty
|
|
524 dash kill pax sync
|
|
525 date ksh ps tcsh
|
|
526 /bin $ ls -F
|
|
527 [* dd* launchctl* pwd* test*
|
|
528 bash* df* link* rm* unlink*
|
|
529 cat* echo* ln* rmdir* wait4path*
|
|
530 chmod* ed* ls* sh* zsh*
|
|
531 cp* expr* mkdir* sleep*
|
|
532 csh* hostname* mv* stty*
|
|
533 dash* kill* pax* sync*
|
|
534 date* ksh* ps* tcsh*
|
|
535 /bin $ cd ~/learn
|
|
536 ~/learn $
|
|
537 </code>
|
|
538
|
|
539 <p><code>PATH</code> is an environment variable containing a list of directories separated by <code>:</code> that are searched for commands by Bash. The <code>which</code> command shows the full path to a command. <code>ls -F</code> appends a <code>*</code> to executable commands.</p>
|
|
540
|
|
541 <code block>
|
|
542 ~/learn $ subl file1
|
|
543 -bash: subl: command not found
|
|
544 ~/learn $ "/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl" file1
|
|
545 ~/learn $ PATH="$PATH:/Applications/Sublime Text.app/Contents/SharedSupport/bin"
|
|
546 ~/learn $ echo $PATH
|
|
547 /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/fschmidt/Dropbox/bin:/Users/fschmidt/hg/luan/scripts:/usr/local/opt/postgresql@9.5/bin:/Applications/Sublime Text.app/Contents/SharedSupport/bin
|
|
548 ~/learn $ subl file1
|
|
549 ~/learn $
|
|
550 </code>
|
|
551
|
|
552 <p>Here I edit the file <code>file1</code> with <a href="http://localhost:8080/learn.html#editor">Sublime Text</a>, first by using the full path, and then by adding the directory to <code>PATH</code> so that Bash can find <code>subl</code>.</p>
|
|
553
|
|
554 <p>I have Microsoft Word on Windows. From the Windows Command Prompt (not Bash):</p>
|
|
555
|
|
556 <code block>
|
|
557 C:\Users\fschmidt>winword
|
|
558
|
|
559 C:\Users\fschmidt>where winword
|
|
560 C:\Program Files\Microsoft Office\root\Office16\WINWORD.EXE
|
|
561 </code>
|
|
562
|
|
563 <p><code>winword</code> runs Microsoft Word. The Command Prompt <code>where</code> command is like the Bash <code>which</code> command. So now on MSYS2:</p>
|
|
564
|
|
565 <code block>
|
|
566 ~ $ winword
|
|
567 bash: winword: command not found
|
|
568 ~ $ echo $PATH
|
|
569 /usr/local/bin:/usr/bin:/bin:/opt/bin:/c/Windows/System32:/c/Windows:/c/Windows/System32/Wbem:/c/Windows/System32/WindowsPowerShell/v1.0/:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl:/c/Program Files/TortoiseHg:/c/Program Files/Java/jdk1.8.0_202/bin
|
|
570 ~ $ PATH="$PATH:/c/Program Files/Microsoft Office/root/Office16"
|
|
571 ~ $ echo $PATH
|
|
572 /usr/local/bin:/usr/bin:/bin:/opt/bin:/c/Windows/System32:/c/Windows:/c/Windows/System32/Wbem:/c/Windows/System32/WindowsPowerShell/v1.0/:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl:/c/Program Files/TortoiseHg:/c/Program Files/Java/jdk1.8.0_202/bin:/c/Program Files/Microsoft Office/root/Office16
|
|
573 ~ $ winword
|
|
574 ~ $
|
|
575 </code>
|
|
576
|
|
577 <p>Returning to the Mac, there is another way to run applications found in Finder's "Applications" simply as applications instead of as commands.</p>
|
|
578
|
|
579 <code block>
|
|
580 ~/learn $ open -a 'Sublime Text' file1
|
|
581 </code>
|
|
582
|
|
583 <p>Another useful environment variable is <code>PS1</code> which controls the command prompt. I already have this set up, but if I didn't:</p>
|
|
584
|
|
585 <code block>
|
|
586 Franklins-MacBook-Pro:learn fschmidt$ echo $PS1
|
|
587 \h:\W \u\$
|
|
588 Franklins-MacBook-Pro:learn fschmidt$ PS1="\w $ "
|
|
589 ~/learn $ echo $PS1
|
|
590 \w $
|
|
591 ~/learn $
|
|
592 </code>
|
|
593
|
|
594 <p>Google "bash PS1" for more info.</p>
|
|
595
|
46
|
596 <%
|
|
597 end
|
|
598 }
|
|
599 bash_profile = {
|
|
600 title = [[.bash_profile]]
|
|
601 content = function()
|
|
602 %>
|
47
|
603 <code block>
|
|
604 ~/learn $ cd
|
|
605 ~ $ ls .bash_profile
|
|
606 .bash_profile
|
|
607 </code>
|
|
608
|
|
609 <p>If <code>.bash_profile</code> isn't found then do <code>touch .bash_profile</code> to create it. This file contains Bash commands that are run when Bash starts. If you already have this file, it is likely to contain comments that start with <code>#</code>. Comments are ignored like this:</p>
|
|
610
|
|
611 <code block>
|
|
612 ~ $ # comment line, does nothing
|
|
613 ~ $ echo whatever # end of line comment
|
|
614 whatever
|
|
615 ~ $
|
|
616 </code>
|
|
617
|
|
618 <p>To edit <code>.bash_profile</code> on a Mac, you can do:</p>
|
|
619
|
|
620 <code block>
|
|
621 ~ $ open -a 'Sublime Text' .bash_profile
|
|
622 </code>
|
|
623
|
|
624 <p>To edit <code>.bash_profile</code> on Windows, you can do:</p>
|
|
625
|
|
626 <code block>
|
|
627 ~ $ notepad .bash_profile
|
|
628 </code>
|
|
629
|
|
630 <p>Now try adding this line to <code>.bash_profile</code>:</p>
|
|
631
|
|
632 <code block>
|
|
633 echo hello there
|
|
634 </code>
|
|
635
|
|
636 <p>Now when you open a new Bash terminal, you should see "hello there". <code>.bash_profile</code> runs when Bash is started by opening a new Bash terminal.</p>
|
|
637
|
|
638 <p>I set <code>PS1</code> and <code>PATH</code> in <code>.bash_profile</code> to have the command prompt I want, and access to the commands that I want. I suggest that you make the <a href="https://www.sublimetext.com/docs/command_line.html">Sublime Text command</a> <code>subl</code> available in <code>PATH</code>.</p>
|
|
639
|
|
640 <%
|
|
641 end
|
|
642 }
|
|
643 ctrl_c = {
|
|
644 title = [[Control+c]]
|
|
645 content = function()
|
|
646 %>
|
|
647 <code block>
|
|
648 ~/learn $ sleep 3
|
|
649 ~/learn $ sleep 30
|
|
650 ^C
|
|
651 ~/learn $
|
|
652 </code>
|
|
653
|
|
654 <p><code>sleep 3</code> sleeps for 3 seconds, meaning it does nothing for 3 seconds. I waited 3 seconds for this command to finish. Then I ran <code>sleep 30</code> which would sleep for 30 seconds, but I lost my patience and pressed control+c which interrupts the program and breaks out of it. You can try control+c if you ever get stuck waiting for a command to finish.</p>
|
46
|
655 <%
|
|
656 end
|
|
657 }
|
45
|
658 later = {
|
|
659 title = [[placeholder]]
|
41
|
660 content = function()
|
|
661 %>
|
|
662 <p>later</p>
|
|
663 <%
|
|
664 end
|
|
665 }
|
40
|
666 }
|
|
667
|
|
668
|
|
669 local function show_toc(content)
|
|
670 %>
|
|
671 <ul>
|
|
672 <%
|
|
673 for id, info in pairs(content) do
|
|
674 %>
|
|
675 <li><a id="c_<%=id%>" href="#<%=id%>"><%=info.title%></a></li>
|
|
676 <%
|
|
677 end
|
|
678 %>
|
|
679 </ul>
|
|
680 <%
|
|
681 end
|
|
682
|
|
683 local function show_content(content,h)
|
|
684 for id, info in pairs(content) do
|
|
685 %>
|
|
686 <div heading>
|
|
687 <h<%=h%>><a id="<%=id%>" href="#<%=id%>"><%=info.title%></a></h<%=h%>>
|
|
688 <a href="#c_<%=id%>">contents</a>
|
|
689 </div>
|
|
690 <%
|
|
691 info.content()
|
|
692 end
|
|
693 end
|
|
694
|
|
695 return function()
|
|
696 Io.stdout = Http.response.text_writer()
|
|
697 %>
|
|
698 <!doctype html>
|
|
699 <html>
|
|
700 <head>
|
|
701 <% head() %>
|
45
|
702 <title>Reactionary Bash Tutorial</title>
|
40
|
703 </head>
|
|
704 <body>
|
|
705 <% header() %>
|
|
706 <div content>
|
45
|
707 <h1><a href="learn.html">Reactionary Bash Tutorial</a></h1>
|
40
|
708 <hr>
|
|
709 <h2>Contents</h2>
|
|
710 <div toc>
|
|
711 <% show_toc(content) %>
|
|
712 </div>
|
|
713 <hr>
|
|
714 <% show_content(content,2) %>
|
|
715 </div>
|
|
716 </body>
|
|
717 </html>
|
|
718 <%
|
|
719 end
|