Bash – Override Commands
Bash – Override Commands : In this Bash Tutorial, we shall learn to override an inbuilt command using Bash Functions.
We may override commands in bash by creating a function with the same name as the command we would like to override. For example to override ps command, you have to create a function with name ps .
This could be helpful in scenarios like you use a command with certain options, and you do not like to provide the whole command with options several times in your script. So you may override the command for command with options. Now let us learn how overriding commands could be done in Bash Shell Scripting.
How Bash Resolves an Overridden Command Name
Before overriding a command in Bash, it is useful to know what happens when you type a command name. Bash checks aliases, keywords, functions, builtins, and executable files found through PATH. A shell function with the same name as an external command can therefore intercept the command name and run your custom logic first.
For example, if you define a function named ls, then typing ls inside that shell session runs the function. Inside the function, use command ls to call the real external ls command without recursively calling the function again.
command command_name arguments
For Bash builtins such as echo, cd, or printf, use builtin when you specifically want to call the shell builtin from inside an overridden function.
builtin builtin_name arguments
Example – Bash – Override Command
In this example, we shall try overriding ls command for ls -ltr .
#!/bin/bash
# override 'ls' command for 'ls -ltr'
ls () {
command ls -ltr
}
ls
$ ./bash-override-command
total 8
-rwxrwxrwx 1 arjun arjun 82 Nov 21 16:41 bash-function
-rwxrwxrwx 1 arjun arjun 82 Nov 21 16:41 bash-override-command
In the above script, ls is now a Bash function. If the function body used plain ls -ltr, Bash would call the same function again and again. The keyword command avoids that recursion and invokes the actual command available through PATH.
Example – 2 – Bash – Override Command
In this example, we shall try overriding echo command, and add time stamp in from of the argument to echo command.
#!/bin/bash
# override 'ls' command for 'ls -ltr'
echo () {
builtin echo -n `date +"[%m-%d %H:%M:%S]"` ": "
builtin echo $1
}
echo "Maverick Reporting. Over."
$ ./bash-override-command-echo
[11-21 17:10:14] : Maverick Reporting. Over.
Here, echo is a Bash builtin. Therefore, the function uses builtin echo to call the original builtin implementation. This is different from command ls, which is normally used to bypass a function and run an external command.
Safer Bash Function Override with All Arguments Preserved
When overriding a Bash command, preserve all command-line arguments unless you intentionally want to support only one argument. In Bash, "$@" passes all received arguments exactly as separate arguments. This is safer than using $1 when the command may receive multiple values or values with spaces.
#!/bin/bash
# Override echo and preserve all arguments
echo() {
builtin printf '[%s] : ' "$(date '+%m-%d %H:%M:%S')"
builtin echo "$@"
}
echo "Build status" "completed"
[11-21 17:10:14] : Build status completed
The same approach can be used for external commands. Add your default options first and then pass "$@" so the user can still provide extra arguments.
#!/bin/bash
# Always use long listing, but still accept extra ls arguments
ls() {
command ls -ltr "$@"
}
ls /tmp
Bypass a Bash Function Override Temporarily
Sometimes you may want to keep the override, but run the original command for one command line. Bash provides a few common ways to bypass a function or alias depending on what has been overridden.
| Requirement | Command to use | Example |
|---|---|---|
| Run external command instead of a function | command | command ls -l |
| Run a shell builtin instead of a function | builtin | builtin echo "hello" |
| Bypass an alias in an interactive shell | Backslash before command | \ls |
| Show how Bash resolves a name | type | type ls |
The type command is helpful while debugging because it tells you whether a name is currently an alias, function, builtin, keyword, or external command.
type ls
type echo
ls is a function
echo is a function
Remove an Overridden Bash Command Function
If you created a command override in the current shell session and want to remove it, use unset -f with the function name. After removing the function, Bash again resolves the command name normally.
unset -f ls
If the override is stored in .bashrc, .bash_profile, or another sourced script, remove or comment that function definition from the file too. Otherwise, the override will return when a new shell session starts.
Function Override vs Alias for Bash Commands
For simple shortcuts, an alias may be enough. For example, alias ll='ls -ltr' is easier than overriding ls. A Bash function is better when you need logic, conditions, multiple commands, argument handling, or a wrapper around a builtin command.
| Use case | Better choice | Reason |
|---|---|---|
| Simple default options | Alias | Short and readable |
| Run several commands | Function | Can contain multiple statements |
| Handle arguments safely | Function | Can use "$@" and conditions |
| Wrap a Bash builtin | Function | Can call original builtin with builtin |
In scripts, prefer functions with clear custom names when possible, such as list_recent_files instead of overriding ls. Overriding common commands can make scripts harder to read for someone who expects the standard command behavior.
Common Mistakes When Overriding Bash Commands
- Calling the same function recursively: Inside
ls(), plainlscalls the function again. Usecommand ls. - Using
$1when all arguments are needed: Use"$@"to forward every argument safely. - Overriding commands globally without noticing: A function in
.bashrcaffects every interactive shell started after that file is sourced. - Using
commandfor builtins whenbuiltinis clearer: For builtin commands,builtin echoorbuiltin printfclearly states the intention. - Changing expected command behavior in shared scripts: Use descriptive function names in scripts that other users will maintain.
FAQ on Overriding Commands in Bash
Can a Bash function override a system command?
Yes. A Bash function can have the same name as a system command. When the name is entered, Bash can run the function before the external command. Use command command_name inside the function to call the original external command.
How do I call the original command after overriding it in Bash?
Use command for external commands, such as command ls. Use builtin for Bash builtins, such as builtin echo or builtin printf.
How do I remove a Bash command override?
If the override is a function, run unset -f function_name. For example, unset -f ls removes the ls function from the current shell session.
Should I override common commands like ls, cd, or echo in scripts?
Override common commands only when there is a clear reason. In scripts, descriptive custom function names are usually easier to understand and safer to maintain.
What is the difference between command and builtin in Bash?
command runs a command while bypassing shell functions. builtin runs a Bash builtin directly. Use command for external commands and builtin when wrapping a builtin command.
QA Checklist for Bash Command Override Examples
- Confirm that each override function avoids recursion by using
commandorbuiltin. - Check that argument forwarding uses
"$@"when the wrapper should preserve all arguments. - Verify whether the overridden name is an external command, Bash builtin, alias, or existing function using
type. - Document how to remove the override with
unset -fif the tutorial asks readers to test it in an interactive shell. - Avoid recommending global overrides for shared scripts unless the behavior is clearly explained.
Conclusion
In this Bash Scripting Tutorial, we have learnt to override commands with the help of Example Bash Scripts.
A Bash function override is useful when you want to wrap an existing command with default options or extra behavior. Use command to call the original external command, use builtin for shell builtins, and use unset -f when you need to remove a function override from the current shell.
TutorialKart.com