1

I have an alias rm='/bin/rm -i' and I know that if I type "rm" filename in the command line, the alias will be ignored somehow and the normal rm command without the -i flag will be called, but I don't understand why this works.

I don't understand why "rm" works at all, considering...

Enclosing characters in double quotes (‘"’) preserves the literal value of all characters within the quotes, with the exception of ‘$’, ‘`’, ‘\’, and, when history expansion is enabled, ‘!’. - [gnu.org][1]

... but it does work somehow. And it ignores my alias. How does this work?

Furthermore, I see these alias-ignoring command invocations in my organization's Korn shell scripts sometimes. But that's unnecessary, right? Because Korn shell scripts don't have access to shell-level aliases anyway?

[1]: https://www.gnu.org/software/bash/manual/html_node/Double-Quotes.html#:~:text=Enclosing%20characters%20in%20double%20quotes,POSIX%20Mode)%2C%20the%20%27%20!

1
  • I don't have a solid answer. I suspect the reason is encoded within the syntax parser. Commented Nov 4, 2022 at 22:43

1 Answer 1

1

This is a very specific case: see

https://www.gnu.org/software/bash/manual/bash.html#Aliases

The first word of each simple command, if unquoted, is checked to see if it has an alias. If so, that word is replaced by the text of the alias.

That section of the manual notes several confusing aspects of aliases, and ends with:

For almost every purpose, shell functions are preferred over aliases.

2
  • 1
    Also, according to the POSIX standard for shell command processing, section 2.3.1 Alias Substitution (emphasis added): "After a token has been delimited, but before applying the grammatical rules in Shell Grammar, a resulting word that is identified to be the command name word of a simple command shall be examined to determine whether it is an unquoted, valid alias name." Commented Nov 5, 2022 at 1:49
  • Thank you, both! Commented Nov 7, 2022 at 21:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .