Here's a preview from my zine, Bite Size Bash! If you want to see more comics like this, sign up for my saturday comics newsletter or browse more comics!
get the zine!
read the transcript!
how to set a variable
var=valueright (no spaces!)var = valuewrong
var = value will try to run the program var with the arguments “=” and “value”
how to use a variable: “$var”
filename=blah.txt
echo "$filename"
they’re case sensitive. environment variables are traditionally all-caps, like $HOME
there are no numbers, only strings
a=2
a="2"
both of these are the string “2”
technically bash can do arithmetic, but I avoid it
always use quotes around variables
$filename="swan 1.txt"
$ cat $filename (wrong)
bash: ok, I’ll run cat swan 1.txt
2 files! oh no! we didn’t mean that!
cat: Um swan and 1.txt don’t exist…
$ cat “$filename” (right!)
bash: ok, I’ll run cat "swan 1.txt"
cat ‘“swan 1.txt”`! that’s a file! yay!
${varname}
To add a suffix to a variable like “2”, you have to use ${varname}. Here’s why:
$ zoo=panda
$ echo "$zoo2" prints "", zoo2 isn’t a variable
$ echo "${zoo}2" this prints “panda2” like we wanted