view src/learn_bash.html.luan @ 64:b91375e2a28a

minor
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 16 Aug 2024 11:06:52 -0400
parents 3082eaf8457d
children b1982e401900
line wrap: on
line source

local Luan = require "luan:Luan.luan"
local error = Luan.error
local pairs = Luan.pairs or error()
local Io = require "luan:Io.luan"
local Http = require "luan:http/Http.luan"
local Shared = require "site:/lib/Shared.luan"
local head = Shared.head or error()
local header = Shared.header or error()


local content = {
	intro = {
		title = [[Introduction]]
		content = function()
%>
<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 crap.  And they don't explain core concepts.  So I have no choice but to write this for my <a href="http://localhost:8080/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>
<%
		end
	}
	access = {
		title = [[Running Bash]]
		content = function()
%>
<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>
<%
		end
	}
	start = {
		title = [[Getting Started]]
		content = function()
%>
<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>
<%
		end
	}
	man = {
		title = [[The "man" Command]]
		content = function()
%>
<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>
<%
		end
	}
	dirs = {
		title = [[Directories]]
		content = function()
%>
<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>
<%
		end
	}
	files = {
		title = [[Working with Files]]
		content = function()
%>
<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>
<%
		end
	}
	quote = {
		title = [[Quoting]]
		content = function()
%>

<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>

<%
		end
	}
	vars = {
		title = [[Variables]]
		content = function()
%>

<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="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>

<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>

<%
		end
	}
	bash_profile = {
		title = [[.bash_profile]]
		content = function()
%>
<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>

<%
		end
	}
	find = {
		title = [[The "find" Command]]
		content = function()
%>
<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>
<%
		end
	}
	io = {
		title = [[Input and Output]]
		content = function()
%>
<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 &lt;test.txt 
this is another test
another line
~/learn $ cat &lt;&lt;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>&lt;file</code> reads standard input from <code>file</code>.  <code>&lt;&lt;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>
<%
		end
	}
	ctrl = {
		title = [[Control Keys]]
		content = function()
%>
<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>
<%
		end
	}
	subst = {
		title = [[Command Substitution]]
		content = function()
%>
<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>
<%
		end
	}
	ampersand = {
		title = [[Ampersand]]
		content = function()
%>
<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>
<%
		end
	}
	scripts = {
		title = [[Shell Scripts]]
		content = function()
%>
<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>
<%
		end
	}
	vars_and_scripts = {
		title = [[Variables and Scripts]]
		content = function()
%>
<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>

<%
		end
	}
	your_scripts = {
		title = [[Your Scripts]]
		content = function()
%>
<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>
<%
		end
	}
	advanced = {
		title = [[Advanced Scripting]]
		content = function()
%>
<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>
<%
		end
	}
	conclusion = {
		title = [[Conclusion]]
		content = function()
%>
<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>
<%
		end
	}
}


local function show_toc(content)
%>
			<ul>
<%
	for id, info in pairs(content) do
%>
				<li><a id="c_<%=id%>" href="#<%=id%>"><%=info.title%></a></li>
<%
	end
%>
			</ul>
<%
end

local function show_content(content,h)
	for id, info in pairs(content) do
%>
			<div heading>
				<h<%=h%>><a id="<%=id%>" href="#<%=id%>"><%=info.title%></a></h<%=h%>>
				<a href="#c_<%=id%>">contents</a>
			</div>
<%
		info.content()
	end
end

return function()
	Io.stdout = Http.response.text_writer()
%>
<!doctype html>
<html>
	<head>
<%		head() %>
		<title>Reactionary Bash Tutorial</title>
	</head>
	<body>
<%		header() %>
		<div content>
			<h1><a href="learn_bash.html">Reactionary Bash Tutorial</a></h1>
			<hr>
			<h2>Contents</h2>
			<div toc>
<%			show_toc(content) %>
			</div>
			<hr>
<%			show_content(content,2) %>
		</div>
	</body>
</html>
<%
end