CLEλR-C0DE

DRAFT Bash

A script file needs to be executable:

In the first line it should contain a shebang (#!) indicating the script language:

#!/bin/bash

or

#!/usr/bin/env bash

Comments

Comments start with the pound symbol:

# This is a comment
echo "Hello" # and this is another one

Script arguments

$? contains the exit status of the last invocation.

Fire And Forget

If a command is terminated by the control operator ‘&’, the shell executes the command asynchronously in a subshell. This is known as executing the command in the background, and these are referred to as asynchronous commands.

command &

Exit status

Every script and function returns the status code of the last executed instruction. That numeric value is:

It can also be returned explicitly with:

Variable

There cannot be a space before the equal = symbol, or it will be interpreted as a function call!

In general, when using variables favor both, quotes and curly brackets:

  1. echo variable_name
  2. echo $variable_name # nicer
  3. echo "${variable_name}other text here" # the nicest

DECLARE ??? (SET, UNSET, ENV)

CONSTANTS

INTEGERS

ARRAYS (and ASSOCIATIVE ARRAYS)

FUNCTIONS

EXPORT

Function local

f() {
  local name=value  
}

Local

name=value

Just for the call

name=value command arg1 arg2

Session

export name=value

Prompt user

Prompt the user with echo:

read -p "Enter a value:" variable_name

Prompt the user without echo (silent):

read -sp "Enter a value:" variable_name

Value checks

When writing direct checks (no if, while, etc) we need [] or [[]], except for regex checks (=~ and == with path style) that can only be used with [[]]. These brackets need to be surrounded by spaces!

TEST !!! (test or [[ or [ or (( ))

Existence tests:

Permission tests:

Quick logic

IF

if [ check ] ; then
  ...
fi
if [ check ] ; then
  ...
else
  ...
fi
if [ check ] ; then
  ...
elif [ check ] ; then
  ...
else
  ...
fi

For oneliners an extra ; is needed after every command.

if [ check ] ; then  ... ; fi

AND / OR

if [ check ] && [ check ] ; then
  ...
fi
if [ check ] || [ check ] ; then
  ...
fi

Modern arithmetic checks

The $ sign in variable names can be dropped

if (( numeric_check )); then
    
fi

FOR

for loop_variable in value_list ; do
  ...
done 

C-style

for (( init ; check ; inc )) ; do
  ...
done 

Array iteration idiom

for (( i=0 ; i<${#array[*]} ; i++ )) ; do
  ...
done 

WHILE/UNTIL

while [ check ] ; do
  ...
done
until [ check ] ; do
  ...
done

BREAK/CONTINUE

CASE

Matches using Shell pattern matching rules.

case argument in
  pattern )
    ...
  ;;
  pattern )
    ...
  ;;
  * )
    ...
  ;;
esac
case argument in
  pattern | pattern )
    ...
  ;;
  pattern | pattern | pattern )
    ...
  ;;
  * )
    ...
  ;;
esac

EXEC ???

Functions

Function definition (should be ahead use):

function_name() {
  ...
}

alternative version

function function_name() {
  ...
}

Function arguments follow the same pattern as the script arguments.

Management

List of existing functions names: declare -F

View function definition: type function_name