0

I have a bash script containing this code:

path=/apps/files/
ext=*.csv

rm -f $path$ext

This is not working. When I set -o to echo each command I see this in the output...

rm -f '*.csv'

So it's not getting the path, but I use that path variable throughout the script and it works. The only difference would be that I use it with specific file names and with commands other than rm. So something like...

file=abc.csv
echo "some text" >> $path$file

... will work perfectly.

Any help would be appreciated.

1
  • 1
    It might help to post the complete script (or at least a more substantial portion of it) Sep 20, 2018 at 22:46

1 Answer 1

0

By not quoting the assignment

ext=*.csv

You will set ext to "*.csv'" only if there are NO.csvfiles in the current directory. If the current directory has*.csvfilesext` will get a list of them.

For example, I have 18 .csv files in my home directory, and none in ~/bin.

walt@bat:~(0)$ cd ~/bin
walt@bat:~/bin(0)$ ext=*.csv
walt@bat:~/bin(0)$ echo $ext
*.csv
walt@bat:~/bin(0)$ cd
walt@bat:~(0)$ ext=*.csv
walt@bat:~(0)$ echo $ext
abook15.csv abook62.csv abook8.csv abook.csv accountactivity.csv Address book headers.csv addresses.csv allphones.csv assets.csv bbook.csv Jchq-2013-08-01-2014-07-31.csv Jchq.sort.csv PCF2016Aug04.csv portfolios.csv rickdisk.csv test.csv testmoo1.csv testmoo2.csv

Clearly, "$path/$ext" will look funny.

To preclude this filename interpolation, quote your assignment:

ext='*.csv'
2
  • In both cases, the expansion of $ext occurs during the echo not during the assignment though, surely? (compare with echo "$ext" for example) Sep 20, 2018 at 19:30
  • Thanks for the advice, but I tried this solution and it still didn't work. I also tried rm -f "${path}"$ext which didn't work either. When this script first runs there shouldn't be any .csv files in the directory, the script creates the files, does stuff with them, and then I would like to delete them. That last part is where I'm having trouble.
    – user873631
    Sep 20, 2018 at 21:00

You must log in to answer this question.

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