Mercurial Hosting > arkian
annotate src/computer_literacy/bash.html @ 27:9134d56639ae default tip
finish computer_literacy
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 09 Oct 2025 19:15:03 -0600 |
parents | fb87f762847e |
children |
rev | line source |
---|---|
26 | 1 <!doctype html> |
2 <html lang="en"> | |
3 <head> | |
4 <script src="/site.js"></script> | |
5 <script> head() </script> | |
6 <title>Arkian - Bash</title> | |
7 <script> | |
8 'use strict'; | |
9 | |
10 let content = { | |
11 intro: { | |
12 title: 'Introduction', | |
13 content: `\ | |
27
9134d56639ae
finish computer_literacy
Franklin Schmidt <fschmidt@gmail.com>
parents:
26
diff
changeset
|
14 <p><a href="https://www.gnu.org/software/bash/">Bash</a> is a <a href="https://en.wikipedia.org/wiki/Unix_shell">shell</a>, one of many, but the one I prefer. 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> |
26 | 15 ` , |
16 }, | |
17 access: { | |
18 title: 'Running Bash', | |
19 content: `\ | |
20 <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> | |
21 ` , | |
22 }, | |
23 start: { | |
24 title: 'Getting Started', | |
25 content: `\ | |
26 <p>When I start Bash on my Mac I see:</p> | |
27 | |
28 <code block> | |
29 Last login: Thu Jan 4 23:25:35 on ttys004 | |
30 | |
31 The default interactive shell is now zsh. | |
32 To update your account to use zsh, please run 'chsh -s /bin/zsh'. | |
33 For more details, please visit https://support.apple.com/kb/HT208050. | |
34 ~ $ | |
35 | |
36 </code> | |
37 | |
38 <p>On Windows - MSYS2 I just see:</p> | |
39 | |
40 <code block> | |
41 ~ $ | |
42 | |
43 </code> | |
44 | |
45 <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> | |
46 | |
47 <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> | |
48 | |
49 <code block> | |
50 ~ $ qqq | |
51 -bash: qqq: command not found | |
52 ~ $ | |
53 </code> | |
54 | |
55 <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> | |
56 | |
57 <code block> | |
58 ~ $ echo hi | |
59 hi | |
60 ~ $ echo how are you | |
61 how are you | |
62 ~ $ echo bye | |
63 bye | |
64 </code> | |
65 | |
66 <p>The <code>echo</code> command just echoes 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> | |
67 | |
68 <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> | |
69 | |
70 <code block> | |
71 ~ $ echo how are you | |
72 how are you | |
73 </code> | |
74 | |
75 <p><code>echo</code> just returns the arguments separated by one space.</p> | |
76 | |
77 <code block> | |
78 ~ $ echo one; echo two | |
79 one | |
80 two | |
81 </code> | |
82 | |
83 <p>You can put multiple commands on one line separated by a <code>;</code>.</p> | |
84 ` , | |
85 }, | |
86 man: { | |
87 title: 'The "man" Command', | |
88 content: `\ | |
89 <p>Enter:</p> | |
90 <code block> | |
91 ~ $ man echo | |
92 </code> | |
93 | |
94 <p>You should get something like:</p> | |
95 | |
96 <code block> | |
97 | |
98 ECHO(1) BSD General Commands Manual ECHO(1) | |
99 | |
100 NAME | |
101 echo -- write arguments to the standard output | |
102 | |
103 SYNOPSIS | |
104 echo [-n] [string ...] | |
105 | |
106 DESCRIPTION | |
107 The echo utility writes any specified operands, separated by single blank | |
108 (' ') characters and followed by a newline ('\\n') character, to the stan- | |
109 dard output. | |
110 | |
111 The following option is available: | |
112 | |
113 -n Do not print the trailing newline character. This may also be | |
114 achieved by appending '\\c' to the end of the string, as is done by | |
115 iBCS2 compatible systems. Note that this option as well as the | |
116 effect of '\\c' are implementation-defined in IEEE Std 1003.1-2001 | |
117 (''POSIX.1'') as amended by Cor. 1-2002. Applications aiming for | |
118 maximum portability are strongly encouraged to use printf(1) to | |
119 suppress the newline character. | |
120 : | |
121 </code> | |
122 | |
123 <p>But if you are on Windows, you may not have <code>man</code> installed. In that case, do:</p> | |
124 | |
125 <code block> | |
126 ~ $ pacman -S man-db | |
127 </code> | |
128 | |
129 <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> | |
130 | |
131 <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> | |
132 | |
133 <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> | |
134 ` , | |
135 }, | |
136 dirs: { | |
137 title: 'Directories', | |
138 content: `\ | |
139 <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> | |
140 | |
141 <p>On Mac:</p> | |
142 | |
143 <code block> | |
144 ~ $ pwd | |
145 /Users/fschmidt | |
146 ~ $ open . | |
147 ~ $ | |
148 </code> | |
149 | |
150 <p>On Windows:</p> | |
151 | |
152 <code block> | |
153 ~ $ pwd | |
154 /home/fschmidt | |
155 ~ $ explorer . | |
156 ~ $ | |
157 </code> | |
158 | |
159 <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. <code>open .</code> should open the Mac Finder for the current directory, and <code>explorer .</code> should open the Windows File Explorer for the current directory.<p> | |
160 | |
161 <p>Continuing on my Mac:</p> | |
162 | |
163 <code block> | |
164 ~ $ mkdir learn | |
165 ~ $ cd learn | |
166 ~/learn $ pwd | |
167 /Users/fschmidt/learn | |
168 </code> | |
169 | |
170 <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> | |
171 | |
172 <code block> | |
173 ~/learn $ pwd | |
174 /Users/fschmidt/learn | |
175 ~/learn $ ls | |
176 ~/learn $ touch file1 | |
177 ~/learn $ ls | |
178 file1 | |
179 ~/learn $ touch file2 | |
180 ~/learn $ touch file3 | |
181 ~/learn $ ls | |
182 file1 file2 file3 | |
183 ~/learn $ mkdir dir1 | |
184 ~/learn $ ls | |
185 dir1 file1 file2 file3 | |
186 ~/learn $ ls -F | |
187 dir1/ file1 file2 file3 | |
188 ~/learn $ ls -a | |
189 . .. dir1 file1 file2 file3 | |
190 ~/learn $ ls -a -F | |
191 ./ ../ dir1/ file1 file2 file3 | |
192 ~/learn $ ls -aF | |
193 ./ ../ dir1/ file1 file2 file3 | |
194 </code> | |
195 | |
196 <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> | |
197 | |
198 <code block> | |
199 ~/learn $ ls file1 | |
200 file1 | |
201 ~/learn $ ls qqq | |
202 ls: qqq: No such file or directory | |
203 ~/learn $ ls file1 qqq file2 | |
204 ls: qqq: No such file or directory | |
205 file1 file2 | |
206 ~/learn $ ls dir1 | |
207 ~/learn $ touch dir1/d1file | |
208 ~/learn $ ls dir1 | |
209 d1file | |
210 ~/learn $ ls -d dir1 | |
211 dir1 | |
212 ~/learn $ ls file1 file2 dir1 | |
213 file1 file2 | |
214 | |
215 dir1: | |
216 d1file | |
217 ~/learn $ ls -d file1 file2 dir1 | |
218 dir1 file1 file2 | |
219 ~/learn $ ls -dF file1 file2 dir1 | |
220 dir1/ file1 file2 | |
221 </code> | |
222 | |
223 <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> | |
224 | |
225 <code block> | |
226 ~/learn $ ls | |
227 dir1 file1 file2 file3 | |
228 ~/learn $ ls . | |
229 dir1 file1 file2 file3 | |
230 ~/learn $ ls -d . | |
231 . | |
232 ~/learn $ ls -dF . | |
233 ./ | |
234 ~/learn $ ls ./file1 | |
235 ./file1 | |
236 ~/learn $ ls dir1 | |
237 d1file | |
238 ~/learn $ ls ./dir1 | |
239 d1file | |
240 ~/learn $ pwd | |
241 /Users/fschmidt/learn | |
242 ~/learn $ cd . | |
243 ~/learn $ pwd | |
244 /Users/fschmidt/learn | |
245 </code> | |
246 | |
247 <p><code>.</code> is the current directory.</p> | |
248 | |
249 <code block> | |
250 ~/learn $ pwd | |
251 /Users/fschmidt/learn | |
252 ~/learn $ cd dir1 | |
253 ~/learn/dir1 $ pwd | |
254 /Users/fschmidt/learn/dir1 | |
255 ~/learn/dir1 $ ls . | |
256 d1file | |
257 ~/learn/dir1 $ ls .. | |
258 dir1 file1 file2 file3 | |
259 ~/learn/dir1 $ cd .. | |
260 ~/learn $ pwd | |
261 /Users/fschmidt/learn | |
262 ~/learn $ cd dir1 | |
263 ~/learn/dir1 $ pwd | |
264 /Users/fschmidt/learn/dir1 | |
265 ~/learn/dir1 $ cd ../.. | |
266 ~ $ pwd | |
267 /Users/fschmidt | |
268 ~ $ cd learn | |
269 ~/learn $ pwd | |
270 /Users/fschmidt/learn | |
271 </code> | |
272 | |
273 <p><code>..</code> is the parent directory.</p> | |
274 | |
275 <code block> | |
276 ~/learn $ echo * | |
277 dir1 file1 file2 file3 | |
278 ~/learn $ echo d* | |
279 dir1 | |
280 ~/learn $ echo f* | |
281 file1 file2 file3 | |
282 ~/learn $ echo *1 | |
283 dir1 file1 | |
284 ~/learn $ echo dir1/* | |
285 dir1/d1file | |
286 ~/learn $ echo */* | |
287 dir1/d1file | |
288 ~/learn $ echo qqq* | |
289 qqq* | |
290 </code> | |
291 | |
292 <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> | |
293 | |
294 <code block> | |
295 ~/learn $ ls * | |
296 file1 file2 file3 | |
297 | |
298 dir1: | |
299 d1file | |
300 ~/learn $ ls -dF * | |
301 dir1/ file1 file2 file3 | |
302 ~/learn $ ls -dF d* | |
303 dir1/ | |
304 ~/learn $ ls -dF f* | |
305 file1 file2 file3 | |
306 ~/learn $ ls -dF *1 | |
307 dir1/ file1 | |
308 ~/learn $ ls dir1/* | |
309 dir1/d1file | |
310 ~/learn $ ls */* | |
311 dir1/d1file | |
312 ~/learn $ ls -dF qqq* | |
313 ls: qqq*: No such file or directory | |
314 </code> | |
315 | |
316 <p>Should be self-explanatory.</p> | |
317 | |
318 <code block> | |
319 ~/learn $ pwd | |
320 /Users/fschmidt/learn | |
321 ~/learn $ cd ~ | |
322 ~ $ pwd | |
323 /Users/fschmidt | |
324 ~ $ cd learn/dir1 | |
325 ~/learn/dir1 $ pwd | |
326 /Users/fschmidt/learn/dir1 | |
327 ~/learn/dir1 $ cd | |
328 ~ $ pwd | |
329 /Users/fschmidt | |
330 ~ $ cd ~/learn | |
331 ~/learn $ pwd | |
332 /Users/fschmidt/learn | |
333 ~/learn $ echo ~ | |
334 /Users/fschmidt | |
335 ~/learn $ echo . | |
336 . | |
337 ~/learn $ echo .. | |
338 .. | |
339 </code> | |
340 | |
341 <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> | |
342 | |
343 <code block> | |
344 ~/learn $ ls -ltF | |
345 total 0 | |
346 drwxr-xr-x 3 fschmidt staff 96 Jan 5 02:33 dir1/ | |
347 -rw-r--r-- 1 fschmidt staff 0 Jan 5 02:21 file3 | |
348 -rw-r--r-- 1 fschmidt staff 0 Jan 5 02:21 file2 | |
349 -rw-r--r-- 1 fschmidt staff 0 Jan 5 02:21 file1 | |
350 </code> | |
351 | |
352 <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> | |
353 | |
354 <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> | |
355 | |
356 <code block> | |
357 ~/learn $ echo file | |
358 file1 file2 file3 | |
359 ~/learn $ echo file | |
360 </code> | |
361 | |
362 <p>In general, you can press tab anytime while entering a file name and see what happens. Autocompletion saves a lot of typing.</p> | |
363 ` , | |
364 }, | |
365 files: { | |
366 title: 'Working with Files', | |
367 content: `\ | |
368 <code block> | |
369 ~/learn $ ls -F | |
370 dir1/ file1 file2 file3 | |
371 ~/learn $ cp file1 copied | |
372 ~/learn $ ls -F | |
373 copied dir1/ file1 file2 file3 | |
374 ~/learn $ mv copied moved | |
375 ~/learn $ ls -F | |
376 dir1/ file1 file2 file3 moved | |
377 ~/learn $ rm moved | |
378 ~/learn $ ls -F | |
379 dir1/ file1 file2 file3 | |
380 </code> | |
381 | |
382 <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> | |
383 | |
384 <code block> | |
385 ~/learn $ ls -F | |
386 dir1/ file1 file2 file3 | |
387 ~/learn $ mkdir dir2 | |
388 ~/learn $ touch dir2/d2file | |
389 ~/learn $ ls -F | |
390 dir1/ dir2/ file1 file2 file3 | |
391 ~/learn $ ls dir2 | |
392 d2file | |
393 ~/learn $ rm dir2 | |
394 rm: dir2: is a directory | |
395 ~/learn $ rm -d dir2 | |
396 rm: dir2: Directory not empty | |
397 ~/learn $ rm dir2/d2file | |
398 ~/learn $ rm -d dir2 | |
399 ~/learn $ ls -F | |
400 dir1/ file1 file2 file3 | |
401 </code> | |
402 | |
403 <code block> | |
404 ~/learn $ ls -F | |
405 dir1/ file1 file2 file3 | |
406 ~/learn $ mkdir dir2 | |
407 ~/learn $ touch dir2/d2file | |
408 ~/learn $ ls -F | |
409 dir1/ dir2/ file1 file2 file3 | |
410 ~/learn $ rm -r dir2 | |
411 ~/learn $ ls -F | |
412 dir1/ file1 file2 file3 | |
413 </code> | |
414 | |
415 <code block> | |
416 ~/learn $ ls -F | |
417 dir1/ file1 file2 file3 | |
418 ~/learn $ cp dir1 dir2 | |
419 cp: dir1 is a directory (not copied). | |
420 ~/learn $ cp -r dir1 dir2 | |
421 ~/learn $ ls -F | |
422 dir1/ dir2/ file1 file2 file3 | |
423 ~/learn $ ls dir2 | |
424 d1file | |
425 ~/learn $ cp f* dir2 | |
426 ~/learn $ ls dir2 | |
427 d1file file1 file2 file3 | |
428 ~/learn $ rm -r dir2 | |
429 ~/learn $ ls -F | |
430 dir1/ file1 file2 file3 | |
431 </code> | |
432 | |
433 <code block> | |
434 ~/learn $ ls -F | |
435 dir1/ file1 file2 file3 | |
436 ~/learn $ mkdir dir2 | |
437 ~/learn $ cp -r dir1 dir2 | |
438 ~/learn $ ls -F dir2 | |
439 dir1/ | |
440 ~/learn $ ls -F dir2/dir1 | |
441 d1file | |
442 ~/learn $ rm -r dir2 | |
443 ~/learn $ ls -F | |
444 dir1/ file1 file2 file3 | |
445 </code> | |
446 | |
447 <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> | |
448 ` , | |
449 }, | |
450 quote: { | |
451 title: 'Quoting', | |
452 content: `\ | |
453 <code block> | |
454 ~/learn $ echo a b | |
455 a b | |
456 ~/learn $ echo "a b" | |
457 a b | |
458 ~/learn $ echo 'a b' | |
459 a b | |
460 ~/learn $ echo "a b" c | |
461 a b c | |
462 </code> | |
463 | |
464 <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> | |
465 | |
466 <code block> | |
467 ~/learn $ echo a\\ \\ \\ b | |
468 a b | |
469 </code> | |
470 | |
471 <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> | |
472 | |
473 <code block> | |
474 ~/learn $ echo file* | |
475 file1 file2 file3 | |
476 ~/learn $ echo "file*" | |
477 file* | |
478 ~/learn $ echo 'file*' | |
479 file* | |
480 </code> | |
481 | |
482 <p>Quotes prevent wildcard expansion.</p> | |
483 ` , | |
484 }, | |
485 vars: { | |
486 title: 'Variables', | |
487 content: `\ | |
488 <code block> | |
489 ~/learn $ echo $X | |
490 | |
491 ~/learn $ X="some text" | |
492 ~/learn $ echo $X | |
493 some text | |
494 ~/learn $ echo "X is: $X" | |
495 X is: some text | |
496 ~/learn $ echo 'X is: $X' | |
497 X is: $X | |
498 ~/learn $ X="$X and more" | |
499 ~/learn $ echo $X | |
500 some text and more | |
501 </code> | |
502 | |
503 <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> | |
504 | |
505 <p>There are special variables called environment variables that are used by Bash.</p> | |
506 | |
507 <code block> | |
508 ~/learn $ echo $PATH | |
509 /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 | |
510 ~/learn $ which ls | |
511 /bin/ls | |
512 ~/learn $ cd /bin | |
513 /bin $ pwd | |
514 /bin | |
515 /bin $ ls | |
516 [ dd launchctl pwd test | |
517 bash df link rm unlink | |
518 cat echo ln rmdir wait4path | |
519 chmod ed ls sh zsh | |
520 cp expr mkdir sleep | |
521 csh hostname mv stty | |
522 dash kill pax sync | |
523 date ksh ps tcsh | |
524 /bin $ ls -F | |
525 [* dd* launchctl* pwd* test* | |
526 bash* df* link* rm* unlink* | |
527 cat* echo* ln* rmdir* wait4path* | |
528 chmod* ed* ls* sh* zsh* | |
529 cp* expr* mkdir* sleep* | |
530 csh* hostname* mv* stty* | |
531 dash* kill* pax* sync* | |
532 date* ksh* ps* tcsh* | |
533 /bin $ cd ~/learn | |
534 ~/learn $ | |
535 </code> | |
536 | |
537 <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 files.</p> | |
538 | |
539 <code block> | |
540 ~/learn $ subl file1 | |
541 -bash: subl: command not found | |
542 ~/learn $ "/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl" file1 | |
543 ~/learn $ PATH="$PATH:/Applications/Sublime Text.app/Contents/SharedSupport/bin" | |
544 ~/learn $ echo $PATH | |
545 /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 | |
546 ~/learn $ subl file1 | |
547 ~/learn $ | |
548 </code> | |
549 | |
27
9134d56639ae
finish computer_literacy
Franklin Schmidt <fschmidt@gmail.com>
parents:
26
diff
changeset
|
550 <p>Here I edit the file <code>file1</code> with <a href="computer_literacy.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> |
26 | 551 |
552 <p>I have Microsoft Word on Windows. From the Windows Command Prompt (not Bash):</p> | |
553 | |
554 <code block> | |
555 C:\\Users\\fschmidt>winword | |
556 | |
557 C:\\Users\\fschmidt>where winword | |
558 C:\\Program Files\\Microsoft Office\\root\\Office16\\WINWORD.EXE | |
559 </code> | |
560 | |
561 <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> | |
562 | |
563 <code block> | |
564 ~ $ winword | |
565 bash: winword: command not found | |
566 ~ $ echo $PATH | |
567 /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 | |
568 ~ $ PATH="$PATH:/c/Program Files/Microsoft Office/root/Office16" | |
569 ~ $ echo $PATH | |
570 /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 | |
571 ~ $ winword | |
572 ~ $ | |
573 </code> | |
574 | |
575 <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> | |
576 | |
577 <code block> | |
578 ~/learn $ open -a 'Sublime Text' file1 | |
579 </code> | |
580 | |
581 <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> | |
582 | |
583 <code block> | |
584 Franklins-MacBook-Pro:learn fschmidt$ echo $PS1 | |
585 \\h:\\W \\u\\$ | |
586 Franklins-MacBook-Pro:learn fschmidt$ PS1="\\w $ " | |
587 ~/learn $ echo $PS1 | |
588 \\w $ | |
589 ~/learn $ | |
590 </code> | |
591 | |
592 <p>Google "bash PS1" for more info.</p> | |
593 ` , | |
594 }, | |
595 bash_profile: { | |
596 title: '.bash_profile', | |
597 content: `\ | |
598 <code block> | |
599 ~/learn $ cd | |
600 ~ $ ls .bash_profile | |
601 .bash_profile | |
602 </code> | |
603 | |
604 <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> | |
605 | |
606 <code block> | |
607 ~ $ # comment line, does nothing | |
608 ~ $ echo whatever # end of line comment | |
609 whatever | |
610 ~ $ | |
611 </code> | |
612 | |
613 <p>To edit <code>.bash_profile</code> on a Mac, you can do:</p> | |
614 | |
615 <code block> | |
616 ~ $ open -a 'Sublime Text' .bash_profile | |
617 </code> | |
618 | |
619 <p>To edit <code>.bash_profile</code> on Windows, you can do:</p> | |
620 | |
621 <code block> | |
622 ~ $ notepad .bash_profile | |
623 </code> | |
624 | |
625 <p>Now try adding this line to <code>.bash_profile</code>:</p> | |
626 | |
627 <code block> | |
628 echo hello there | |
629 </code> | |
630 | |
631 <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> | |
632 | |
633 <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> | |
634 ` , | |
635 }, | |
636 find: { | |
637 title: 'The "find" Command', | |
638 content: `\ | |
639 <code block> | |
640 ~/learn $ find . | |
641 . | |
642 ./file3 | |
643 ./file2 | |
644 ./file1 | |
645 ./dir1 | |
646 ./dir1/d1file | |
647 ~/learn $ find . -name 'file*' | |
648 ./file3 | |
649 ./file2 | |
650 ./file1 | |
651 ~/learn $ find . -name '*file' | |
652 ./dir1/d1file | |
653 ~/learn $ find . -name 'd*' | |
654 ./dir1 | |
655 ./dir1/d1file | |
656 ~/learn $ find . -name '*1' -or -name '*2' | |
657 ./file2 | |
658 ./file1 | |
659 ./dir1 | |
660 </code> | |
661 | |
662 <p><code>find</code> recursively searches for files in a directory tree. Note that in this case the <code>*</code> wildcard matching is not being done by Bash, it is being done by <code>find</code>. <code>find</code> has many options for searching for files and acting on them, see <code>man find</code>.</p> | |
663 ` , | |
664 }, | |
665 io: { | |
666 title: 'Input and Output', | |
667 content: `\ | |
668 <code block> | |
669 ~/learn $ echo 'this is a test' >test.txt | |
670 ~/learn $ ls -F | |
671 dir1/ file1 file2 file3 test.txt | |
672 ~/learn $ cat test.txt | |
673 this is a test | |
674 ~/learn $ echo 'this is another test' >test.txt | |
675 ~/learn $ cat test.txt | |
676 this is another test | |
677 ~/learn $ echo 'another line' >>test.txt | |
678 ~/learn $ cat test.txt | |
679 this is another test | |
680 another line | |
681 ~/learn $ cat <test.txt | |
682 this is another test | |
683 another line | |
684 ~/learn $ cat <<End >test.txt | |
685 > I am typing this | |
686 > and this | |
687 > End | |
688 ~/learn $ cat test.txt | |
689 I am typing this | |
690 and this | |
691 ~/learn $ (echo one; echo two) >test.txt | |
692 ~/learn $ cat test.txt | |
693 one | |
694 two | |
695 </code> | |
696 | |
697 <p>All programs have standard input, standard output, and standard error. Programs write normal output to standard output and error messages to standard error. By default, standard input comes from the terminal, and standard output and standard error go to the terminal, but this can be changed. <code>>file</code> sends standard output to <code>file</code>. <code>>>file</code> appends standard output to <code>file</code>. <code><file</code> reads standard input from <code>file</code>. <code><<whatever</code> reads standard input from the text that follows until a line with just <code>whatever</code>. Commands can be combined between <code>(</code> and <code>)</code>. Be sure to <code>man cat</code> to understand how <code>cat</code> works.</p> | |
698 | |
699 <code block> | |
700 ~/learn $ ls >ls.txt | |
701 ~/learn $ cat ls.txt | |
702 dir1 | |
703 file1 | |
704 file2 | |
705 file3 | |
706 ls.txt | |
707 test.txt | |
708 ~/learn $ ls -d f* q* >ls.txt | |
709 ls: q*: No such file or directory | |
710 ~/learn $ cat ls.txt | |
711 file1 | |
712 file2 | |
713 file3 | |
714 ~/learn $ ls -d f* q* 2>ls.txt | |
715 file1 file2 file3 | |
716 ~/learn $ cat ls.txt | |
717 ls: q*: No such file or directory | |
718 ~/learn $ ls -d f* q* | tee ls.txt | |
719 ls: q*: No such file or directory | |
720 file1 | |
721 file2 | |
722 file3 | |
723 ~/learn $ cat ls.txt | |
724 file1 | |
725 file2 | |
726 file3 | |
727 ~/learn $ ls -d f* q* 2>&1 | tee ls.txt | |
728 ls: q*: No such file or directory | |
729 file1 | |
730 file2 | |
731 file3 | |
732 ~/learn $ cat ls.txt | |
733 ls: q*: No such file or directory | |
734 file1 | |
735 file2 | |
736 file3 | |
737 </code> | |
738 | |
739 <p><code>2>file</code> sends standard error to <code>file</code>. <code>|</code> sends standard output of the previous command to standard input of the following command. <code>2>&1</code> sends standard error to standard output. <code>tee file</code> reads standard input and then writes it to both standard output and to <code>file</code>.</p> | |
740 | |
741 <code block> | |
742 ~/learn $ find . -type f | wc -l | |
743 6 | |
744 </code> | |
745 | |
746 <p>There are 6 files in <code>learn</code>. Use <code>man</code> to figure out how this works.</p> | |
747 ` , | |
748 }, | |
749 ctrl: { | |
750 title: 'Control Keys', | |
751 content: `\ | |
752 <code block> | |
753 ~/learn $ sleep 3 | |
754 ~/learn $ sleep 30 | |
755 ^C | |
756 ~/learn $ | |
757 </code> | |
758 | |
759 <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> | |
760 | |
761 <code block> | |
762 ~/learn $ wc | |
763 I am typing this | |
764 and this | |
765 now I will end my input with control+d | |
766 3 14 65 | |
767 ~/learn $ wc | |
768 this time I will use control+c to break out | |
769 ^C | |
770 ~/learn $ | |
771 </code> | |
772 | |
773 <p>Control+d means end of input.</p> | |
774 ` , | |
775 }, | |
776 subst: { | |
777 title: 'Command Substitution', | |
778 content: `\ | |
779 <code block> | |
780 ~/learn $ echo I am in $(pwd) | |
781 I am in /Users/fschmidt/learn | |
782 ~/learn $ echo this directory contains: $(ls) | |
783 this directory contains: dir1 file1 file2 file3 ls.txt test.txt | |
784 ~/learn $ echo this directory contains $(ls | wc -l) files | |
785 this directory contains 6 files | |
786 </code> | |
787 | |
788 <p><code>cmd $(commands)</code> will use the output of <code>commands</code> as argument text for <code>cmd</code>.</p> | |
789 | |
790 <code block> | |
791 ~/learn $ cat $(find . -type f) | wc -c | |
792 86 | |
793 </code> | |
794 | |
795 <p>The files in <code>learn</code> contain a total of 86 bytes. Use <code>man</code> to figure out how this works.</p> | |
796 ` , | |
797 }, | |
798 ampersand: { | |
799 title: 'Ampersand', | |
800 content: `\ | |
801 <code block> | |
802 ~/learn $ (sleep 5; echo done) & | |
803 [1] 10080 | |
804 ~/learn $ echo waiting | |
805 waiting | |
806 ~/learn $ done | |
807 | |
808 [1]+ Done ( sleep 5; echo done ) | |
809 ~/learn $ | |
810 </code> | |
811 | |
812 <p>Normally Bash waits for a command to complete before showing the command prompt and allowing input. But ending a command line with <code>&</code> tells bash not to wait, but instead to run the command in a separate process. Above in <code>~/learn $ echo waiting</code>, I typed in <code>echo waiting</code>. But in <code>~/learn $ done</code>, I did not type <code>done</code>. Instead this was produced by <code>echo done</code> after 5 seconds. <code>[1] 10080</code> tells me that a process was started and <code>[1]+ Done ( sleep 5; echo done )</code> tells me that the process finished.</p> | |
813 | |
814 <p>This is useful where you do not want to wait for a command to finish. Consider this on Windows:</p> | |
815 | |
816 <code block> | |
817 ~ $ notepad | |
818 </code> | |
819 | |
820 <p>Here you will not get a command prompt again until you quit Notepad because Bash is waiting for this command to finish. So instead do: | |
821 | |
822 <code block> | |
823 ~ $ notepad & | |
824 [1] 2010 | |
825 ~ $ | |
826 </code> | |
827 | |
828 <p>Now Notepad will run and you can continue using Bash.</p> | |
829 ` , | |
830 }, | |
831 scripts: { | |
832 title: 'Shell Scripts', | |
833 content: `\ | |
834 <p>Make a file called <code>test.sh</code> containing the following:</p> | |
835 | |
836 <code block> | |
837 echo this is a shell script | |
838 </code> | |
839 | |
840 <p>Now from Bash:</p> | |
841 | |
842 <code block> | |
843 ~/learn $ cat test.sh | |
844 echo this is a shell script | |
845 ~/learn $ ./test.sh | |
846 -bash: ./test.sh: Permission denied | |
847 ~/learn $ ls -F test.sh | |
848 test.sh | |
849 ~/learn $ chmod +x test.sh | |
850 ~/learn $ ls -F test.sh | |
851 test.sh* | |
852 ~/learn $ ./test.sh | |
853 this is a shell script | |
854 ~/learn $ | |
855 </code> | |
856 | |
857 <p><code>chmod +x file</code> makes <code>file</code> into an executable that can be run. Now I will edit <code>test.sh</code></p> | |
858 | |
859 <code block> | |
860 ~/learn $ # edit test.sh | |
861 ~/learn $ cat test.sh | |
862 nonsense | |
863 echo this is a shell script | |
864 ~/learn $ ./test.sh | |
865 ./test.sh: line 1: nonsense: command not found | |
866 this is a shell script | |
867 ~/learn $ # edit test.sh | |
868 ~/learn $ cat test.sh | |
869 set -e | |
870 nonsense | |
871 echo this is a shell script | |
872 ~/learn $ ./test.sh | |
873 ./test.sh: line 2: nonsense: command not found | |
874 ~/learn $ | |
875 </code> | |
876 | |
877 <p>By default, scripts continue running after an error. In longer scripts, we want the script to exit after an error. <code>set -e</code> does this, see <code>help set</code>.</p> | |
878 | |
879 <code block> | |
880 ~/learn $ X=some | |
881 ~/learn $ echo $X | |
882 some | |
883 ~/learn $ echo $Xthing | |
884 | |
885 ~/learn $ echo \${X}thing | |
886 something | |
887 ~/learn $ # edit test.sh | |
888 ~/learn $ cat test.sh | |
889 echo "\\$* = $*" | |
890 echo "\\$# = $#" | |
891 echo "\\$0 = $0" | |
892 echo "\\$1 = $1" | |
893 echo "\\$2 = $2" | |
894 echo "\\$3 = $3" | |
895 echo "\\$4 = $4" | |
896 echo "\\$14 = $14" | |
897 echo "\\\${14} = \$\{14}" | |
898 echo "\\$@ = $@" | |
899 ./count.sh "$*" | |
900 ./count.sh "$@" | |
901 ~/learn $ ./test.sh a b "c d" | |
902 $* = a b c d | |
903 $# = 3 | |
904 $0 = ./test.sh | |
905 $1 = a | |
906 $2 = b | |
907 $3 = c d | |
908 $4 = | |
909 $14 = a4 | |
910 \${14} = | |
911 $@ = a b c d | |
912 1 | |
913 3 | |
914 ~/learn $ cat count.sh | |
915 echo $# | |
916 ~/learn $ | |
917 </code> | |
918 | |
919 <p>Bash scripts have special defined variables. The difference between <code>$*</code> and <code>$@</code> is subtle, and you will usually just use <code>$*</code>. <code>$*</code> returns all arguments as one string while <code>$@</code> returns the arguments separately, but this distinction rarely makes any difference.</p> | |
920 ` , | |
921 }, | |
922 vars_and_scripts: { | |
923 title: 'Variables and Scripts', | |
924 content: `\ | |
925 <code block> | |
926 ~/learn $ X=value | |
927 ~/learn $ echo $X | |
928 value | |
929 ~/learn $ # edit test.sh | |
930 ~/learn $ cat test.sh | |
931 echo "\\$X = $X" | |
932 ~/learn $ ./test.sh | |
933 $X = | |
934 ~/learn $ export X | |
935 ~/learn $ ./test.sh | |
936 $X = value | |
937 </code> | |
938 | |
939 <p>Variables are defined in the current shell. Shell scripts are run in their own shell. So by default, they don't see variables defined in the terminal/parent shell. <code>export var</code> makes <code>var</code> available in descendant processes, meaning available in shell scripts. It is a good idea to do <code>export PATH</code> in <code>.bash_profile</code> so that your PATH is available to your scripts.</p> | |
940 | |
941 <code block> | |
942 ~/learn $ X=terminal | |
943 ~/learn $ echo $X | |
944 terminal | |
945 ~/learn $ # edit test.sh | |
946 ~/learn $ cat test.sh | |
947 X=script | |
948 export X | |
949 ~/learn $ ./test.sh | |
950 ~/learn $ echo $X | |
951 terminal | |
952 ~/learn $ . test.sh | |
953 ~/learn $ echo $X | |
954 script | |
955 </code> | |
956 | |
957 <p>You can export a variable from parent to children but not from children to parent. <code>. script</code> includes the text in the file <code>script</code> in the current shell. In this case, it is not run in a separate shell. This is the only way to have a script set variables in your terminal shell.</p> | |
958 | |
959 <code block> | |
960 ~/learn $ pwd | |
961 /Users/fschmidt/learn | |
962 ~/learn $ # edit test.sh | |
963 ~/learn $ cat test.sh | |
964 cd ~ | |
965 ~/learn $ ./test.sh | |
966 ~/learn $ pwd | |
967 /Users/fschmidt/learn | |
968 ~/learn $ . test.sh | |
969 ~ $ pwd | |
970 /Users/fschmidt | |
971 ~ $ cd learn | |
972 ~/learn $ | |
973 </code> | |
974 | |
975 <p>This illustrates the difference between <code>./script</code> and <code>. script</code>.</p> | |
976 ` , | |
977 }, | |
978 your_scripts: { | |
979 title: 'Your Scripts', | |
980 content: `\ | |
981 <code block> | |
982 ~/learn $ echo $PATH | |
983 /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 | |
984 ~/learn $ echo ~/Dropbox/bin | |
985 /Users/fschmidt/Dropbox/bin | |
986 ~/learn $ ls -F ~/Dropbox/bin/e | |
987 /Users/fschmidt/Dropbox/bin/e* | |
988 ~/learn $ cat ~/Dropbox/bin/e | |
989 open -a 'Sublime Text' $* | |
990 ~/learn $ e test.sh | |
991 ~/learn $ | |
992 </code> | |
993 | |
994 <p>When you write useful scripts, put them in a directory and add that directory to your PATH. I use <code>~/Dropbox/bin</code> and I have a script named <code>e</code> in that directory for editing files. So <code>e test.sh</code> lets me edit <code>test.sh</code> from the command line.</p> | |
995 | |
996 <p>Note that Bash will only look in your PATH for scripts unless you give an explicit path to the script.</p> | |
997 | |
998 <code block> | |
999 ~/learn $ # edit test.sh | |
1000 ~/learn $ cat test.sh | |
1001 echo this is a shell script | |
1002 ~/learn $ test.sh | |
1003 -bash: test.sh: command not found | |
1004 ~/learn $ ./test.sh | |
1005 this is a shell script | |
1006 ~/learn $ | |
1007 </code> | |
1008 | |
1009 <p>Calling <code>test.sh</code> by itself fails because it isn't in the PATH. But <code>./test.sh</code> works because it is an explicit path.</p> | |
1010 ` , | |
1011 }, | |
1012 advanced: { | |
1013 title: 'Advanced Scripting', | |
1014 content: `\ | |
1015 <p>Here is a more advanced script called <code>undocx.sh</code> that unpacks a Word DOCX file.</p> | |
1016 | |
1017 <code block> | |
1018 #!/bin/bash | |
1019 | |
1020 set -e | |
1021 | |
1022 if [ $# -ne 1 ]; then | |
1023 echo "usage: $0 filename" | |
1024 exit 1 | |
1025 fi | |
1026 | |
1027 FILE="$1" | |
1028 NEWDIR=$(basename $FILE .docx) | |
1029 | |
1030 mkdir $NEWDIR | |
1031 unzip $FILE -d $NEWDIR | |
1032 | |
1033 export XMLLINT_INDENT=$'\\t' | |
1034 for file in $(find $NEWDIR -name "*.xml" -o -name "*.rels"); do | |
1035 mv "$file" temp.xml | |
1036 xmllint --format temp.xml >"$file" | |
1037 done | |
1038 rm temp.xml | |
1039 </code> | |
1040 | |
1041 <p>Bash is a full programming language containing all the usual features. Some commands in my script are well explained by <code>man</code>, but some are not. In particular, the documentation for <code>if</code> and <code>for</code> are poor. In cases like this, I suggest asking ChatGPT like this: | |
1042 | |
1043 <code block> | |
1044 Please explain the Bash "if" statement. | |
1045 </code> | |
1046 | |
1047 <code block> | |
1048 Please explain the Bash "for" statement. | |
1049 </code> | |
1050 | |
1051 <p>ChatGPT knows Bash well. I trust ChatGPT to explain details but not to explain core concepts. You can also try Google, but ChatGPT is better than modern programmers.</p> | |
1052 ` , | |
1053 }, | |
1054 conclusion: { | |
1055 title: 'Conclusion', | |
1056 content: `\ | |
1057 <p>At least 90% of your usage of Bash will be simple commands that you enter in the terminal. Try to use Bash as much as possible instead of using the GUI so that you get practice using it. Unless you become system administrator, you won't use advanced scripting much. But with a solid understanding of the core basics, you should be able to figure out how to read or write advanced scripts when needed.</p> | |
1058 ` , | |
1059 }, | |
1060 }; | |
1061 </script> | |
1062 </head> | |
1063 <body> | |
1064 <script> header() </script> | |
1065 <div content> | |
1066 <h1><a href="bash.html">Bash Tutorial</a></h1> | |
1067 <hr> | |
1068 <h2>Contents</h2> | |
1069 <div toc> | |
1070 <script> showToc(content) </script> | |
1071 </div> | |
1072 <hr> | |
1073 <script> showContent(content,2) </script> | |
1074 </div> | |
1075 </body> | |
1076 </html> |