How to Create a Bash Alias in Linux: Execute Commands Using Shortcuts.
What is Bash Alias and Bash Function ?
In Linux, we can use aliases and functions to simplify frequently used commands. While an alias is useful for creating a simple shortcut, a Bash function provides more flexibility and can execute one or multiple commands.
In Linux, we often execute the same commands repeatedly. Typing long commands every time can be tedious and error prone.
Linux provides a simple feature called a Bash alias that allows us to create shortcuts for frequently used commands.
In this blog, we will learn what a Bash alias is, how to create one, and how to make it permanent.
I am creating a Bash function to start my RAC database without writing long srvctl command. You can make your own as per your requirement.
1. Add the alias .bashrc file.
FYI,
set -x = Enables command tracing
set +x = Disables command tracing
Open your Bash configuration file:
[oracle@test ~]$ vi ~/.bashrc
Add this line at the end:
startdb() {
set -x
source /home/oracle/CDBTEST1_test.env
srvctl start database -d CDBTEST
srvctl status database -d CDBTEST
set +x
}
stopdb() {
set -x
source /home/oracle/CDBTEST1_test.env
srvctl stop database -d CDBTEST
srvctl status database -d CDBTEST
set +x
}
Save and exit.
2. Come to the prompt and execute your relevant command.
[oracle@test ~]$ stopdb
+ source /home/oracle/CDBTEST1_test.env
++ ORACLE_HOME=/u01/app/oracle/19crac
++ export ORACLE_HOME
++ ORACLE_SID=CDBTEST1
++ export ORACLE_SID
++ TNS_ADMIN=/u01/app/oracle/19crac/network/admin
++ export TNS_ADMIN
++ PATH=/usr/lib64/qt-3.3/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/oracle/.local/bin:/home/oracle/bin:/u01/app/oracle/19crac/bin:.
++ export PATH
++ ORA_NLS10=/u01/app/oracle/19crac/nls/data/9idata
++ export ORA_NLS10
++ ORACLE_UNQNAME=
++ test '' '!=' ''
+ srvctl stop database -d CDBTEST
+ srvctl status database -d CDBTEST
Instance CDBTEST1 is not running on node test
+ set +x3. How to remove Bash function ?
Temporary Removal :
Comment the corresponding line from ~/.bashrc and reload the file.
Permanent Removal :
Delete the corresponding line from ~/.bashrc and reload the file.
Important Note :
When using aliases or function to automate any kind of stuff, remember that the alias/function only provides a shortcut. It does not change how the underlying command works.
By adding a single line to ~/.bashrc, you can create a custom command shortcut, make it persistent across interactive Bash sessions, and simplify your daily administration tasks.
Happy Linux learning! 🙂
Disclaimer:
This is an independent personal blog and is not affiliated with, sponsored by, or endorsed by Oracle Corporation. The views, explanations, and experiences expressed here are the author’s own. Commands, scripts, and procedures may be based on Oracle’s official documentation and are provided for educational and reference purposes only. Always test and validate them before use in a Live/Production environment. Sample output shown in this article was generated during the author’s own testing and may vary depending on the environment.
Please read full Disclaimer.
