Bash script
Basics
#!/bin/bash# add current working directory to path
PATH=$PATH:$PWD# instead of
#!/usr/local/bin/python3
# use
#!/usr/bin/env python3Differences between shell functions and other scripts (e.g. python scripts)
Variables
a=Hello # no spaces!
# a = Hello means run a with = and Hello as arguments
echo $a # Hello
echo "$a" # Hello
echo '$a' # $a
echo a # a
# add attributes to variables
declare -i d=123 # d is an integer
declare -r e=456 # e is read-only
declare -l f="LOLCats" # f is lolcats
declare -u g="LOLCats" # g is LOLCATS
# Built-in varibles
# $HOME $PWD $MACHTYPE $HOSTNAME $RANDOM
# $BASH_VERSION related: bash --version
# $0 name of the script
# $SECONDS time since this bash session had run / since the script started
# env: shows all environment variables
d=$(pwd) # run pwd and put the result in d.
# Arithmetic (only integers are supported)
val=$((expression))
# operators: **, %, +-*/%
e=5
e+=2 # string concatenation
((e+=2)) # arithmeticCommand substitution
Process substitution
Comparisons
Strings
Reading and Writing Text Files
Control Structures
if
while/until
for loop
Case (switch in C)
Array
Functions
Arguments
Interact with the User
Working with flags
Get input during execution
Ensuring a response
Date and Printf
Advanced Topics
Debug Mode
Brace Expansion
Print Colored Text
Here Documents
References
Last updated