Bash: Assigning a substring to a variable
I've been learning bash scripting, but unfortunately it's more out of necessity than taking my time and learning the meaning of all the syntax really well. I've run into a problem that I can't seem to figure out from any documentation - so if anyone can shed some light on this I'd be appreciative.
I have a loop where I need to loop through all the files, and do several different things with the part of the filename before the extension. The first 'echo' statement below outputs exactly what I need, however since I do several different actions with that output, I really need it to be stored in it's own variable.
Code:
for file in `ls *.txt `
do
# outputs the desired result, but I need it in a variable
echo `expr "$file" : '\([^.]\+\)'`
# this either tries to execute the result or yields a '0'
let s=`expr "$file" : '\([^.]\+\)'`
echo $s
done
My first thought was the 'let' and the second echo, but that results in a '0'. I've also tried wrapping the expression in a $( ... ) but that yields '0' if I remove the back ticks, and tries to execute the result as a command if I leave the backticks. I also initially thought that having slashes in the file path was causing it to be evaluated differently, but I modified the program so there are no longer any slashes in the path - just the filename.
Any thoughts?