💾 Archived View for gemini.susa.net › bash_notes.gmi captured on 2022-06-03 at 23:24:08. Gemini links have been rewritten to link to archived content
⬅️ Previous capture (2021-11-30)
-=-=-=-=-=-=-
The following are notes on bash usage that I often need to refer to.
length ${#MYVAR} substring ${MYVAR:2}, ${MYVAR, 0, 4} (third parameter is length) from end ${MYVAR:(-4)} (avoid :- interpreted as default) with vars ${MYVAR:$START:$LEN} parameters ${*:1} or ${@:1} (positional parameter) multi ${*:2:3} three parameters starting from the second
${MYVAR#/home} delete shortest match from start of string ${MYVAR##/home} delete longest match from start of string ${MYVAR%.exe} delete shortest match from end of string ${MYVAR%%.exe} delete longest match from end of string ${MYVAR%%$SUFF} can use a variable ${MYVAR##gemini*cgi-bin} delete longest match with wildcard
Suppose MYVAR='mykey=myvalue', then
${MYVAR%%=*} yields 'mykey' (delete =* from end of string) ${MYVAR##*=} yields 'myvalue' (delete *= from start of string)
substitute ${MYVAR/old/new} all ${MYVAR//old/new} delete ${MYVAR/old} delete all ${MYVAR//old} at start ${MYVAR/#usr/var} at end ${MYVAR/%.exe/.sh}
Remove whitespace and newlines: ${MYVAR//[