I just opened a legacy shell script (written in old ksh88 on Solaris) and found the following repeated all throughout the code:

[ -f $myfile ] && \rm -f $myfile

The escaping backslash strikes me as odd.

I know it is deliberate, since this kind of (apparently useless) escaping is repeated all throughout the code. The original author is long gone, I cannot get in touch with him to ask him.

Is this simply a funny idiosyncrasy of the author or is it some sort of deprecated good practice that made sense at some point in time? Or maybe is actually the recommended way of doing things and I'm missing something altogether?

  • 3
    Although there is a good reason for it, providing alias protection in a script using this method is not what I'd call "recommended." It would be sufficient to clear the alias at the top of the script, or to invoke rm by full path. – Sorpigal Apr 5 '12 at 13:25

This is used for alias protection:

$ ls
.bashrc  a  b

$ alias ls
alias ls='ls $LS_OPTIONS'

$ \ls
a   b

It is generally good practice to throw some safeguards in for rm, which is typically achieved by aliasing. In multiuser environments, you'll often see many of these protections in place.

For a shell scripting practitioner, it is often useful for one to disable these safeguards as presumably, they know what they are doing. This, as mentioned, is accomplished by prefacing the command with a \.

Contrary to @Sorpigal's suggestion, I would definitely advise against unsetting the aliases, lest the script fails to give the user back their safeguards. Further, using a full path is also unwise as rm may be in an auxiliary path for a reason - i.e. GNU rm vs. BSD rm. To override it with a strict path would be to defeat the purpose of having PATHs, namely, to scale and handle many architectures, environments and users.

  • 3
    Despite being common, aliasing rm is not a good but quite a poor and unfortunate practice. – jlliagre Apr 9 '12 at 17:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.