Bash Programming Notes. ======================= Syntax Rules : -------------- '#' - Donates a comment, can be placed anywhere on a line. Everything after it will be ignored until a new line. '#!' - Donates the interpreter (when on the first line). ';' - Command separator. ';;' - Case option terminator. '.' - Equivalent to source command. '"' - Allows special characters to be interpreted. ''' - String literal, no special character interpretation. ',' - Used to seperate mathamatical expressions all are executed but only the last is returned. '\x' - Donates an escape/special character to be interpreted. '`' - This allows a programs output to be assigned to a variable. ':' - Null command, returns true always. '!' - Reverse the value of a true|false value or expression. '*' - Wild card operator. Or arithmetic op for multiplication. '**' - Exponent op. '?' - Single character wild card. '$' - Variable substitution. '${}' - Parameter substitution. '()' - Command group. '{}' - Code block. '[]' - Test construct. '[[]]'- Test Construct. '[]' - Array element. '(())'- Integer expansion. '&&' - AND logical operator. '||' - OR logical operator. Scriptlets : ------------ echo $((2#10101001)) - Base conversion. Special Shell Variables : ------------------------- '$0' - Script name. '${n}' - Parameter. '$#' - Number of parameters. '$*' - All parameters as a single word. '$@' - All parameters as seperate strings. '$?' - Return value. '$$' - Script PID. '$PWD' - Current working directory. '$OLDPWD' - Old working directory. Comparison Operators : ---------------------- '-eq' '=' '==' - Equality. '-ne' '!=' - Inequality. '-lt' '\<' '<' - Less than. '-le' '<=' - Less than or equal. '-gt' '\>' '>' - Greater than. '-ge' '>=' - Greater than or equal. '-z' - String is empty. '-n' - String is not empty. Test Operations (files) : ------------------------- '-e' - File exists. '-s' - File is not zero size. '-f' - File is a regular file. '-d' - File is a directory. '-h' '-L' - File is a symlink. '-b' - File is a block device. '-c' - File is a character device. '-p' - File is a pipe. '-S' - File is a socket. '-r' - File has read permission. '-w' - File has write permission. '-x' - File has execute permission. '-t' - File has a terminal association. '-N' - File has been modified since last read. '-O' - File owned by executing UID. '-G' - File has same GID as executor. '-g' - SGID flag set. '-u' - SUID flag set. '-k' - Sticky bit set. '!' - Reverses sense of file test operations. String Operations : ------------------- ${#stringName} - Length of string. ${varName:position} - Extract sub-string at position. ${varName:position:length} - Extract sub-string of length. Flow Control : -------------- If Statements : --------------- if [ expression ] then # Code to be executed. fi if [ expression ] then # Code to be executed. else # Code to be executed if expression not satisfied. fi if [ expression ] then # Code to be executed. elif [ expression ] then # Code to be executed. else # Code to be executed. fi Case Statement : ---------------- case "$varName" in abc) echo "First case." ;; def) echo "Second case." ;; ghi) echo "Third case." ;; jkl) echo "Forth case." ;; esac For Loop : ---------- for [currentItr] in [itrList] do [ command ... ] done