Reputation: 93
Terminal/Bash has a default set of commands, such as cp, echo, grep,...
I'd like to be able to add a command, like "hello" that I can execute and get a result instead of -bash: hello: command not found.
Upvotes: 1
Views: 949
Reputation: 5129
You can add custom functions to your .bashrc
or if you mean actual stand-alone applications, then you could make a directory such as ~/bin
and then add it to the $PATH
variable in your .bash_profile
or .profile
and put any stand-alone applications in said ~/bin
directory.
Greg's Wiki is a pretty reliable source for information on functions and other aspects of Bash, along with the Bash Manual and the Bash Hackers Wiki.
NOTE:
If you have a .profile
you can still use .bash_profile
, if you choose not to use .bash_profile
, please be sure to encapsulate any code for Bash within an if-statement since .profile
is used by other shells:
if [ -n "$BASH_VERSION" ]; then # BASH_VERSION is defined, therefore we are using BASH
# BASH CODE
fi
To simplify things you could even create functions allowing you to easily run one-liners depending on which shell you're running:
ifbash()
{
if [ -n "$BASH_VERSION" ]; then
$@
else
return 1
fi
}
ifzsh()
{
if [ -n "$ZSH_VERSION" ]; then
$@
else
return 1
fi
}
# You can use these to conditionally execute commands
# here's a function that prints the type/blueprint of a function
fn-printout()
{
for y in "$@"; do
ifbash type "$y" || ifzsh whence -f "$y"
done
}
(Note: while using [ TEST ]
is not recommended when using Bash, other shells may not understand [[ TEST ]]
; this is one reason why it is better to simply use .bash_profile
, instead of .profile
)
As pointed out in some comments, while you can define variables such as $PATH
in your .bashrc
, it is not necessarily recommended as this will cause said variable to be re-set every time you start an interactive session of Bash, resulting in unnecessary computational steps every time you start a new interactive session--it's better to store variables and other environmental changes that only need happen once (such as at login) in .bash_profile
, or .profile
, which can then be re-sourced as needed using bash -l
.
Upvotes: 4
Reputation: 26985
You just need to modify your current PATH and give priority to the paths you care more, for example, you could have a directory in your $HOME/mycommands
and a PATH like this:
PATH="$HOME/mycomands:/usr/local/bin:/usr/bin" ...
in $HOME/mycommands
you could have your custom commands that will be called first instead of the ones defined on your system
Upvotes: 0