7

I am having trouble remembering which one of the parameter expansions ${var%subst} or ${var#subst} remove from the front and which one from the back of the string. Example:

$ var=/a/b/c
$ echo dirname=${var#/*} filename=${var%%*/}
dirname=a/b/c filename=/a/b/c                      # Wrong!
$ echo dirname=${var%/*} filename=${var##*/}
dirname=/a/b filename=c

I always mix them and either end up writing some test commands or checking the manual. It's easy to remember that %% removes more then %, because %% is a longer string then %, 2 characters vs 1 character, same for ## vs #. But I always mix % with #.

Is there a memory rule to know which % or # remove from which end of the string?

Quasímodo
  • 3,812
  • 14
  • 25
KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • 2
    I just always try one, and if it fails, then the other :P – ilkkachu Mar 01 '21 at 16:36
  • consider a cheat sheet of examples; could be a `*.txt` file you can quickly double-click from the desktop, or perhaps define an alias to `cat` the cheat sheet at the command prompt; if you end up with a lot of cheat sheets consider a shell script that runs an input arg through a `case` statement to determine which cheat sheet to display – markp-fuso Mar 01 '21 at 17:46

5 Answers5

13

Percent symbols % always come last in numbers (e.g. 86%), so they remove from the end.

The hash symbols # start comments, so they remove from the start.

Quasímodo
  • 3,812
  • 14
  • 25
  • 3
    "The #1 rule, useful 100% of the time" - That's the phrase I use. Hence `#` removes from the start because it's at the start, `%` removes from the end for the opposite reason. Then the only complication is greedy/non-greedy mode. Obviously people who want *more* (such as `##` rather than `#`) are greedy. – paxdiablo Mar 04 '21 at 07:55
8

Remember only 1 and other will be Opposite of it.

# Shebang starts from a hash which means it will remove from starting till pattern, if you remember this and you know what other does IMHO :)

RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
6

For keyboards of US English layout, # is on the left and % on the right.

pynexj
  • 19,215
  • 5
  • 38
  • 56
1

I use a figurative representation of the symbols:

  • %: Looks like a pair of scissors to cut the right part of a text strip while you hold the scissors in your right hand and the strip in your left hand.

  • #: Looks like an eraser you use to erase the left part of a text strip while you hold the right part with your right hand and the eraser in your left hand.

  • //: This bash specific text-replace, looks like the cuts in a strip to re-assemble or edit in-between parts.

Léa Gris
  • 17,497
  • 4
  • 32
  • 41
  • Thank You! `%: Looks like a pair of scissors to cut the right part of a text strip` This, worked for me, it just "clicked", because scissors are so visual, I imagined that once, and now I always think of scissors when writing scripts. Then I remember that `#` is the opposite of `%`, and I have it. – KamilCuk Oct 06 '21 at 00:18
0

And I thought I was alone with this problem.

My mnemonic is that since percent sign (%) looks like a slash surrounded by a couple of circles (°/o), put the slashes alongside ie. %/ - also, that also looks like it says o lol (°/o/).

With the asterisk in place (°/o/*) it also looks like a guyperson holding an orb and a BZZRT in hishir raised hands (can not be unseen).

James Brown
  • 36,089
  • 7
  • 43
  • 59