Basics
Starting the script
Shebang: #!
: used by the shell to decide which interpreter to run the rest of the script
- Starts with a “shebang”
#!
and path to shell you want script to use#!/bin/bash
Executing the script
- Assign execution rights to user:
chmod u+x <file>.sh
- chmod –> modifies ownership of a file for the current user: u
- +x –> execution rights
Variables and data types
- Every variable is an array so can start using any variable as an array.
Set a variable
thing=1
-
Cannot have spaces in between (NO GOOD:
thing = 1
)- If you want to use a space:
(( foo = 3 ))
- If you want to use a space:
-
Can also reassign
thing=$foo
-
If includes spaces, need to quote around it
thing="hello world"
Reference a variable
A reference to a variable is an implicit reference to the first index
echo $thing
-
Use dollar sign
$
-
For variable operations and arrays, wrap around braces
echo ${foo}
-
Reference index
foo[0]=1
foo[1]=2
echo ${foo[1]} # prints 2
Delete a variable
foo=1
echo $foo
unset foo
echo $foo