1

When I print a variable with one substring replacing another as described in this documentation, it behaves correctly:

stringZ='abc - 123'

echo $stringZ             # abc - 123

echo ${stringZ/ - /test}  # abctest123

However, if I get the string from a filename, the replacement is ignored completely:

for f in mp3/*; do

    stringZ=$(basename $f)

    echo $stringZ             # abc - 123.txt

    echo ${stringZ/ - /test}  # abc - 123.txt

done

How can I replace a substring in a variable derived from a filename?

5
  • 4
    Perhaps the dash you see is not the dash you wrote?
    – Jeff Schaller
    Commented Jun 12, 2019 at 19:56
  • 1
    try echo ... | hexdump -C or mp3/*' - '* to see if file matches pattern at all Commented Jun 12, 2019 at 19:59
  • You should be getting some error messages there. Aren't you?
    – terdon
    Commented Jun 12, 2019 at 20:09
  • I'm not seeing any error messages. Is there way to make it more verbose?
    – Keyslinger
    Commented Jun 12, 2019 at 20:12
  • echo 'abc - 123.txt' | hexdump -C and echo $stringZ | hexdump -C both output 00000000 61 62 63 20 2d 20 31 32 33 2e 74 78 74 0a
    – Keyslinger
    Commented Jun 12, 2019 at 20:13

1 Answer 1

5

The answer is, as usual, that you need to always quote your variables:

$ ls -N mp3/
abc - 123.txt

Now, let's try your loop:

$ for f in mp3/*; do 
    stringZ=$(basename $f); 
    echo $stringZ; 
    echo ${stringZ/ - /test} ; 
done
basename: extra operand ‘123.txt’
Try 'basename --help' for more information.

What happens is that the $f variable has the value mp3/abc - 123.txt, so you are running basename mp3/abc - 123.txt and that complains since it sees the space and assumes that mp3/abc is the first argument. It should work as expected if you quote:

$ for f in mp3/*; do 
    stringZ=$(basename "$f"); 
    echo "$stringZ"; 
    echo "${stringZ/ - /test}" ; 
done
abc - 123.txt
abctest123.txt

Only the quotes in the basename "$f" are absolutely necessary here, but always quoting your variables is a good habit to get into.

3
  • Also related: unix.stackexchange.com/questions/68694/…
    – Kusalananda
    Commented Jun 12, 2019 at 20:17
  • good catch! I subconsciously quoted it when testing earlier. haha. (it could also be a typo in the question, as it had the echo and claimed the output was correct) Commented Jun 12, 2019 at 20:31
  • Thank you so much, you saved my day!
    – Keyslinger
    Commented Jun 12, 2019 at 20:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .