You are not logged in.
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 packages • Zsh and other configs
Offline
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
https://stackoverflow.com/questions/295 … osix-shell
https://unix.stackexchange.com/question … sh-and-zsh
Offline
echo ${_tag//[v-]/}
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
echo ${_tag//[v-]/}
Well, that is regex that works for this simple test case.
...
Darnit that is in ash and not in dash! o_O
Last edited by GaKu999 (2020-11-16 14:08:57)
Offline
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
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)
Offline
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
${//} 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)
Offline
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 packages • Zsh and other configs
Offline
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.
Offline
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 packages • Zsh and other configs
Offline
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)
Offline
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 packages • Zsh and other configs
Offline
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.
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)
Offline
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.
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
Thanks guys!
CPU-optimized Linux-ck packages @ Repo-ck • AUR packages • Zsh and other configs
Offline
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
Thanks guys!
No prob, it's nice to have normal genuine questions from time to time.
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.
(joking, don't hit me) *runs away from the magical flying rocks*
Last edited by GaKu999 (2020-11-17 22:54:03)
Offline
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'.
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
>.>
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! >.>
Offline