Avoiding Aliases, Functions

Problem

You’ve written an alias or function to override a real command, and now you want to execute the real command.

Solution

Use the bash shell’s builtin command to ignore shell functions and aliases to run the actual built-in command.

Use the command command to ignore shell functions and aliases to run the actual external command.

If you only want to avoid alias expansion, but still allow function definitions to be considered, then prefix the command with \ to just prevent alias expansion.

Use the type command (also with -a) to figure out what you’ve got.

Here are some examples:

$ alias echo='echo ~~~'

$ echo test
~~~ test

$ \echo test
test

$ builtin echo test
test

$ type echo
echo is aliased to `echo ~~~'

$ unalias echo

$ type echo
echo is a shell builtin

$ type -a echo
echo is a shell builtin
echo is /bin/echo

$ echo test
test

Here is a function definition that we will discuss:

function cd ()
{
if [[ $1 = "..." ]]
then
builtin cd ../..
else
builtin cd $1
fi
}

Discussion

The alias command is smart enough not to go into an endless loop when you say something like alias ls='ls-a' or alias echo='echo ~~~', so in our first example we need to do nothing special on the righthand side of our alias definition to refer to the actual echo command.

When we have echo defined as an alias, then the type command will tell us not only that this is an alias, but will show us the alias definition. Similarly with function definitions, we would be shown the actual body of the function. ...

Get bash Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.