WK_ARTIST="$(echo "$ARTIST" | sed 's/ /_/g' )"
WK_ALBUM="$(echo "$ALBUM" | sed 's/ /_/g' )"
echo "$WK_ARTIST"
echo "$WK_ALBUM"
wget "https://en.wikipedia.org/wiki/File:${WK_ARTIST}_-_$WK_ALBUM.jpg"
Shell variables should be quoted whenever you use them (including inside command substitution). The sole exception is when you know with absolute certainty that you actually want the variable's value to be subject to shell's whitespace splitting.
$WK_ARTIST
needs to be disambiguated from the _
by surrounding it with curly braces ({}
) so that the shell doesn't interpret the variable name as $WK_ARTIST_
(which probably does not exist so evaluates as the empty string).
This is necessary for WK_ARTIST but not later in the string for WK_ALBUM because _
is a valid character in a shell variable name (just like A-Z, a-z. and 0-9 for characters after the first), while .
is not.
i.e. FOO
, FOO_
, FOO1
, FOO_1
, and FOOBAR
are all valid and completely distinct variable names.
Or, using bash's built-in search & replace feature instead of echo ... | sed
:
WK_ARTIST="${WK_ARTIST// /_}"
WK_ALBUM="${WK_ALBUM// /_}"
echo "$WK_ARTIST"
echo "$WK_ALBUM"
wget "https://en.wikipedia.org/wiki/File:${WK_ARTIST}-$WK_ALBUM.jpg"
Or, if you're going to use the URL multiple times (or just to make the script easier to read), it's worth doing something like:
...
URL="https://en.wikipedia.org/wiki/File:${WK_ARTIST}_-_$WK_ALBUM.jpg"
...
wget "$URL"
echo "ARTIST=$ARTIST, ALBUM=$ALBUM"
and its result to your question – Chris Davies Jun 10 '21 at 10:21$ARTIST_
here instead of$ARTIST
. – terdon Jun 10 '21 at 10:39wget
bit anyway). However, it would be good to know what exactly is in those variables and where that data comes from. It looks as if theARTIST
variable may possible contain junk at the start. – Kusalananda Jun 16 '21 at 09:33set -x
into your script or running it likebash -x yourscript
you should be able to debug similar problems. – Bodo Jul 13 '21 at 17:26