bash

Positional Parameters

bash

$0-$9 You can access parameters greater than 9 with parameter expansion. ${10} $# Number of arguments use shift to get all parameters with a loop #!/bin/bash # posit-param2: script to display all arguments count=1 while [[ $# -gt 0 ]]; do echo "Argument $count= $1" count=$((count + 1)) shift done A useful trick is to PROGNAME="$(basename "$0")" in the usage for the program name. ...

Process Substitution

bash

Process Substitution is a form or redirection where the input of output of a process appear as a temporary file. <( <LIST> ) >( <LIST> ) Explanation: The command LIST is executed and its: standard output filedescriptor in the <( … ) form or standard input filedescriptor in the >( … ) form is connected to a FIFO or a file in /dev/fd/. Useful for redirecting output of a command to something that expects input from a file and not from stdin. ...

Programming

dev, bash, python

Languages # Bash Python Lisp Clojure JavaScript Lua Paradigms # Imperative Functional Syntax # Code Linting Machine Learning # Physics-based Deep Learning Misc. # Art of problem solving: Hammock Driven Development 20220623214742-cool-dev-shit The real cost of context switching

Scoping In Bash

bash

Global Scope # Global: maintain existence throughout script foo=0 # global variable foo Local Scope # Local: only accessible within function they’re defined in Often desirable to limit scope of variables; prevents name conflicts. funct_1 () { local foo # variable foo local to funct_1 foo=1 echo "funct_1: foo = $foo" }

Special Parameters

bash

Both the $* and $@ variables contain all of of the parameters passed to the script. They’re a single special variable that contains all of the parameters used when the script was ran. Parameter Description $* Expands into the list of positional parameters, starting with 1. When “$*” expands into a double-quoted string each separated by first IFS shell variable (default=space). ...

String Comparison Operators

bash

Expression Is True If… string string is not null -n string The length of string is greater than zero -z string The length of string is zero string1 = string2 string1 and string2 are equal. Single or double string1 == string2 equal signs may be used. The use of double equal signs is supported by bash and is preferred, but is not POSIX compliant string1 ! ...

String Operations

bash

String length # ${#parameter} Expands into length of the string contained by parameter. If parameter is either ‘@’ or ‘*’ then results in number of positional parameters. Substring expansion # ${parameter:offset} ${parameter:offset:length} Expands to portion of string defined by offset and length. End of string unless length is specified. If offset negative taken from end of string. (Space needed not to be confused with ${parameter:-word}, ${foo: -5}). Substring removal # ${parameter#pattern} ${parameter##pattern} Remove leading portion of string contained in parameter defined by pattern. ...

Temporary Files

bash

To avoid temp race attack give temporary files unpredictable file-names. Temp files usually stored in /tmp tempfile=/tmp/$(basename $0).$$.$RANDOM Or even better us mktemp tempfile=$(mktemp /tmp/foobar.$$.XXXXXXXXXX) For scripts executed by regular users may be better to use local /tmp ~[[ -d $HOME/tmp ]] || mkdir $HOME/tmp