Issue
How to understand the different output between file2 and file3?
(base) ➜ Youtube cat 1.sh
base="/VS/Harry Potter - Hedwig's Theme (Harmonica Ver.) [w2kuM-Rp_IY].info.json"
echo "base: $base";
dir=$(dirname "$base");
filename=$(basename "$base");
file=${${filename%.*}%.*};
file2=${${"$(basename $filename)"%.*}%.*};
file3=${${$(basename $filename)%.*}%.*};
echo "file: $dir/$file";
echo "file2: $dir/$file2";
echo "file3: $dir/$file3";
(base) ➜ Youtube zsh 1.sh
base: /VS/Harry Potter - Hedwig's Theme (Harmonica Ver.) [w2kuM-Rp_IY].info.json
file: /VS/Harry Potter - Hedwig's Theme (Harmonica Ver.) [w2kuM-Rp_IY]
file2: /VS/Harry Potter - Hedwig's Theme (Harmonica Ver.) [w2kuM-Rp_IY]
file3: /VS/Harry Potter - Hedwig's Theme (Harmonica Ver [w2kuM-Rp_IY]
It seems basename command returns a serval part split by space and ${var%.*}
command works on each part.
Solution
It seems basename command returns a serval part split by space and
${var%.*}
command works on each part.
Pretty much, yes.
From the documentation on command substitution:
If the substitution is not enclosed in double quotes, the output is broken into words using the
IFS
parameter.
And from the section on parameter expansion talking about pattern deletion and substitutions:
... when name is an array and the substitution is not quoted, or if the
(@)
flag or thename[@]
syntax is used, matching and replacement is performed on each array element separately.
So that's the situation you're running into with the unquoted command substitution. Its output is broken up into words and each word is checked for a matching pattern to delete.
Answered By - Shawn Answer Checked By - Timothy Miller (WPSolving Admin)