I have a bash script that renames certain text files according to their content. A file with the following content e.g. should be renamed to 2020-05-11_Chess-com.txt
.
[Site "Chess.com"]
[Date "2020-05-11"]
[White "Computer 6"]
[Result "0-1"]
[Termination " won by checkmate"]
... More content ...
Below you find a mockup of the script I am currently using:
#!/bin/bash
site=$(sed -n '1p' < "$1" | cut -d ' ' -f2 | tr -d '"]' | tr ' .' '-')
datum=$(sed -n '2s/[^0-9-]*\([0-9-]*\)[^0-9-]*/\1/p' < "$1")
echo "${datum}_${site}" # 2020-05-11_Chess-com
echo "${datum}_${site}".txt # .txt-05-11_Chess-com
echo "$datum"_"${site}".txt # .txt-05-11_Chess-com
The result of the string substitution in the last two lines is totally unexpected for me and I find it difficult to find any explanation for the script's odd behavior.
One observation I made however, although I did not help much, was that if I substitute the line site=$(...)
with site="Chess-com"
, then I get the expected result 2020-05-11_Chess-com.txt
.