Scripting and Programming

Created Tuesday 23 February 2021

Shell Scripting

So far, we've been doing everything on the command line. What if we want to do 2 or more things?

& - run another command SIMULTANEOUSLY

&& run another command only after the first has completed

|| run another command after the first FAILS

; - run another command after the first has completed
regardless of outcome

Scripting Cont'd

Well, you could also put a bunch of commands in one file, one command per line, and then run that file.


Bash Scripting

Start with a SHEBANG!
(note, # is also the comment delimiter)
End by saving AND chmodding
chmod +x


Note, they must be run with a FULLPATH!
But /home/user/
(which can be shortened to ./ )

This is for your own good ☺


Set them without $, use them with $ (NO SPACES)

thingtoecho=“Hey, this will be echoed”
echo $thingtoecho

Or “read” them from standard input

read $yourname
echo “Whattup $yourname”


Bash is strongly oriented toward:

Whitespace (Spaces and tabs) as "Delimiters"

john r marks
Means a john then an r then a marks
john r marks as a unit ... unless

Notes on Quotes

“Double Quotes” – Print contents, expand variables
'Single Quotes' - Print contents LITERALLY
`backticks` - Execute command, put contents in quotes* (dont use this ever)

(also, backslashes “literalize” special chars)

ALWAYS put double quotes around called variables (dollar signed ones)
GOOD IDEA to do curly braces, too (esp if your output isn't spaced normally)

Notes on Quotes (get our REPL on)

Consider the following terrible idea:
"Date" is a command. It means, give the date.
(And time. And is a very powerful command)

So, what do the following do?

date=“eh, whenever”

Note, never use backticks, use $(command)

That functionality is super cool — it replaces a "command" with the output it would give as if it were a variable;
But do it with $(command) — (dollar sign + Parentheses)
and note this is different from
${variablename} - {dollar sign + curly braces}
which is just a way to set off the name of the variable if you want your output to be unspaced.

and of course you'll probably want double quotes around these
and possible related output as well (man, so much to think about)

Obvious Use of Variables

But, lets clean up and make this more portable

Special Variables-Environment

Environment Variables - "Built into the system"
Usually all caps and contain system/shell info
(don't make your variables all caps)


Special Variables - Arguments

$1 is first, $2 is second, and so on.
$# is number of args, $* is all of them, eg

Create a command

Make a file called

echo “I'm sorry about all the password stuff, $1”

Usage: "Your name"

(Consider if it will do last or first, and how?)

Making decisions.

Now you're playing with power. Seriously

Expressing conditions

True = 0; False = 1 (or anything else)

Generally just use double brackets:


Conditions with commands

In Bash, sometimes there's a slick way to use conditions with commands.
The general rule is, if a command HAS a result, it's true, if not, it's false.


“While” is very similar to if; it keeps repeating
the loop while a condition is true.

(while + read + “cat pipe” or “<” ) 

is very good for reading files


for variable in (range or list)

— NOTE, for obeys general argument rules. e.g.

for 1 2 3 — thus you can do slickness like

for $(seq 1..20) or for $(./folder/*)


Remember aliases and ?

All related to FUNCTIONS
,and they're so general and powerful
.. they're kind of hard to talk about. You can think of them as
their own commands, or programs and whatnot
..and arguably the most powerful and robust languages in
the world are "functional"

function say_hi_to () {

echo "Hello $name"

echo "Hello