Any character in front of an alias will prevent the alias from triggering:
alias ls='ls -la'
ls foo.txt #will run ls -la foo.txt
\ls foo.txt #will run ls foo.txt
'ls' foo.txt #will run ls foo.txt
'ls foo.txt' #will run ls foo.txt
However, that doesn't stop functions, and you need command
to reference the underlying builtin if you create a function with the same name.
ls () {
echo "not an alias"
}
alias ls='echo "an alias"'
ls foo.txt #will echo "an alias"
\ls foo.txt #will echo "not an alias"
'ls' foo.txt #will echo "not an alias"
command ls foo.txt #will actually run ls
Explanation
The problem is that the first two options can only deal with aliases. They are not special redirecting operators that can sense if you have a function or command with the same name. All they do is put something in front of the alias, as aliases only expand if they are the first part of a pipe or command
alias v='sudo vim'
v x.txt
#automatically expands "v" to "sudo vim" and then does the rest of the command
# v x.txt ==> sudo vim x.txt
Bash just tries to expand the first word of a command using the list of aliases it knows about (which you can get with alias
). Aliases don't take arguments, and can only be the first word (space-separated; vim x.txt
won't expand into sudo vimim x.txt
using the alias above) in a command to expand properly.
However, expansion never happens to things in single quotes: echo '$USER'
will print out a literal $USER
and not what the variable stands for. Also, bash expands \x
to be x
(mostly). These aren't extra added-in ways specifically to escape an alias, they're just part of how bash expansion was written. Thus, you can use these methods to let an alias shadow a command and still have access to the actual command.
sudo -H
does not work here (I tried it). It sets$HOME
equal to the new user's home, not the home of the user calling sudo. Also, you might like my .vimrc function to display a message if sudo triggered – jeremysprofile Jul 10 '18 at 22:14command vim
as a more generic (PATH
-respecting) approach, rather than/usr/bin/vim
. – Charles Duffy Jul 10 '18 at 23:38