Bash script: check if a file is a text file
From the fact that it says file $attachment
rather than file "$attachment"
,
I guess your script cannot handle filenames that contain spaces.
But, be advised that filenames can contain spaces,
and well-written scripts can handle them. Note, then: