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. type -a some_command will show us all of the places (aliases, built-ins, functions, and external) that contain some_command (as long as you are not also using -p).

In our last example, the function overrides the definition of cd so that we can add a simple shortcut. We want our function to understand that cd… means to go up two directories; i.e., cd ../.. (see Creating a Better cd Command). All other arguments will be treated as normal. Our function simply looks for a match with … and substitutes the real meaning. But how, within (or without) the function, do you invoke the underlying cd command so as to actually change directories? The builtin command tells bash to assume that the command that follows is a shell built-in command and not to use any alias or function definition. We use it within the function, but it can be used at any time to refer, unambiguously, to the actual command, avoiding any function name that might be overriding it.

If your function name was that of an executable, like ls, and not a built-in command, then you can override any alias and/or function definition by just referring to the full path to the executable, such as /bin/ls rather than just ls as the command. If you don’t know its full path name, just prefix the command with the keyword command and bash will ignore any alias and function definitions with that name and use the actual command. Please note, however, that the $PATH variable will still be used to determine the location of the command. If you are running the wrong ls because your $PATH has some unexpected values, adding a command will not help in that situation.

See Also

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.165.70