This is a simple script I wrote for a class, where we're suppose to "fake delete" files by moving them to $HOME/trashbin
. I'm trying to make it compatible with file names containing spaces, however nothing seems to work.
Even when used with fake_erase.sh "file name.txt"
, if I do echo $#
it says 2
and tries to parse both bits of the input separately. Is there anything I can do so that it works properly ?
rm "file name.txt"
has no problem with the file having a space in it if it's in quotes ! (though it's likely not written in bash)
Here is the entire code :
#!/usr/bin/env bash
trashbin="$HOME/trashbin"
function usage() {
echo "usage : $0 file ..."
echo "moves files to $trashbin"
echo "creates $trashbin if it doesn't exist"
exit -1
}
# Error if no arguments
if (( $# < 1 )); then
usage
fi
# Create directory if it doesn't exist
if [[ ! -d "$trashbin" ]]; then
mkdir "$trashbin"
fi
for i in $@; do
name="$( basename $i )"
mv $i "$trashbin/$name"
done