Difference between revisions of "Linux shell/Bash prompt PS1, settings and history"

From Ever changing code
Jump to navigation Jump to search
Line 19: Line 19:
It will similar to
It will similar to
[[File:Bash-git-branch-prompt.png|none|left|Git branch in bash prompt]]
[[File:Bash-git-branch-prompt.png|none|left|Git branch in bash prompt]]
= Reload shell without logging out =
. ~/.bashrc
source ~/.bashrc
exec bash
exec "$BASH"
Differences
*<tt>'''source ~/.bashrc'''</tt> will preserve your current shell. Except for the modifications that reloading <tt>~/.bashrc</tt> into the current shell (sourcing) makes, the current shell and its state are preserved, which includes environment variables, shell variables, shell options, shell functions, and command history.
*<tt>'''exec bash'''</tt>, or, more robustly, <tt>exec "$BASH"[1]</tt>, will replace your current shell with a new instance, and therefore only preserve your current shell's environment variables (including ones you've defined ad-hoc). In other words: Any ad-hoc changes to the current shell in terms of shell variables, shell functions, shell options, command history are lost.
[1] exec bash could in theory execute a different bash executable than the one that started the current shell, if it happens to exist in a directory listed earlier in the $PATH. Since special variable $BASH always contains the full path of the executable that started the current shell, exec "$BASH" is guaranteed to use the same executable.


= Resources =
= Resources =
*[https://help.ubuntu.com/community/CustomizingBashPrompt CustomizingBashPrompt] Ubuntu wiki
*[https://help.ubuntu.com/community/CustomizingBashPrompt CustomizingBashPrompt] Ubuntu wiki
*[https://github.com/magicmonty/bash-git-prompt/blob/master/README.md Git Bash Prompt repo project] Great informative Git prompt
*[https://github.com/magicmonty/bash-git-prompt/blob/master/README.md Git Bash Prompt repo project] Great informative Git prompt

Revision as of 13:01, 9 April 2017

This are steps to set up bash prompt showing git branch. This has been tested in Ubuntu 14 LTS

Edit vi ~/.bashrc

  1. Uncomment #force_color_prompt=yes
  2. Find if [ "$color_prompt" = yes ]; then statement
  3. then comment out #PS1= and add following code in bold
if [ "$color_prompt" = yes ]; then
        parse_git_branch() {
        git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
        }
        PS1="\u@\h \[\033[32m\]\w\[\033[33m\]\$(parse_git_branch)\[\033[00m\] $ "
       #PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ ' #this is default colour prompt
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi

It will similar to

Git branch in bash prompt

Reload shell without logging out

. ~/.bashrc
source ~/.bashrc
exec bash
exec "$BASH"

Differences

  • source ~/.bashrc will preserve your current shell. Except for the modifications that reloading ~/.bashrc into the current shell (sourcing) makes, the current shell and its state are preserved, which includes environment variables, shell variables, shell options, shell functions, and command history.
  • exec bash, or, more robustly, exec "$BASH"[1], will replace your current shell with a new instance, and therefore only preserve your current shell's environment variables (including ones you've defined ad-hoc). In other words: Any ad-hoc changes to the current shell in terms of shell variables, shell functions, shell options, command history are lost.

[1] exec bash could in theory execute a different bash executable than the one that started the current shell, if it happens to exist in a directory listed earlier in the $PATH. Since special variable $BASH always contains the full path of the executable that started the current shell, exec "$BASH" is guaranteed to use the same executable.

Resources