Effective Bash: Know How to Read `#` and `%` in Parameter Expansions
I've struggled for years to come up with mnemonic for # and % in paramater expansion. % is easy because you can think of it as modulo or 'without the remainder' so it masks at the end. I alwas just sort of thought of # as 'the other one' but that wasn't very satisfying.
Now I read # as 'mask' and % as 'modulo' and I think it finally makes sense. Mask is for the beginning and modulo is for the end. ## is longest mask and %% is longest modulo.
$ x=charnock $ cat <<EOF > masked x=${x#char} > longest masked x=#{##*o} > modulo x=${x%ock} > longest modulo x=${x%%n*} > EOF masked x=nock longest masked x=#{##*o} modulo x=charn longest modulo x=char
This gives me a lot more confidence when using parameter expansion which is an intrinsic part of being effective at bash.








