What does expanding a variable as "${var%%r*}" mean in bash? [duplicate] - bash

This question already has an answer here:
Bash: manipulating with strings (percent sign)
(1 answer)
Closed 6 years ago.
I've got the following variable set in bash:
ver=$(/usr/lib/virtualbox/VBoxManage -v | tail -1)
then I have the following variable which I do not quite understand:
pkg_ver="${ver%%r*}"
Could anyone elaborate on what this does, and how pkg_ver is related to the original ver value?

It is a bash parameter expansion syntax to extract text from end of string upto first occurrence of r
name="Ivory"
printf "%s\n" "${name%%r*}"
Ivo
${PARAMETER%%PATTERN}
This form is to remove the described pattern trying to match it from the end of the string. The operator "%" will try to remove the shortest text matching the pattern, while "%%" tries to do it with the longest text matching.

You will get everything from variable ver until first "r" character and it will be stored inside pkg_ver.
export ver=aaarrr
echo "${ver%%r*}"
aaa

Related

What does the #*$ in a shell script's string interpolation do? [duplicate]

This question already has answers here:
What is the meaning of the ${0##...} syntax with variable, braces and hash character in bash?
(4 answers)
Closed 11 months ago.
I found the following code in a shell script, but I am unsure of what the test condition is evaluating for
if test "${SOME_VAR#*$from asdf/qwer}" != "$SOME_VAR"; then
echo "##zxcv[message text='some text.' status='NORMAL']";
fi
The combination #*$ does not mean anything special. There are three special symbols and each of them has its own meaning in this context.
${SOME_VAR#abc} is a parameter expansion. Its value is the value of $SOME_VAR with the shortest prefix that matches abc removed.
In your example, abc is *${from} asdf/qwer. That means anything * followed by the value of variable $from (which is dynamic and replaced when the expression is evaluated), followed by a space and followed by asdf/qwer.
All in all, if the value of $SOME_VAR starts with a string that ends in ${from} asdf/qwer then everything before and including asdf/qwer is removed and the resulting value is passed as the first argument to test.
Type man bash in your terminal to read the documentation of bash or read it online.

BASH remove specific tokens from a word [duplicate]

This question already has answers here:
Command not found error in Bash variable assignment
(5 answers)
Closed 5 years ago.
I am trying to find te longest word in a given file. Before I check the lengtgh of each word I need to remove all of the following tokens {,.:} that may be attached (once or more) to the word. so for example, for this text:
:,cat dog, encyclopedia; remove:.,
i need the result:
cat dog encyclopedia remove
I am trying this, but I get a "command not found":
longest=0
for word in $(<$1)
do
#new_word = $(echo "${word//[.,:]/}")
new_word = "${word//[.,:]/}"
len=${#new_word}
if (( len > longest ))
then
longest=$len
longword=$new_word
fi
done
echo The longest word is $longword and its length is $longest.
thank you.
Your use of parameter expansion replacement pattern is correct.
The problem is that there must not be any whitespace around = while declaring variables in bash (any shell in general).
So, the following should work:
new_word="${word//[.,:]/}"
As an aside, use a while read ... construct to loop over the lines in a file, using for is pretty fragile.

UNIX Replace string without replacing space [duplicate]

This question already has answers here:
I just assigned a variable, but echo $variable shows something else
(7 answers)
Closed 5 years ago.
For string matching purposes I need to define a bash variable with leading spaces.
I need to define this starting from an integer, like:
jj=5
printf seems to me a good idea, so if I want to fill spaces up to 6 character:
jpat=`printf " %6i" $jj`
but unluckly when I am trying to recall the variable:
echo $jpat
the leading whitespaces are removed and I only get the $jj integer as it was.
Any solution to keep such spaces?
(This is equivalent to this: v=' val'; echo $v$v. Why aren't there leading and multiple spaces in output?)
Use More Quotes! echo "$jpat" will do what you want.
There is another issue with what you're doing: Command substitutions will remove trailing newlines. It's not an issue in the printf command you're using, but for example assigning jpat=$(printf " %6i\n" "$jj") would give you exactly the same result as your command.

What is a `##` means in bash variable substitution `${}`? [duplicate]

This question already has answers here:
What is the meaning of the ${0##...} syntax with variable, braces and hash character in bash?
(4 answers)
Closed 2 years ago.
I am learning the bash script materials on http://www.tldp.org/LDP/abs/html/index.html
and stuck in the Example 7-7:
http://tldp.org/LDP/abs/html/comparison-ops.html#EX14
There is an ${filename##*.} != "gz", this probably means
that the $filename does not end with .gz, but I do not
know the meaning of ## here. Could anyone help me?
Thanks!
Used in a variable expansion, ${string##sub} removes the longest matching substring sub from string (# removes the shortest matching substring by contrast).
In your case, yes - this will return the string after the first . from the filename, giving the file extension.
If you search for ## in this documentation, you'll find an explanation (along with other similar commands).
In the context of filenames, is trying to find the extension in the variable filename
filename="*.log"
echo ${filename##*.}
log
We are attaining the part of the string filename after "*."
## is a used for to remove a substring from a variable. For more info check this page.
For eg. if filename=/home/user.name/folder.1/test.gz, then ${filename##*.} will give you output as gz.

Appending text to the end of a variable [duplicate]

This question already has answers here:
How to concatenate string variables in Bash
(30 answers)
Closed 8 years ago.
The following works, but I don't want the space that it returns:
read input
file= "$input"
file= "$file ins.b" # how to get rid of the space here?
echo "$file"
This outputs 'file ins.b'
I don't want the space between file and ins.b
If I don't leave that space in the code it returns only '.b'. What can I do to resolve this problem?
Append like:
file="${file}ins.b"
If you don't use braces then it treats fileins as a variable and expands it. Since, it's probably not set it just prints .b.
Related: When do we need curly braces in variables using Bash?
In bash you can also reference variables like ${file}. So this should work for you:
file="${file}ins.b"
You don't need to expand the old value at all; bash has a += operator:
file+="ins.b"
file="${file}ins.b"
or
file=$file"ins.b"

Resources