$FUNCNAME
is the current functionShell
eg. bash
(sh
but “bourne” again), zsh
(default for macOS), tcsh
(NIH favorite)
Tutorials
- https://www.tutorialspoint.com/unix/index.htm starts super super basic but covers advance topics (via Orma)
- https://www.learnshell.org/ (via Amar)
- AFNI's unix intro to scripting
- Andy's brain blog book unix chapter includes videos
- Pitt's center for research computing suggests
Readline Keyboard Shortcut reference
Utilities
fzf, fasd (z), readline editing options, alt+., ctrl+r, fuzzy ctrl+r, alt+n newfiles
Idioms
short if-statements. short form function definitions. regexp w/BASH_REMATCH
if [ -r file.txt ]; then rm file.txt fi # same as [ -r file.txt ] && rm file.txt # same as test -r file.txt && rm "$_" #### function abc { echo abc } # same as abc(){ echo abc; } ### foobar="a1 b2 c3" [[ $foobar =~ c.* ]] && echo $BASH_REMATCH # c3
Input Arguments
Both functions and scripts take “input arguments”: what's provided to the right of the script or function.
“$#”
is number input args, “$*”
is all as single string “$@”
is each argument quoted
$1
.. $9
are input arguments 1 to 9 1). printf
like echo
but first argument is how to format what follows
nargs() { echo "$#"; } arg_at() { nargs "$@"; } arg_star() { nargs "$*"; } arg_at a b c # 3 arg_star a b c # 1 second() { printf "*%s*\n" "$2"; } arg_at2() { second "$@"; } arg_star2(){ second "$*"; } arg_at2 a b c # *b* arg_star2 a b c # **
1)
'$0' is the scripts name,