Mercurial Hosting > arkian
changeset 26:fb87f762847e default tip
start computer_literacy
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 09 Oct 2025 16:56:36 -0600 |
parents | 4e019ee9c44d |
children | |
files | src/computer_literacy/bash.html src/computer_literacy/computer_literacy.html src/site.css |
diffstat | 3 files changed, 1278 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/computer_literacy/bash.html Thu Oct 09 16:56:36 2025 -0600 @@ -0,0 +1,1078 @@ +<!doctype html> +<html lang="en"> + <head> + <script src="/site.js"></script> + <script> head() </script> + <title>Arkian - Bash</title> + <script> +'use strict'; + +let content = { + intro: { + title: 'Introduction', + content: `\ +<p>I really don't want to write this tutorial, but all the existing Bash 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 junk. And they don't explain core concepts. So I have no choice but to write this for my <a href="learn.html#bash">Learn Reactionary Programming</a> Bash lesson.</p> + +<p><a href="bash.html">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> +` , + }, + access: { + title: 'Running Bash', + content: `\ +<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> +` , + }, + start: { + title: 'Getting Started', + content: `\ +<p>When I start Bash on my Mac I see:</p> + +<code block> +Last login: Thu Jan 4 23:25:35 on ttys004 + +The default interactive shell is now zsh. +To update your account to use zsh, please run 'chsh -s /bin/zsh'. +For more details, please visit https://support.apple.com/kb/HT208050. +~ $ + +</code> + +<p>On Windows - MSYS2 I just see:</p> + +<code block> +~ $ + +</code> + +<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> + +<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> + +<code block> +~ $ qqq +-bash: qqq: command not found +~ $ +</code> + +<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> + +<code block> +~ $ echo hi +hi +~ $ echo how are you +how are you +~ $ echo bye +bye +</code> + +<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> + +<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> + +<code block> +~ $ echo how are you +how are you +</code> + +<p><code>echo</code> just returns the arguments separated by one space.</p> + +<code block> +~ $ echo one; echo two +one +two +</code> + +<p>You can put multiple commands on one line separated by a <code>;</code>.</p> +` , + }, + man: { + title: 'The "man" Command', + content: `\ +<p>Enter:</p> +<code block> +~ $ man echo +</code> + +<p>You should get something like:</p> + +<code block> + +ECHO(1) BSD General Commands Manual ECHO(1) + +NAME + echo -- write arguments to the standard output + +SYNOPSIS + echo [-n] [string ...] + +DESCRIPTION + The echo utility writes any specified operands, separated by single blank + (' ') characters and followed by a newline ('\\n') character, to the stan- + dard output. + + The following option is available: + + -n Do not print the trailing newline character. This may also be + achieved by appending '\\c' to the end of the string, as is done by + iBCS2 compatible systems. Note that this option as well as the + effect of '\\c' are implementation-defined in IEEE Std 1003.1-2001 + (''POSIX.1'') as amended by Cor. 1-2002. Applications aiming for + maximum portability are strongly encouraged to use printf(1) to + suppress the newline character. +: +</code> + +<p>But if you are on Windows, you may not have <code>man</code> installed. In that case, do:</p> + +<code block> +~ $ pacman -S man-db +</code> + +<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> + +<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> + +<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> +` , + }, + dirs: { + title: 'Directories', + content: `\ +<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> + +<p>On Mac:</p> + +<code block> +~ $ pwd +/Users/fschmidt +~ $ open . +~ $ +</code> + +<p>On Windows:</p> + +<code block> +~ $ pwd +/home/fschmidt +~ $ explorer . +~ $ +</code> + +<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> + +<p>Continuing on my Mac:</p> + +<code block> +~ $ mkdir learn +~ $ cd learn +~/learn $ pwd +/Users/fschmidt/learn +</code> + +<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> + +<code block> +~/learn $ pwd +/Users/fschmidt/learn +~/learn $ ls +~/learn $ touch file1 +~/learn $ ls +file1 +~/learn $ touch file2 +~/learn $ touch file3 +~/learn $ ls +file1 file2 file3 +~/learn $ mkdir dir1 +~/learn $ ls +dir1 file1 file2 file3 +~/learn $ ls -F +dir1/ file1 file2 file3 +~/learn $ ls -a +. .. dir1 file1 file2 file3 +~/learn $ ls -a -F +./ ../ dir1/ file1 file2 file3 +~/learn $ ls -aF +./ ../ dir1/ file1 file2 file3 +</code> + +<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> + +<code block> +~/learn $ ls file1 +file1 +~/learn $ ls qqq +ls: qqq: No such file or directory +~/learn $ ls file1 qqq file2 +ls: qqq: No such file or directory +file1 file2 +~/learn $ ls dir1 +~/learn $ touch dir1/d1file +~/learn $ ls dir1 +d1file +~/learn $ ls -d dir1 +dir1 +~/learn $ ls file1 file2 dir1 +file1 file2 + +dir1: +d1file +~/learn $ ls -d file1 file2 dir1 +dir1 file1 file2 +~/learn $ ls -dF file1 file2 dir1 +dir1/ file1 file2 +</code> + +<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> + +<code block> +~/learn $ ls +dir1 file1 file2 file3 +~/learn $ ls . +dir1 file1 file2 file3 +~/learn $ ls -d . +. +~/learn $ ls -dF . +./ +~/learn $ ls ./file1 +./file1 +~/learn $ ls dir1 +d1file +~/learn $ ls ./dir1 +d1file +~/learn $ pwd +/Users/fschmidt/learn +~/learn $ cd . +~/learn $ pwd +/Users/fschmidt/learn +</code> + +<p><code>.</code> is the current directory.</p> + +<code block> +~/learn $ pwd +/Users/fschmidt/learn +~/learn $ cd dir1 +~/learn/dir1 $ pwd +/Users/fschmidt/learn/dir1 +~/learn/dir1 $ ls . +d1file +~/learn/dir1 $ ls .. +dir1 file1 file2 file3 +~/learn/dir1 $ cd .. +~/learn $ pwd +/Users/fschmidt/learn +~/learn $ cd dir1 +~/learn/dir1 $ pwd +/Users/fschmidt/learn/dir1 +~/learn/dir1 $ cd ../.. +~ $ pwd +/Users/fschmidt +~ $ cd learn +~/learn $ pwd +/Users/fschmidt/learn +</code> + +<p><code>..</code> is the parent directory.</p> + +<code block> +~/learn $ echo * +dir1 file1 file2 file3 +~/learn $ echo d* +dir1 +~/learn $ echo f* +file1 file2 file3 +~/learn $ echo *1 +dir1 file1 +~/learn $ echo dir1/* +dir1/d1file +~/learn $ echo */* +dir1/d1file +~/learn $ echo qqq* +qqq* +</code> + +<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> + +<code block> +~/learn $ ls * +file1 file2 file3 + +dir1: +d1file +~/learn $ ls -dF * +dir1/ file1 file2 file3 +~/learn $ ls -dF d* +dir1/ +~/learn $ ls -dF f* +file1 file2 file3 +~/learn $ ls -dF *1 +dir1/ file1 +~/learn $ ls dir1/* +dir1/d1file +~/learn $ ls */* +dir1/d1file +~/learn $ ls -dF qqq* +ls: qqq*: No such file or directory +</code> + +<p>Should be self-explanatory.</p> + +<code block> +~/learn $ pwd +/Users/fschmidt/learn +~/learn $ cd ~ +~ $ pwd +/Users/fschmidt +~ $ cd learn/dir1 +~/learn/dir1 $ pwd +/Users/fschmidt/learn/dir1 +~/learn/dir1 $ cd +~ $ pwd +/Users/fschmidt +~ $ cd ~/learn +~/learn $ pwd +/Users/fschmidt/learn +~/learn $ echo ~ +/Users/fschmidt +~/learn $ echo . +. +~/learn $ echo .. +.. +</code> + +<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> + +<code block> +~/learn $ ls -ltF +total 0 +drwxr-xr-x 3 fschmidt staff 96 Jan 5 02:33 dir1/ +-rw-r--r-- 1 fschmidt staff 0 Jan 5 02:21 file3 +-rw-r--r-- 1 fschmidt staff 0 Jan 5 02:21 file2 +-rw-r--r-- 1 fschmidt staff 0 Jan 5 02:21 file1 +</code> + +<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> + +<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> + +<code block> +~/learn $ echo file +file1 file2 file3 +~/learn $ echo file +</code> + +<p>In general, you can press tab anytime while entering a file name and see what happens. Autocompletion saves a lot of typing.</p> +` , + }, + files: { + title: 'Working with Files', + content: `\ +<code block> +~/learn $ ls -F +dir1/ file1 file2 file3 +~/learn $ cp file1 copied +~/learn $ ls -F +copied dir1/ file1 file2 file3 +~/learn $ mv copied moved +~/learn $ ls -F +dir1/ file1 file2 file3 moved +~/learn $ rm moved +~/learn $ ls -F +dir1/ file1 file2 file3 +</code> + +<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> + +<code block> +~/learn $ ls -F +dir1/ file1 file2 file3 +~/learn $ mkdir dir2 +~/learn $ touch dir2/d2file +~/learn $ ls -F +dir1/ dir2/ file1 file2 file3 +~/learn $ ls dir2 +d2file +~/learn $ rm dir2 +rm: dir2: is a directory +~/learn $ rm -d dir2 +rm: dir2: Directory not empty +~/learn $ rm dir2/d2file +~/learn $ rm -d dir2 +~/learn $ ls -F +dir1/ file1 file2 file3 +</code> + +<code block> +~/learn $ ls -F +dir1/ file1 file2 file3 +~/learn $ mkdir dir2 +~/learn $ touch dir2/d2file +~/learn $ ls -F +dir1/ dir2/ file1 file2 file3 +~/learn $ rm -r dir2 +~/learn $ ls -F +dir1/ file1 file2 file3 +</code> + +<code block> +~/learn $ ls -F +dir1/ file1 file2 file3 +~/learn $ cp dir1 dir2 +cp: dir1 is a directory (not copied). +~/learn $ cp -r dir1 dir2 +~/learn $ ls -F +dir1/ dir2/ file1 file2 file3 +~/learn $ ls dir2 +d1file +~/learn $ cp f* dir2 +~/learn $ ls dir2 +d1file file1 file2 file3 +~/learn $ rm -r dir2 +~/learn $ ls -F +dir1/ file1 file2 file3 +</code> + +<code block> +~/learn $ ls -F +dir1/ file1 file2 file3 +~/learn $ mkdir dir2 +~/learn $ cp -r dir1 dir2 +~/learn $ ls -F dir2 +dir1/ +~/learn $ ls -F dir2/dir1 +d1file +~/learn $ rm -r dir2 +~/learn $ ls -F +dir1/ file1 file2 file3 +</code> + +<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> +` , + }, + quote: { + title: 'Quoting', + content: `\ +<code block> +~/learn $ echo a b +a b +~/learn $ echo "a b" +a b +~/learn $ echo 'a b' +a b +~/learn $ echo "a b" c +a b c +</code> + +<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> + +<code block> +~/learn $ echo a\\ \\ \\ b +a b +</code> + +<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> + +<code block> +~/learn $ echo file* +file1 file2 file3 +~/learn $ echo "file*" +file* +~/learn $ echo 'file*' +file* +</code> + +<p>Quotes prevent wildcard expansion.</p> +` , + }, + vars: { + title: 'Variables', + content: `\ +<code block> +~/learn $ echo $X + +~/learn $ X="some text" +~/learn $ echo $X +some text +~/learn $ echo "X is: $X" +X is: some text +~/learn $ echo 'X is: $X' +X is: $X +~/learn $ X="$X and more" +~/learn $ echo $X +some text and more +</code> + +<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> + +<p>There are special variables called environment variables that are used by Bash.</p> + +<code block> +~/learn $ echo $PATH +/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 +~/learn $ which ls +/bin/ls +~/learn $ cd /bin +/bin $ pwd +/bin +/bin $ ls +[ dd launchctl pwd test +bash df link rm unlink +cat echo ln rmdir wait4path +chmod ed ls sh zsh +cp expr mkdir sleep +csh hostname mv stty +dash kill pax sync +date ksh ps tcsh +/bin $ ls -F +[* dd* launchctl* pwd* test* +bash* df* link* rm* unlink* +cat* echo* ln* rmdir* wait4path* +chmod* ed* ls* sh* zsh* +cp* expr* mkdir* sleep* +csh* hostname* mv* stty* +dash* kill* pax* sync* +date* ksh* ps* tcsh* +/bin $ cd ~/learn +~/learn $ +</code> + +<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> + +<code block> +~/learn $ subl file1 +-bash: subl: command not found +~/learn $ "/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl" file1 +~/learn $ PATH="$PATH:/Applications/Sublime Text.app/Contents/SharedSupport/bin" +~/learn $ echo $PATH +/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 +~/learn $ subl file1 +~/learn $ +</code> + +<p>Here I edit the file <code>file1</code> with <a href="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> + +<p>I have Microsoft Word on Windows. From the Windows Command Prompt (not Bash):</p> + +<code block> +C:\\Users\\fschmidt>winword + +C:\\Users\\fschmidt>where winword +C:\\Program Files\\Microsoft Office\\root\\Office16\\WINWORD.EXE +</code> + +<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> + +<code block> +~ $ winword +bash: winword: command not found +~ $ echo $PATH +/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 +~ $ PATH="$PATH:/c/Program Files/Microsoft Office/root/Office16" +~ $ echo $PATH +/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 +~ $ winword +~ $ +</code> + +<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> + +<code block> +~/learn $ open -a 'Sublime Text' file1 +</code> + +<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> + +<code block> +Franklins-MacBook-Pro:learn fschmidt$ echo $PS1 +\\h:\\W \\u\\$ +Franklins-MacBook-Pro:learn fschmidt$ PS1="\\w $ " +~/learn $ echo $PS1 +\\w $ +~/learn $ +</code> + +<p>Google "bash PS1" for more info.</p> +` , + }, + bash_profile: { + title: '.bash_profile', + content: `\ +<code block> +~/learn $ cd +~ $ ls .bash_profile +.bash_profile +</code> + +<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> + +<code block> +~ $ # comment line, does nothing +~ $ echo whatever # end of line comment +whatever +~ $ +</code> + +<p>To edit <code>.bash_profile</code> on a Mac, you can do:</p> + +<code block> +~ $ open -a 'Sublime Text' .bash_profile +</code> + +<p>To edit <code>.bash_profile</code> on Windows, you can do:</p> + +<code block> +~ $ notepad .bash_profile +</code> + +<p>Now try adding this line to <code>.bash_profile</code>:</p> + +<code block> +echo hello there +</code> + +<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> + +<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> +` , + }, + find: { + title: 'The "find" Command', + content: `\ +<code block> +~/learn $ find . +. +./file3 +./file2 +./file1 +./dir1 +./dir1/d1file +~/learn $ find . -name 'file*' +./file3 +./file2 +./file1 +~/learn $ find . -name '*file' +./dir1/d1file +~/learn $ find . -name 'd*' +./dir1 +./dir1/d1file +~/learn $ find . -name '*1' -or -name '*2' +./file2 +./file1 +./dir1 +</code> + +<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> +` , + }, + io: { + title: 'Input and Output', + content: `\ +<code block> +~/learn $ echo 'this is a test' >test.txt +~/learn $ ls -F +dir1/ file1 file2 file3 test.txt +~/learn $ cat test.txt +this is a test +~/learn $ echo 'this is another test' >test.txt +~/learn $ cat test.txt +this is another test +~/learn $ echo 'another line' >>test.txt +~/learn $ cat test.txt +this is another test +another line +~/learn $ cat <test.txt +this is another test +another line +~/learn $ cat <<End >test.txt +> I am typing this +> and this +> End +~/learn $ cat test.txt +I am typing this +and this +~/learn $ (echo one; echo two) >test.txt +~/learn $ cat test.txt +one +two +</code> + +<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> + +<code block> +~/learn $ ls >ls.txt +~/learn $ cat ls.txt +dir1 +file1 +file2 +file3 +ls.txt +test.txt +~/learn $ ls -d f* q* >ls.txt +ls: q*: No such file or directory +~/learn $ cat ls.txt +file1 +file2 +file3 +~/learn $ ls -d f* q* 2>ls.txt +file1 file2 file3 +~/learn $ cat ls.txt +ls: q*: No such file or directory +~/learn $ ls -d f* q* | tee ls.txt +ls: q*: No such file or directory +file1 +file2 +file3 +~/learn $ cat ls.txt +file1 +file2 +file3 +~/learn $ ls -d f* q* 2>&1 | tee ls.txt +ls: q*: No such file or directory +file1 +file2 +file3 +~/learn $ cat ls.txt +ls: q*: No such file or directory +file1 +file2 +file3 +</code> + +<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> + +<code block> +~/learn $ find . -type f | wc -l + 6 +</code> + +<p>There are 6 files in <code>learn</code>. Use <code>man</code> to figure out how this works.</p> +` , + }, + ctrl: { + title: 'Control Keys', + content: `\ +<code block> +~/learn $ sleep 3 +~/learn $ sleep 30 +^C +~/learn $ +</code> + +<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> + +<code block> +~/learn $ wc +I am typing this +and this +now I will end my input with control+d + 3 14 65 +~/learn $ wc +this time I will use control+c to break out +^C +~/learn $ +</code> + +<p>Control+d means end of input.</p> +` , + }, + subst: { + title: 'Command Substitution', + content: `\ +<code block> +~/learn $ echo I am in $(pwd) +I am in /Users/fschmidt/learn +~/learn $ echo this directory contains: $(ls) +this directory contains: dir1 file1 file2 file3 ls.txt test.txt +~/learn $ echo this directory contains $(ls | wc -l) files +this directory contains 6 files +</code> + +<p><code>cmd $(commands)</code> will use the output of <code>commands</code> as argument text for <code>cmd</code>.</p> + +<code block> +~/learn $ cat $(find . -type f) | wc -c + 86 +</code> + +<p>The files in <code>learn</code> contain a total of 86 bytes. Use <code>man</code> to figure out how this works.</p> +` , + }, + ampersand: { + title: 'Ampersand', + content: `\ +<code block> +~/learn $ (sleep 5; echo done) & +[1] 10080 +~/learn $ echo waiting +waiting +~/learn $ done + +[1]+ Done ( sleep 5; echo done ) +~/learn $ +</code> + +<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> + +<p>This is useful where you do not want to wait for a command to finish. Consider this on Windows:</p> + +<code block> +~ $ notepad +</code> + +<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: + +<code block> +~ $ notepad & +[1] 2010 +~ $ +</code> + +<p>Now Notepad will run and you can continue using Bash.</p> +` , + }, + scripts: { + title: 'Shell Scripts', + content: `\ +<p>Make a file called <code>test.sh</code> containing the following:</p> + +<code block> +echo this is a shell script +</code> + +<p>Now from Bash:</p> + +<code block> +~/learn $ cat test.sh +echo this is a shell script +~/learn $ ./test.sh +-bash: ./test.sh: Permission denied +~/learn $ ls -F test.sh +test.sh +~/learn $ chmod +x test.sh +~/learn $ ls -F test.sh +test.sh* +~/learn $ ./test.sh +this is a shell script +~/learn $ +</code> + +<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> + +<code block> +~/learn $ # edit test.sh +~/learn $ cat test.sh +nonsense +echo this is a shell script +~/learn $ ./test.sh +./test.sh: line 1: nonsense: command not found +this is a shell script +~/learn $ # edit test.sh +~/learn $ cat test.sh +set -e +nonsense +echo this is a shell script +~/learn $ ./test.sh +./test.sh: line 2: nonsense: command not found +~/learn $ +</code> + +<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> + +<code block> +~/learn $ X=some +~/learn $ echo $X +some +~/learn $ echo $Xthing + +~/learn $ echo \${X}thing +something +~/learn $ # edit test.sh +~/learn $ cat test.sh +echo "\\$* = $*" +echo "\\$# = $#" +echo "\\$0 = $0" +echo "\\$1 = $1" +echo "\\$2 = $2" +echo "\\$3 = $3" +echo "\\$4 = $4" +echo "\\$14 = $14" +echo "\\\${14} = \$\{14}" +echo "\\$@ = $@" +./count.sh "$*" +./count.sh "$@" +~/learn $ ./test.sh a b "c d" +$* = a b c d +$# = 3 +$0 = ./test.sh +$1 = a +$2 = b +$3 = c d +$4 = +$14 = a4 +\${14} = +$@ = a b c d +1 +3 +~/learn $ cat count.sh +echo $# +~/learn $ +</code> + +<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> +` , + }, + vars_and_scripts: { + title: 'Variables and Scripts', + content: `\ +<code block> +~/learn $ X=value +~/learn $ echo $X +value +~/learn $ # edit test.sh +~/learn $ cat test.sh +echo "\\$X = $X" +~/learn $ ./test.sh +$X = +~/learn $ export X +~/learn $ ./test.sh +$X = value +</code> + +<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> + +<code block> +~/learn $ X=terminal +~/learn $ echo $X +terminal +~/learn $ # edit test.sh +~/learn $ cat test.sh +X=script +export X +~/learn $ ./test.sh +~/learn $ echo $X +terminal +~/learn $ . test.sh +~/learn $ echo $X +script +</code> + +<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> + +<code block> +~/learn $ pwd +/Users/fschmidt/learn +~/learn $ # edit test.sh +~/learn $ cat test.sh +cd ~ +~/learn $ ./test.sh +~/learn $ pwd +/Users/fschmidt/learn +~/learn $ . test.sh +~ $ pwd +/Users/fschmidt +~ $ cd learn +~/learn $ +</code> + +<p>This illustrates the difference between <code>./script</code> and <code>. script</code>.</p> +` , + }, + your_scripts: { + title: 'Your Scripts', + content: `\ +<code block> +~/learn $ echo $PATH +/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 +~/learn $ echo ~/Dropbox/bin +/Users/fschmidt/Dropbox/bin +~/learn $ ls -F ~/Dropbox/bin/e +/Users/fschmidt/Dropbox/bin/e* +~/learn $ cat ~/Dropbox/bin/e +open -a 'Sublime Text' $* +~/learn $ e test.sh +~/learn $ +</code> + +<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> + +<p>Note that Bash will only look in your PATH for scripts unless you give an explicit path to the script.</p> + +<code block> +~/learn $ # edit test.sh +~/learn $ cat test.sh +echo this is a shell script +~/learn $ test.sh +-bash: test.sh: command not found +~/learn $ ./test.sh +this is a shell script +~/learn $ +</code> + +<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> +` , + }, + advanced: { + title: 'Advanced Scripting', + content: `\ +<p>Here is a more advanced script called <code>undocx.sh</code> that unpacks a Word DOCX file.</p> + +<code block> +#!/bin/bash + +set -e + +if [ $# -ne 1 ]; then + echo "usage: $0 filename" + exit 1 +fi + +FILE="$1" +NEWDIR=$(basename $FILE .docx) + +mkdir $NEWDIR +unzip $FILE -d $NEWDIR + +export XMLLINT_INDENT=$'\\t' +for file in $(find $NEWDIR -name "*.xml" -o -name "*.rels"); do + mv "$file" temp.xml + xmllint --format temp.xml >"$file" +done +rm temp.xml +</code> + +<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: + +<code block> +Please explain the Bash "if" statement. +</code> + +<code block> +Please explain the Bash "for" statement. +</code> + +<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> +` , + }, + conclusion: { + title: 'Conclusion', + content: `\ +<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> +` , + }, +}; + </script> + </head> + <body> + <script> header() </script> + <div content> + <h1><a href="bash.html">Bash Tutorial</a></h1> + <hr> + <h2>Contents</h2> + <div toc> + <script> showToc(content) </script> + </div> + <hr> + <script> showContent(content,2) </script> + </div> + </body> +</html>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/computer_literacy/computer_literacy.html Thu Oct 09 16:56:36 2025 -0600 @@ -0,0 +1,197 @@ +<!doctype html> +<html lang="en"> + <head> + <script src="/site.js"></script> + <script> head() </script> + <title>Arkian - Computer Literacy</title> + <script> +'use strict'; + +let content = { + intro: { + title: 'Introduction', + content: `\ +<p>Learning reactionary programming will give you a deep understanding of programming and will enable to you to write your own programs and websites. However this is not enough to get a job in depraved modern culture. Modern culture will expect you to use tools popular with them, so you would have to learn one of those. Probably the least horrible programming tool used by modern culture is <a href="python.html">Python</a>, so I would recommend learning that if you want a job.</p> + +<p>The next 4 lessons are basic computer literacy. Even if you are not interested in programming, these would be useful skills.</p> + +<p>I will develop this course incrementally based on demand. Whenever anyone completes the content here, I will add another lesson/section. This way I won't waste my time writing content that no one will use.</p> + +<p>This course will be designed for you to learn reactionary programming in a logical sequence. I will focus on finding relevant resources, and I will only write explanations when nothing else is available on the subject. I will focus on web development since that is what I know best.</p> + +<p>I will often say "Do X" without saying how to do X. A big part of programming is figuring out how to do things. Your two main resources are Google and ChatGPT. If you get stuck, you can ask me.</p> + +<p>ChatGPT has become an essential programming tool. So the only task for this lesson is to play with ChatGPT. Later you can ask it technical questions.</p> + +<p>If you have any questions, you can ask me on <a href="https://mikraite.arkian.net/Reactionary-Software-f1999.html">my forum</a>.</p> +` , + }, + editor: { + title: 'Text Editor and JSON', + content: `\ +<p>A text editor is a basic tool for reactionary programmers. We use simple flexible tools. Modern culture uses an <a href="https://en.wikipedia.org/wiki/Integrated_development_environment">IDE</a> which is big, <a href="https://mikraite.arkian.net/Changed-my-mind-about-IDE-s-tp2769.html">complicated</a>, and inflexible. We don't use such tools.</p> + +<p>I reviewed the available text editors and I think <a href="https://www.sublimetext.com/">Sublime Text</a> is the best one available. So please download it and learn how it works.</p> + +<p><a href="https://en.wikipedia.org/wiki/JSON">JSON</a> is the most popular text data format. I think JSON was the last good programming idea from modern culture, developed around 2001, before modern culture became fully depraved. Do your own research on JSON to understand it. It is fairly simple.</p> + +<p>I paired Sublime Text with JSON because Sublime Text uses JSON files for its <a href="https://www.sublimetext.com/docs/settings.html">settings</a>. (Actually not quite pure JSON because it includes comments which are lines starting with <code>//</code>.) Experiment with Sublime Text settings to learn more about both Sublime Text and JSON.</p> +` , + }, + bash: { + title: 'Bash - Command Line Shell', + content: `\ +<p><a href="bash.html">Bash</a> is the other basic tool for reactionary programmers. I was hoping to just reference existing Bash tutorials in this lesson, but they are all horrible, so I am writing my own. So for this lesson, please read <a href="bash.html">my tutorial</a>.</p> +` , + }, + regex: { + title: 'Regular Expressions', + content: `\ +<p><a href="https://en.wikipedia.org/wiki/Regular_expression">Regular expressions</a>, or regex for short, are used for searching or matching text. Since this is such a common need, regex are found everywhere in programming. Since you just learned Bash, you can learn regex with the <a href="https://en.wikipedia.org/wiki/Grep">grep</a> command that uses regex.</p> + +<p>Unlike with Bash, there are a few decent grep tutorials, so I don't have to write my own. Here they are:</p> + +<ul> +<li><a href="https://vegastack.com/tutorials/regular-expressions-in-grep/">Regular Expressions in Grep</a></li> +<li><a href="https://cs.nyu.edu/~mohri/unix08/lect4">Regular Expressions</a></li> +<li><a href="https://www.gnu.org/software/grep/manual/grep.html">GNU Grep</a></li> +</ul> + +<p>I suggest that you just read through these tutorials so that you get a sense for how regex and grep work. You don't need to practice anything here. You can come back to these tutorials later when you have a real need for regex or grep.</p> + +<p>The details of regex vary by implementation. Luan uses <a href="https://docs.oracle.com/javase/8/docs/api/java/util/regex/package-summary.html">Java regex</a>. + +<p>If you become a serious programmer, then you should have a deeper understanding of regex. In that case I recommend the book <a href="https://www.amazon.com/Mastering-Regular-Expressions-Jeffrey-Friedl/dp/0596528124/">Mastering Regular Expressions</a>.</p> +` , + }, + hg: { + title: 'Mercurial - Source Control', + content: `\ +<p>Thankfully <a href="mercurial.html">Mercurial</a> is well documented. Read <a href="https://hgbook.red-bean.com/">Mercurial: The Definitive Guide</a> up to (and including) chapter 9.</p> + +<p>To get started with Mercurial, download Mercurial from <a href="https://www.mercurial-scm.org/">their website</a> and install it. Then do:</p> + +<code block> +~/learn $ hg version +Mercurial Distributed SCM (version 5.2.2) +(see https://mercurial-scm.org for more information) + +Copyright (C) 2005-2019 Matt Mackall and others +This is free software; see the source for copying conditions. There is NO +warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +~/learn $ +</code> + +<p>If this works, it is installed properly. Next <a href="https://hgbook.red-bean.com/read/a-tour-of-mercurial-the-basics.html#sec:tour-basic:username">set up your Mercurial configuration file</a> which is <code>~/.hgrc</code> on a Mac or <code>/c/Users/$(whoami)/mercurial.ini</code> on Windows <a href="https://www.msys2.org/">MSYS2</a>. My <a href="https://www.mercurial-scm.org/doc/hgrc.5.html">hgrc</a> file looks like this: + +<code block> +[ui] +username = Franklin Schmidt <fschmidt@gmail.com> + +[auth] +hghosting.prefix = https://hg.reactionary.software/repo/ +hghosting.username = fschmidt@gmail.com +hghosting.password = xxxxxxxxxx + +[extensions] +hgext.extdiff = + +[extdiff] +cmd.dm = diffmerge +</code> + +<p>You should register on <a href="https://hg.reactionary.software/">our Mercurial hosting service</a> and use your email as the username and the assigned password in <code>.hgrc</code>. The last two blocks set up <code>hg dm</code> to call <a href="https://sourcegear.com/diffmerge/">DiffMerge</a> by using the <a href="https://wiki.mercurial-scm.org/ExtdiffExtension">Extdiff extension</a>.</p> + +<p>Now you can play with <a href="https://hg.reactionary.software/repo/test/">https://hg.reactionary.software/repo/test/</a> like this:</p> + +<code block> +~/learn $ hg clone https://hg.reactionary.software/repo/test/ hgtest1 +no changes found +updating to branch default +0 files updated, 0 files merged, 0 files removed, 0 files unresolved +~/learn $ hg clone https://hg.reactionary.software/repo/test/ hgtest2 +no changes found +updating to branch default +0 files updated, 0 files merged, 0 files removed, 0 files unresolved +~/learn $ ls -Fd h* +hgtest1/ hgtest2/ +~/learn $ cd hgtest1 +~/learn/hgtest1 $ touch file.txt +~/learn/hgtest1 $ e file.txt +~/learn/hgtest1 $ cat file.txt +some text +~/learn/hgtest1 $ hg status +? file.txt +~/learn/hgtest1 $ hg add file.txt +~/learn/hgtest1 $ hg status +A file.txt +~/learn/hgtest1 $ hg pull +pulling from https://hg.reactionary.software/repo/test/ +no changes found +~/learn/hgtest1 $ hg commit -m 'add file.txt' +~/learn/hgtest1 $ hg push +pushing to https://hg.reactionary.software/repo/test/ +searching for changes +remote: adding changesets +remote: adding manifests +remote: adding file changes +remote: added 1 changesets with 1 changes to 1 files +~/learn/hgtest1 $ cd ../hgtest2 +~/learn/hgtest2 $ hg pull +pulling from https://hg.reactionary.software/repo/test/ +requesting all changes +adding changesets +adding manifests +adding file changes +added 1 changesets with 1 changes to 1 files +new changesets aab34516d8dc +(run 'hg update' to get a working copy) +~/learn/hgtest2 $ hg update +1 files updated, 0 files merged, 0 files removed, 0 files unresolved +~/learn/hgtest2 $ hg status +~/learn/hgtest2 $ cat file.txt +some text +~/learn/hgtest2 $ e file.txt +~/learn/hgtest2 $ cat file.txt +some text +and more +~/learn/hgtest2 $ hg status +M file.txt +~/learn/hgtest2 $ hg pull +pulling from https://hg.reactionary.software/repo/test/ +searching for changes +no changes found +~/learn/hgtest2 $ hg dm +~/learn/hgtest2 $ hg commit -m 'and more' +~/learn/hgtest2 $ hg push +pushing to https://hg.reactionary.software/repo/test/ +searching for changes +remote: adding changesets +remote: adding manifests +remote: adding file changes +remote: added 1 changesets with 1 changes to 1 files +</code> +` , + }, + qqq: { + title: 'qqq', + content: `\ +` , + }, +}; + </script> + </head> + <body> + <script> header() </script> + <div content> + <h1><a href="computer_literacy.html">Computer Literacy</a></h1> + <hr> + <h2>Contents</h2> + <div toc> + <script> showToc(content) </script> + </div> + <hr> + <script> showContent(content,2) </script> + </div> + </body> +</html>