How to remember which expansion ${var%} ${var#} work from which end? [closed]
Asked Answered
C

5

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?

Countertenor answered 1/3, 2021 at 11:39 Comment(2)
I just always try one, and if it fails, then the other :PAcanthaceous
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 displayCrackbrain
I
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.

Imprecise answered 1/3, 2021 at 12:19 Comment(1)
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.Countertenor
D
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.

Decima answered 1/3, 2021 at 11:46 Comment(1)
"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.Dreyer
G
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 :)

Glomerulus answered 1/3, 2021 at 12:0 Comment(0)
L
6

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

Loxodrome answered 1/3, 2021 at 11:45 Comment(1)
On American keyboards at least. On mine, % is on the left and # is on the right.Sacramental
I
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.

Imprecise answered 1/3, 2021 at 12:19 Comment(1)
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.Countertenor
E
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).

Eldridgeeldritch answered 1/3, 2021 at 13:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.