Home > Mobile >  Bash Script variable assigned "-r" when using rm -r $VAR
Bash Script variable assigned "-r" when using rm -r $VAR

Time:06-21

Heres the code:

function rm {

    cd ~/

    if [[ -d ./Jam/projects/"$1" ]]; then
        echo Removing $1 from projects...
        rm -r ./Jam/projects/"$1"
    elif [[ -d ./Jam/archive/"$1" ]]; then
        echo Removing $1 from archives...
        rm -r ./Jam/archive/"$1"
    else
        echo $1 does not exist \in ./Jam/projects/ or ./Jam/archive
        exit
    fi

    echo Finished\!
}

When this is ran, $1 is "Hello World" (a Directory in i./Jam/archive/)

I get this output:

Removing HelloWorld from archives...
-r does not exist in ./Jam/projects/ or ./Jam/archive

Somehow, $1 is assigned to "-r".

I don't know how on earth this would happen. Any help is much appreciated.

CodePudding user response:

Your function is called "rm" and inside your function "rm" you call rm -r thinking it's "normal rm", but it isn't - it's your function, which perfectly demonstrates the danger of calling your function a name that already has a well known meaning.

  •  Tags:  
  • bash
  • Related