LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Use of "\" (backslash) in commands (http://www.linuxquestions.org/questions/linux-newbie-8/use-of-%5C-backslash-in-commands-4175427927/)

 shivaa 09-18-2012 11:40 PM

Use of "\" (backslash) in commands

Hello Friends,
Could anyone explain the use of "\" (backward slash) with "rm" command.
I've seen, that many people use following command when delete a file or directory:
example% \rm -rf <directory>
So why thay use fwd slash before rm?
Thanks a lot.

 TobiSGD 09-18-2012 11:59 PM

I have never seen that. The backslash is an escape character, which is used to give characters to commands that would normally have a different meaning in the shell.
For example, if you have a filename with a space in it, for example "aaa bbb" you could not simply remove the file with
Code:

rm aaa bbb
because rm would try to remove two files, aaa and bbb, but not the file aaa bbb. To circumvent that you can either quote the filename
Code:

rm "aaa bbb"
or you escape the space in the filename
Code:

rm aaa\ bbb
EDIT: Thanks to evo2 and suicidaleggroll, I didn't know that one could escape an alias.

 evo2 09-19-2012 12:03 AM

Hi,

it's to avoid shell aliases and the like.

Try the following.
Code:

alias rm='rm -i' touch foo bar rm foo \rm bar
when you rm foo, you should be asked to confirm (since the alias has -i), but when you \rm bar the alias is not used.

Cheers,

Evo2.

 suicidaleggroll 09-19-2012 12:06 AM

Quote:
 Originally Posted by evo2 (Post 4783856) Hi, it's to avoid shell aliases and the like. Try the following. Code: alias rm='rm -i' touch foo bar rm foo \rm bar when you rm foo, you should be asked to confirm (since the alias has -i), but when you \rm bar the alias is not used. Cheers, Evo2.
^ this

The leading backslash bypasses any aliases the user has set up for the command. For example, I have an alias for df, so whenever I run "df" it actually runs "df -h", just so that I don't always have to type the -h. However, if I ever want to run the df without the -h, for whatever reason, I need to run "\df" so it runs the regular version without my custom alias.

 suicidaleggroll 09-19-2012 12:07 AM

accidental double post

 shivaa 09-19-2012 02:04 AM

Thanks

Quote:
 Originally Posted by evo2 (Post 4783856) Hi, it's to avoid shell aliases and the like. Try the following. Code: alias rm='rm -i' touch foo bar rm foo \rm bar when you rm foo, you should be asked to confirm (since the alias has -i), but when you \rm bar the alias is not used. Cheers, Evo2.