view src/learn_bash.html.luan @ 47:84dd3edd03e9

learn_bash work
author Franklin Schmidt <fschmidt@gmail.com>
date Sat, 06 Jan 2024 21:28:44 -0700
parents 89fdc29b296f
children 889e3c2d2699
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 <a href="bash.html">Bash</a> tutorials are so horrible that I have no choice.  I looked at books, websites, and YouTube - all horrible.  They don't start with the basics.  They include all kinds of useless crap.  And they don't explain core concepts.  So I have no choice but to write this damn thing for my <a href="http://localhost:8080/learn.html#bash">Learn Reactionary Programming</a> Bash lesson.</p>

<p>I will focus on Mac and Windows.  I don't have Linux, and I hate Linux, so I won't discuss it.  Most of Bash is the same on Mac and Windows, but where they differ, I will discuss both.</p>
<%
		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 echos what comes after.  Now press the up-arrow on your keyboard.  This should put the previous command where your cursor is.  Up-arrow again brings the command before that.  Try down-arrow and left-arrow and right-arrow.  You can use this to navigate through your command history.  The delete key also works for editing lines.  And of course you can type.  When you press return/enter then Bash will get your edited command and process it.</p>

<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>
<%
		end
	}
	man = {
		title = [[The "man" Command]]
		content = function()
%>
<p>Enter:</p>
<code block>
~ $ man echo
</code>

<p>You should get:</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.<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 commands.</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
	}
	ctrl_c = {
		title = [[Control+c]]
		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>
<%
		end
	}
	later = {
		title = [[placeholder]]
		content = function()
%>
<p>later</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.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