oracledba.help
SpecialTopics

ORACLE_SID, Easily Changing

Overview

Do you have a Linux system with multiple databases? Here is an easily way to be able to quickly change the ORACLE_SID while in a BASH shell without having a separate file\profile for every ORACLE_SID.

.baschrc Aliases

To achieve this you need to add an alias for each ORACLE_SID as shown below to your .baschrc file. In this example change db1-4 and DB1-4 to your ORACLE_SIDs.

Creating ORACLE_SID Aliases

 # Set ORACLE_SID
 alias db1='echo "export ORACLE_SID=DB1">$HOME/.setORACLE_SID;source $HOME/.setORACLE_SID;echo $ORACLE_SID'
 alias db2='echo "export ORACLE_SID=DB2">$HOME/.setORACLE_SID;source $HOME/.setORACLE_SID;echo $ORACLE_SID'
 alias db3='echo "export ORACLE_SID=DB3">$HOME/.setORACLE_SID;source $HOME/.setORACLE_SID;echo $ORACLE_SID'
 alias db4='echo "export ORACLE_SID=DB4">$HOME/.setORACLE_SID;source $HOME/.setORACLE_SID;echo $ORACLE_SID'

How it Works

  1. The ; (semicolon) separates commands. So we can have multiple commands on one line.
  2. The first command creates the file .setORACLE_SID with the desired ORACLE_SID.
  3. The next command sources the file. This is actually what changes the ORACLE_SID environment variable.
  4. Next the echo command shows the current value for ORACLE_SID.
  5. Once in place you simply enter the alias name while on the command line to change the ORACLE_SID.

After making the changes to your .bashrc make sure to re-source it for your new aliases to become active. source .bashrc

The alias names (shown in lower case above) can be anything you want. The ORACLE_SID values tend to need to be in UPPER case in most environments.

You can also add a quick display of all your aliases like this:
alias db='echo "db1, db2, db3, db4"'
Then simply typing db shows all your alias commands.

Simply running an export command does not change the environment variable in your terminal session. Only sub-processes. You must source a file with the export command.