0

I have created a test .rtf file and a test folder to delete from my desktop in Mac OS

Test and Stuff.rtf file

Test and Things folder

Code:

#!/bin/bash

clear

echo Looking for leftovers

echo 

STR='~/Desktop/Test and Things';

STR1="~/Desktop/Test and Stuff.rtf";

if [[ -e "$STR" ]]; then
    rm -r "$STR";
    echo "File $STR found and removed";
else echo "$STR not found!";

fi

echo 

if [[ -e $STR1 ]]; then
    rm $STR1;
    echo "File $STR1 found and removed";
else echo "$STR1 not found!"; 

fi

I always end up with

~/Desktop/Test and Things not found!

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 1
    The spaces aren't the problem, the `~`s are the problem. Use `$HOME` instead. – Charles Duffy Mar 27 '20 at 18:47
  • 1
    That said, you *will* have problems elsewhere; `rm $STR1` needs to be `rm "$STR1"`. Run your code through http://shellcheck.net/ – Charles Duffy Mar 27 '20 at 18:48
  • 1
    (also, note that POSIX-specified convention is for upper-case names to be used for variables *meaningful to the shell itself*; use lowercase names for your own variables so they don't conflict). – Charles Duffy Mar 27 '20 at 18:48
  • I was using ' and " in multiple spots to test different things that I had read. Gonna give it a shot. Thanks for the help guys – pbcompgeek Mar 27 '20 at 18:50
  • You should replace `str='$HOME/Desktop/Test and Things';` with `str="$HOME/Desktop/Test and Things"` because otherwise, the single-quotes prevent parameter expansion to occur. – ErikMD Mar 27 '20 at 18:58
  • The above edit code works now with str="$HOME/Desktop/Test and Stuff.rtf" Thanks for the help guys!!! – pbcompgeek Mar 27 '20 at 18:59
  • Is there an environment variable for Library on Mac OS? – pbcompgeek Mar 27 '20 at 19:11

0 Answers0