You are not logged in.

#1 2020-11-16 11:55:03

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,595
Website

Possible to combine two parameter substitutions in bash [solved]

My googlefu is not up to par.  I am wondering how to combine two parameter substitutions in bash within the same expression.

For example:

_tag=v5.10-rc4

I want to strip out the literal "v" and "-" to give:

5.10rc4

I can do it using an intermediate variable but am unsure how to combine two substitutions in the same step.

_tag=v5.10-rc4
_step=${_tag#v}
echo ${_step/-/}
5.10rc4

Last edited by graysky (2020-11-17 11:23:18)


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#2 2020-11-16 12:06:45

GaKu999
Member
From: US/Eastern
Registered: 2020-06-21
Posts: 696

Re: Possible to combine two parameter substitutions in bash [solved]

graysky wrote:

can do it using an intermediate variable but am unsure how to combine two substitutions in the same step.

_tag=v5.10-rc4
_step=${_tag#v}
echo ${_step/-/}
5.10rc4

Since the substitutions can only operate on one variable at a time...that is a limitation.
Neither POSIX nor bash give constructs for double substitutions to a variable.

Similar questions I have found with my also not up to par duckfu tongue
https://stackoverflow.com/questions/295 … osix-shell
https://unix.stackexchange.com/question … sh-and-zsh


My reposSome snippets

Heisenberg might have been here.

Offline

#3 2020-11-16 13:45:37

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,444
Website

Re: Possible to combine two parameter substitutions in bash [solved]

echo ${_tag//[v-]/}

"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#4 2020-11-16 14:08:43

GaKu999
Member
From: US/Eastern
Registered: 2020-06-21
Posts: 696

Re: Possible to combine two parameter substitutions in bash [solved]

Trilby wrote:
echo ${_tag//[v-]/}

Well, that is regex that works for this simple test case. tongue

...

Darnit that is in ash and not in dash! o_O

Last edited by GaKu999 (2020-11-16 14:08:57)


My reposSome snippets

Heisenberg might have been here.

Offline

#5 2020-11-16 14:25:39

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,444
Website

Re: Possible to combine two parameter substitutions in bash [solved]

No, ash definitely does not do that.  The question was about bash, so bashisms are appropriate.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#6 2020-11-16 14:30:30

GaKu999
Member
From: US/Eastern
Registered: 2020-06-21
Posts: 696

Re: Possible to combine two parameter substitutions in bash [solved]

Trilby wrote:

No, ash definitely does not do that.  The question was about bash, so bashisms are appropriate.

less /usr/lib/initcpio/hooks/encrypt line 58:

#!/usr/bin/ash
...
    for cryptopt in ${cryptoptions//,/ }; do
...

I'm going to assume that it can be disabled/enabled in the compile configs of busybox. Correct?
Or you mean the little [v-] trick?

Last edited by GaKu999 (2020-11-16 14:32:05)


My reposSome snippets

Heisenberg might have been here.

Offline

#7 2020-11-16 14:32:52

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,444
Website

Re: Possible to combine two parameter substitutions in bash [solved]

Apparently.  There are many optional features that can be added, but the ${//} or ${///} constructs are not POSIX.  So I should have answered differently as "ash" doesn't refer to a specific build of a shell.

Last edited by Trilby (2020-11-16 14:33:29)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#8 2020-11-16 14:48:38

GaKu999
Member
From: US/Eastern
Registered: 2020-06-21
Posts: 696

Re: Possible to combine two parameter substitutions in bash [solved]

Trilby wrote:

${//} or ${///} constructs are not POSIX.

>.>

...

In any case, the original question was about something like ${${${carfoodlopbarbaz#car}%baz}#dlop}, which is not there, anywhere, of course, there is regex substitution in bash.
Usually when ${var[#|##|%|%%]substring} is not enough I end up with sed or my currently poor awk, but OP already knows about sed & awk...

Last edited by GaKu999 (2020-11-16 14:49:13)


My reposSome snippets

Heisenberg might have been here.

Offline

#9 2020-11-16 15:19:19

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,595
Website

Re: Possible to combine two parameter substitutions in bash [solved]

Trilby wrote:
echo ${_tag//[v-]/}

Gold star!  Can you explain how the shell is reading it?

Last edited by graysky (2020-11-16 15:19:36)


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#10 2020-11-16 15:30:30

GaKu999
Member
From: US/Eastern
Registered: 2020-06-21
Posts: 696

Re: Possible to combine two parameter substitutions in bash [solved]

graysky wrote:

Can you explain how the shell is reading it?

'man bash' covers it, tho it's biiiig.

${parameter/pattern/string}:

Pattern substitution.  The pattern is expanded to produce a pattern just as in pathname expansion, Parameter is expanded  and
the longest match of pattern against its value is replaced with string.  The match is performed using the rules described un‐
der Pattern Matching below.  If pattern begins with /, all matches of pattern are replaced with string.   Normally  only  the
first  match  is  replaced.  If pattern begins with #, it must match at the beginning of the expanded value of parameter.  If
pattern begins with %, it must match at the end of the expanded value of parameter.  If string is null,  matches  of  pattern
are  deleted  and the / following pattern may be omitted.  If the nocasematch shell option is enabled, the match is performed
without regard to the case of alphabetic characters.  If parameter is @ or *, the substitution operation is applied  to  each
positional  parameter in turn, and the expansion is the resultant list.  If parameter is an array variable subscripted with @
or *, the substitution operation is applied to each member of the array in turn, and the expansion is the resultant list.

Or you meant something more specific?
'echo ${_tag//[v-]/}' this could be seen mentally as "from __tag replace v and - with <blank>", or in a char by char basis, matching v or - and replacing with <blank> if match.


My reposSome snippets

Heisenberg might have been here.

Offline

#11 2020-11-16 15:46:02

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,595
Website

Re: Possible to combine two parameter substitutions in bash [solved]

I was trying to understand the double forward slash...

'echo ${_tag/v/}' in my mind means: from _tag replace a 'v' with a null.
'echo ${_tag//[v-]/}: from _tag, replace a 'v' or a '-' with a null.  But I do not understand the doubleslash condition.  I am thinking like a regex but that too isn't right.


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#12 2020-11-16 15:51:50

GaKu999
Member
From: US/Eastern
Registered: 2020-06-21
Posts: 696

Re: Possible to combine two parameter substitutions in bash [solved]

graysky wrote:

I was trying to understand the double forward slash...

'echo ${_tag/v/}' in my mind means: from _tag replace a 'v' with a null.
'echo ${_tag//[v-]/}: from _tag, replace a 'v' or a '-' with a null.  But I do not understand the doubleslash condition.  I am thinking like a regex but that too isn't right.

Oh, that makes more sense, "If pattern begins with /, all matches of pattern are replaced with string. Normally only the first match is replaced."
With only one /, only one v or - is going to be replaced...

Sort-of matches the #|## and %|%% of POSIX.

Last edited by GaKu999 (2020-11-16 15:53:09)


My reposSome snippets

Heisenberg might have been here.

Offline

#13 2020-11-16 15:54:24

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,595
Website

Re: Possible to combine two parameter substitutions in bash [solved]

GaKu999 wrote:

Oh, that makes more sense, "If pattern begins with /, all matches of pattern are replaced with string. Normally only the first match is replaced."
With only one /, only one v or - is going to be replaced...

Sort-of matches the #|## and %|%% of POSIX.

Hmmm... so to match 3 characters include a triple slash?  Doesn't seem to work.  Say you want to replace all of these:  v - 4


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#14 2020-11-16 15:58:56

GaKu999
Member
From: US/Eastern
Registered: 2020-06-21
Posts: 696

Re: Possible to combine two parameter substitutions in bash [solved]

graysky wrote:

Hmmm... so to match 3 characters include a triple slash?  Doesn't seem to work.  Say you want to replace all of these:  v - 4

foo=v5v-1-4-c-4
echo ${foo//[-vc4]/}

You need to be careful where is the number and where is -, or you get a derped regex. tongue

EDIT: changed foo for a more evident //
EDIT2: - is a danger char, breaky breaky, make sure it's the first or last, not in between other chars, since it haves other meanings like [a-z0-9], and [v-4] is...noop.

Last edited by GaKu999 (2020-11-16 16:13:13)


My reposSome snippets

Heisenberg might have been here.

Offline

#15 2020-11-16 16:11:11

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,444
Website

Re: Possible to combine two parameter substitutions in bash [solved]

graysky wrote:

Hmmm... so to match 3 characters include a triple slash?

No, read the manual.  One (initial) slash replaces the first instance of the match, two (initial) slashes replaces every instance of the match.  This has nothing to do with regexs here:

var=foobarfoobarfoobar
echo ${var/foo/baz}
echo ${var//foo/baz}

The regex character class is only used to match a set of characters rather than a specific string.

graysky wrote:

Say you want to replace all of these:  v - 4

Then just make a character class for that:

echo ${var//[v4-]/}

EDIT: fyi, a bazbar is a bowling alley that serves alcohol and has a house jazz band.  EDIT: ok, that joke failed as there is actual a bazbar: apparently it's a bar with live music and sushi.  I like my version better.

EDIT: s/regex/character class/.  Or should I say ${post//regex/character class}.

Last edited by Trilby (2020-11-17 22:21:54)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#16 2020-11-17 11:23:08

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,595
Website

Re: Possible to combine two parameter substitutions in bash [solved]

Thanks guys!


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#17 2020-11-17 20:09:10

eschwartz
Fellow
Registered: 2014-08-08
Posts: 4,097

Re: Possible to combine two parameter substitutions in bash [solved]

This ${parameter/pattern/string} is not even a regex though, it's shell globbing.

"The pattern is expanded to produce a pattern just as in pathname expansion,"

[abc] is interpreted identically in regex(7) and glob(7), but given

foo=bar
echo ${foo/?/}

a regex would print "bar", while bash actually treats it as a glob and prints "ar".


Managing AUR repos The Right Way -- aurpublish (now a standalone tool)

Offline

#18 2020-11-17 22:53:14

GaKu999
Member
From: US/Eastern
Registered: 2020-06-21
Posts: 696

Re: Possible to combine two parameter substitutions in bash [solved]

graysky wrote:

Thanks guys!

No prob, it's nice to have normal genuine questions from time to time. smile

eschwartz wrote:

This ${parameter/pattern/string} is not even a regex though, it's shell globbing.

Hmm? *reads man 7 glob*, oh I see, blame on me, muscle memory usually associates string matches with the word regex.

Also I can type regex with my left hand only, yet glob requires the usage of two hands and takes a couple of microseconds more. tongue
(joking, don't hit me) *runs away from the magical flying rocks*

Last edited by GaKu999 (2020-11-17 22:54:03)


My reposSome snippets

Heisenberg might have been here.

Offline

#19 2020-11-17 23:50:11

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,444
Website

Re: Possible to combine two parameter substitutions in bash [solved]

I never thought a discussion of parameter substitution and shell scripting would be the time at the computer one would need to type one handed.  But hey, whatever does it for ya'.

https://everything2.com/title/Command+Line+Porn


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#20 2020-11-18 00:03:48

GaKu999
Member
From: US/Eastern
Registered: 2020-06-21
Posts: 696

Re: Possible to combine two parameter substitutions in bash [solved]

>.> lol
The visible internet is a "wide" and strange place huh?

...

I still need to grow out of the habit of using the arrow keys, and instead use hjkl. It's so hard! >.>


My reposSome snippets

Heisenberg might have been here.

Offline

Board footer

Powered by FluxBB