In Bash Do I Have to Export Again
Anyone who has started a terminal in Linux is familiar with the default Fustigate prompt:
[user@ $host ~]$
But did you know is that this is completely customizable and tin can contain some very useful information? Hither are a few hidden treasures you can apply to customize your Bash prompt.
How is the Bash prompt set?
The Bash prompt is prepare by the environment variable PS1 (Prompt String i), which is used for interactive shell prompts. There is as well a PS2 variable, which is used when more input is required to complete a Fustigate command.
[dneary@dhcp-41-137 ~]$ consign PS1="[Linux Rulez]$ "
[Linux Rulez] export PS2="... "
[Linux Rulez] if true; and then
... repeat "Success!"
... fi
Success!
Where is the value of PS1 set?
PS1 is a regular surround variable.
The system default value is gear up in /etc/bashrc. On my system, the default prompt is prepare with this line:
[ "$PS1" = "\\southward-\\v\\\$ " ] && PS1="[\u@\h \W]\\$ "
This tests whether the value of PS1 is \s-\v$ (the system default value), and if it is, information technology sets PS1 to the value [\u@\h \W]\\$.
If you want to see a custom prompt, however, you should not be editing /etc/bashrc. You should instead add together it to .bashrc in your Home directory.
What do \u, \h, \W, \s, and \five mean?
In the PROMPTING section of human fustigate, you lot tin can find a clarification of all the special characters in PS1 and PS2. The following are the default options:
- \u: Username
- \h: Short hostname
- \W: Basename of the current working directory (~ for habitation, the end of the current directory elsewhere)
- \s: Trounce name (bash or sh, depending on how the shell is called)
- \v: The shell's version
What other special strings tin I apply in the prompts?
There are a number of special strings that can be useful.
- \d: Expands to the engagement in the format "Tue Jun 27"
- \D{fmt}: Allows custom engagement formats—encounter man strftime for the available options
- \D{%c}: Gives the date and fourth dimension in the current locale
- \n: Include a new line (see multi-line prompts below)
- \westward: The total path of the electric current working directory
- \H: The full hostname for the current motorcar
- \!: History number—you can run any previous control with its history number by using the shell history outcome designator ! followed by the number for the specific command you are interested in. (Using Linux history is notwithstanding another tutorial...)
There are many other special characters—you can meet the full list in the PROMPTING section of the Bash man folio.
Multi-line prompts
If you use longer prompts (say if you lot include \H or \w or a full engagement-time), you may want to interruption things over two lines. Here is an example of a multi-line prompt, with the date, time, and current working directory on 1 line, and username @hostname on the second line:
PS1="\D{%c} \w\n[\u@\H]$ "
Are there whatsoever other interesting things I can do?
One thing people occasionally do is create colorful prompts. While I find them abrasive and distracting, yous may similar them. For example, to change the appointment-time higher up to display in red text, the directory in cyan, and your username on a yellow groundwork, yous could try this:
PS1="\[\e[31m\]\D{%c}\[\e[0m\]
\[\eastward[36m\]\due west\[\e[0m\]\northward[\[\e[1;43m\]\u\[\eastward[0m\]@\H]$ "
To dissect this:
- \[..\] declares some non-printed characters
- \east[.. is an escape graphic symbol. What follows is a special escape sequence to change the colour (or other characteristic) in the final
- 31m is red text (41m would be a crimson background)
- 36m is cyan text
- 1;43m declares a yellow background (1;33m would exist xanthous text)
- \[\eastward[0m\] at the end resets the colors to the last defaults
You can notice more colors and tips in the Bash prompt HOWTO. Yous can fifty-fifty make text inverted or blinking! Why on world anyone would desire to do this, I don't know. Just y'all can!
What are your favorite Bash prompt customizations? And which ones have yous seen that drive you crazy? Let me know in the comments.
This work is licensed under a Artistic Commons Attribution-Share Alike 4.0 International License.
Source: https://opensource.com/article/17/7/bash-prompt-tips-and-tricks