ARRAY=(a b c d)cycle_array(){printf'%s\n'"${ARRAY[${i:=0}]}"(( i = i >= ${#ARRAY[@]}-1?0:++i ))}
Get first/last n element of an array
printf'%s\n'"${ARRAY[@]:0:n}"printf'%s\n'"${ARRAY[@]: -n}"# Last n args POSIXset -- arg1 arg2 arg3
shift"$(($#-n))"printf'%s\n'"${@}"
head and tail with bash
head(){mapfile-tn"$1" Line <"$2"printf'%s\n'"${line[@]}"}tail(){mapfile-tn0 Line <"$2"printf'%s\n'"${line[@]: -$1}"}
$ head2file
$ tail2file
Ternary test
## Set the value of x to y if y is greater than x.# x → variable to set# y > x → condition to test# ? y → if the test succeeds# : x → if the test fails(( x = y > x ? y : x ))
Log script output
# Start loggingexec6>&1# save stdoutexec7>&2# save stderrexec&>"$1"# Stop loggingexec1>&66>&- # Restore stdoutexec2>&77>&- # Restore stderr# Redirect errorprintf'%s\n''Err: This is an error message.'>&2