What does ${i%.*} do in this context? [duplicate] - bash

This question already has answers here:
What does the curly-brace syntax ${var%.*} mean?
(3 answers)
Closed 2 years ago.
I'm learning a bit about running a bash script in a linux terminal, specifically in the context of converting audio video files.
I came across this command here on SO that does exactly what I want. However, I'd like to understand it better:
for i in *.avi; do ffmpeg -i "$i" "${i%.*}.mp4"; done
Now, this is obviously a for-loop and I get the first * wildcard. I get the do block. But what I don't quite understand is ${i%.*}. Specifically, what does the %.* bit do in the output location? Why not use ${i}.mp4 instead?

It's called parameter expansion and it removes everything starting from the last dot (ie. extension). Try the following:
$ i="foo.bar.baz"
$ echo ${i%.*}
foo.bar
Author of the original code ("${i%.*}.mp4") apparently wanted to replace original extension with .mp4 so the original extension is removed and .mp4 is appended.

Parameter expansion
${parameter%word}
${parameter%%word}
The word is expanded to produce a pattern and matched according to the rules described below (see Pattern Matching). If the pattern matches a trailing portion of the expanded value of parameter, then the result of the expansion is the value of parameter with the shortest matching pattern (the ‘%’ case) or the longest matching pattern (the ‘%%’ case) deleted.

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.

How do I truncate the last two characters of all files in a directory? [duplicate]

This question already has answers here:
Bash script to remove 'x' amount of characters the end of multiple filenames in a directory?
(3 answers)
Closed 5 years ago.
So pretty simple question. All of the files in my directory are of the form 6bfefb348d746eca288c6d62f6ebec04_0.jpg. I want them to look like 6bfefb348d746eca288c6d62f6ebec04.jpg. Essentially, I want to take off the _0 at the end of every file name. How would I go about doing this with bash?
With Perl's standalone rename command:
rename -n 's/..(\....)$/$1/' *
If everything looks fine, remove -n.
It is possible to use this standalone rename command with a syntax similar to sed's s/regexp/replacement/ command. In regex a . matches one character. \. matches a . and $ matches end of line (here end of filename). ( and ) are special characters in regex to mark a subexpression (here one . and three characters at the end of your filename) which then can be reused with $1. sed uses \1 for first back-reference, rename uses $1.
See: Back-references and Subexpressions with sed

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.

Print the 4th column which contains wild character using shell script [duplicate]

I'm trying to figure out what I thought would be a trivial issue in BASH, but I'm having difficulty finding the correct syntax. I want to loop over an array of values, one of them being an asterisk (*), I do not wish to have any wildcard expansion happening during the process.
WHITELIST_DOMAINS="* *.foo.com *.bar.com"
for domain in $WHITELIST_DOMAINS
do
echo "$domain"
done
I have the above, and I'm trying to get the following output:
*
*.foo.com
*.bar.com
Instead of the above, I get a directory listing on the current directory, followed by *.foo.com and *.bar.com
I know I need some escaping or quoting somewhere.. the early morning haze is still thick on my brain.
I've reviewed these questions:
How to escape wildcard expansion in a variable in bash?
Stop shell wildcard character expansion?
Your problem is that you want an array, but you wrote a single string that contains the elements with spaces between them. Use an array instead.
WHITELIST_DOMAINS=('*' '*.foo.com' '*.bar.com')
Always use double quotes around variable substitutions (i.e. "$foo"), otherwise the shell splits the the value of the variable into separate words and treats each word as a filename wildcard pattern. The same goes for command substitution: "$(somecommand)". For an array variable, use "${array[#]}" to expand to the list of the elements of the array.
for domain in "${WHITELIST_DOMAINS[#]}"
do
echo "$domain"
done
For more information, see the bash FAQ about arrays.
You can use array to store them:
array=('*' '*.foo.com' '*.bar.com')
for i in "${array[#]}"
do
echo "$i"
done

why we use ##*/ expression with bash variable [duplicate]

This question already has answers here:
explain the linux regex for getting filename
(2 answers)
Closed 8 years ago.
I am tring to understand the bash script.
I am seeing ##* / expression with bash variable.
i.e ${foo##*/}
Can someone please tell me why we use that expression?
It's called "Parameter expansion". The variable $foo is searched for a substring matching the pattern */ (i.e. anything up to a slash) from the beginning (#), and what remains in the variable is returned. Doubling the #-sign makes the matching greedy, i.e. it tries to find the longest possible match.

Resources