Bash

GNU Bourne-Again SHell, an POSIX(file system standard) compliant shell thingy. sh is a earlier verison

The language with literally multiple ways to do everything xO

Alternatives include zsh shell which is default interactive shell after Catalina(you can still run bash scripts with bash with a shebang)

Defining a Bash Script

#!/bin/bash
echo Hello World, arg $1

Call with ./scriptname.sh test as that uses the shebang

Can just run in bash without permission with bash scriptname.sh or . scriptname.sh

Variables

var1='soap' #no spaces
var1=soap

echo $var1 		# => soap
echo ${var1}	# => soap
echo var1 		# => var1

Make var avaliable to all subprocesses

export name=value 

String Interpolation

Single quotes won't interpolate anything, but double quotes will.

Print

printf has more standard behavior doing stuff like newlines

Evaluate

^generally better(newer and ez nesting without esacpes)

Find Length

Functions

We supply the arguments directly after the function name. Within the function they are accessible as $1, $2, etc.

Export with export -f myfun so usable by subshells

Special Parameters

Symbol
Equivalent
Example

$@

"$1 " "$2"

`for INPUT in "$@"

$*

"$1c$2c..." where c is the first value of the IFS variable

Conditionals

man test for info about conditionals

man test for a shit ton more about [ ], p.s never forget the spaces

Controls

Exit

exit [n] will close shell with exit status of [n]

Last updated