Here's a preview from my zine, Bite Size Bash!! If you want to see more comics like this, sign up for my saturday comics newsletter or browse more comics!
read the transcript!
panel 1: ${...}
is really powerful
person: “it can do a lot of string operations, my favourite is search/replace
panel 2: ${var}
same as $var
panel 3: ${#var}
length of the string or array var
example:
$ x=panda
$ echo ${#x}
5
panel 4: ${var/bear/panda}
search & replace. Example:
$ x="I'm a bearbear!
$ echo ${x/bear/panda} # replace 1 instance of 'bear'
I'm a pandabear!
$ echo ${x//bear/panda} # replace every instance of 'bear'
I'm a pandapanda!
panel 5: ${var:-othervar}
use a default value if var
is unset/null
Example:
echo ${asdf:-some default value}
panel 6: ${var:?some error}
prints “some error” and exits if var
is null or unset
panel 7: ${var#pattern}
and ${var%pattern}
remove the prefix/suffix pattern
from `var.
Example:
$ x=motorcycle.svg
$ echo "${x%.svg}"
motorcycle
panel 8: ${var:offset:length}
get a substring of var
. Example:
$ x='panda bear time'
$ echo ${x:6:4}
time
panel 9
person: “there are LOTS more, look up ‘bash parameter expansion’!”
Saturday Morning Comics!
Want another comic like this in your email every Saturday? Sign up here!