I'm trying to write a bash script that will look for all .txt files in a folder, grab the basename and then relocate the file. However, I'm having an issue where any time I try to combine the filename with a string, it outputs the "*.txt" wildcard from the for loop instead of the actual file it found. If I simply echo the variable, it outputs the expected filename.
I've included a simplified script below and the output it produces. I'm new to BASH and I assume this has something to do with the way it handles variables. I've been searching for an explanation for a few hours, but I haven't really come up with anything. Can anyone explain why the value changes when concatenated?
Here is the example code:
TEMP_PATH=C/Test
for testFile in $TEMP_PATH/*.txt; do
testFileName=$(basename "$testFile")
echo $testFileName
echo "FileName:$testFileName"
done
Here is the example output from each echo
TestOne.txt
FileName:*.txt
Here are the files in the directory. TestingToo.sh is the script shown above
TestingToo.sh TestOne.txt
Any help would be appreciated! Thank you.
TestOne.txt FileName:TestOne.txtas expectedtestFileset toC/Test/*.txt. Next command will settestFileNameto*.txt. Then, in the first echo command bash will substitute*.txtinto set of matching files, that is:TestOne.txtin your current directory. But in the second echo comand there will be no file matchingFileName:*.txtpattern. So bash will leave it as-is.echo C/Test/*.txtcommand?