How To Customize Shell Prompt To Database SID ?

Typically shell has its own default prompt with $ sign when you logged into the Linux server using Oracle user. 

Question might be come that why we should change it. 

Well, If there are multiple databases configured on the server than customizing prompt will help us to identify session where we are working at a time. 

This will help to eliminate human error by accidentally running particular command on wrong database or session.

Following are the steps to customize your default prompt to Database SID.

Default Prompt:

[oracle@db01 ~]$ 

Change it to Oracle SID

Step 1 First check the value of $PS1 prompt by executing below command

It will give below values 

[oracle@db01 ~]$ echo $PS1
[\u@\h \W]\$

PS1 (Prompt String 1) is one of the prompts available in Unix

Step 2 Export Oracle SID:

$export ORACLE_SID=test

Step 3 Now export PS1 with ORACLE_SID 

$export PS1=[$ORACLE_SID]

Step 4 After executing above command you will get the prompt like below

[oracle@db01 ~]$ export PS1=[$ORACLE_SID]

You would also like below examples for customization of prompt.

  • Display number of seconds completed with Oracle SID since the terminal opened

[oracle@db01 ~]$ export ORACLE_SID=test
[oracle@db01 ~]$ PS1='$SECONDS [$ORACLE_SID] '
10 [test]
15 [test]
21 [test]

  • Change prompt to  present machine time with Oracle SID

[oracle@db01 ~]$ export ORACLE_SID=test
[oracle@db01 ~]$ PS1='\A [$ORACLE_SID] '
12:52 [test]
12:52 [test]

  • Change prompt to hostname with Oracle SID:

[oracle@db01 ~]$ PS1='$(hostname) [$ORACLE_SID] ' [test] [test]

  • You can use your name as well in prompt with Oracle SID

[oracle@db01 ~]$ export ORACLE_SID=test
[oracle@db01 ~]$ PS1='John [$ORACLE_SID] '
John [test]
John [test]

Below are control command for PS1 which you can use to customize your prompt.

\d – the date in "Weekday Month Date" format (e.g., "Tue May 26")
\e – an ASCII escape character (033)
\h – the hostname up to the first .
\H – the full hostname
\j – the number of jobs currently run in background
\l – the basename of the shells terminal device name
\n – newline
\r – carriage return
\s – the name of the shell, the basename of $0 (the portion following the final slash)
\t – the current time in 24-hour HH:MM:SS format
\T – the current time in 12-hour HH:MM:SS format
\@ – the current time in 12-hour am/pm format
\A – the current time in 24-hour HH:MM format
\u – the username of the current user
\v – the version of bash (e.g., 4.00)
\V – the release of bash, version + patch level (e.g., 4.00.0)
\w – Complete path of current working directory
\W – the basename of the current working directory
\! – the history number of this command
\# – the command number of this command
\$ – if the effective UID is 0, a #, otherwise a $
\nnn – the character corresponding to the octal number nnn
\\ – a backslash
\[ – begin a sequence of non-printing characters, which could be used to embed a terminal control sequence into the prompt
\] – end a sequence of non-printing characters

No comments:

Post a Comment