Shell – Creating an empty file having a variable name in a script

filesquotingshell-script

I need to create an empty file using a shell script. The filename will be determined by examining all the files in a directory matching a pattern, say TEST.*, and appending _END to it so that the new file might look like TEST.2011_END. Let the current directory be $DIR. So I tried:

for file in $DIR/TEST.*
do
    touch $file_END  
done

This gives an error. I also tried replacing touch $file_END with:

filename=$file_END  
touch $filename  

but with no luck. What should I do?

Best Answer

The syntax would be:

filename="${file}_END"

or in your code

touch "${file}_END"

The " quotes are not necessary as long as $file does not have any whitespace or globbing character in it.

Related Question