💾 Archived View for gemini.susa.net › bash_notes.gmi captured on 2021-12-05 at 23:47:19. 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//[