1

As part of an online course on Git, the tutor recommended that we add some commands at the end of the .bashrc file so that the terminal prompt displays some useful Git-related information. These changes are very useful for the Git course but I don't like them for general use.

The Gnome Terminal has the ability to create profiles and set per profile preferences.

Is it possible to configure Bash in such a way that the changes added to the .bashrc file are associated only with a single terminal profile? I would like the Git-related information displayed in one terminal profile but not the others.

In case it is pertinent, this is the code at the end of the .bashrc file:

# Enable tab completion
source ~/git-completion.bash

# colors!
green="\[\033[0;32m\]"
blue="\[\033[0;34m\]"
purple="\[\033[0;35m\]"
reset="\[\033[0m\]"

# Change command prompt
source ~/git-prompt.sh
export GIT_PS1_SHOWDIRTYSTATE=1
# '\u' adds the name of the current user to the prompt
# '\$(__git_ps1)' adds git-related stuff
# '\W' adds the name of the current directory
export PS1="$purple\u$green\$(__git_ps1)$blue \W $ $reset"
8
  • 1
    I haven't tried it, but I guess you could have a custom ~/.bashrc_git that sources your regular rc file(s), plus all the git-specific stuff, and then set the gnome terminal profile's custom command to be /bin/bash --rcfile=~/.bashrc_git May 13, 2019 at 16:42
  • 1
    Or, similarly, have a custom command of env FOO=bar bash and branch in .bashrc based on the value $FOO. Unfortunately the current directory of the previous tab is not preserved in these cases.
    – egmont
    May 13, 2019 at 16:46
  • 2
    A somewhat different approach could be not to use profiles, but to set up shell aliases (or functions) that quickly toggle the prompt. It might be faster to type let's say giton and gitoff or whatever names you choose, rather than fiddle with the mouse. Moreover, this way you don't have to decide when you open a new terminal.
    – egmont
    May 13, 2019 at 16:47
  • 1
    Instead of appending to ~/.bashrc create a new file called ~/.bashgit with said commands. After opening a terminal and doing your fit stuff type . .bashgit (observe space between two .. Use ctrl+shit+t for new terminal tab to get unadultered .bashrc if you've already run the git mods. May 13, 2019 at 17:23
  • @steeldriver Using /bin/bash --rcfile ~/.bashrc_git works (without the =) which is close to what I was looking for. I must set the modified profile as the default profile or use File > New window/tab to open a new terminal using the modified profile - just changing the profile via the Terminal menu does not run the custom command unfortunately (which is what I would have liked). You could re-post your comment (corrected) as an answer and I will accept it. May 13, 2019 at 18:28

1 Answer 1

1

Create separate run commands (rc) file to .bashrc

Keep your ~/.bashrc as is. When opening a terminal you will have normal operations.

The lines you were thinking of appending to ~/.bashrc put into a new file instead:

# ~/.gitrc
# Call after opening terminal use ". .gitrc" when it is time to
# work on Git course labs.

# Enable tab completion
source ~/git-completion.bash

# colors!
green="\[\033[0;32m\]"
blue="\[\033[0;34m\]"
purple="\[\033[0;35m\]"
reset="\[\033[0m\]"

# Change command prompt
source ~/git-prompt.sh
export GIT_PS1_SHOWDIRTYSTATE=1
# '\u' adds the name of the current user to the prompt
# '\$(__git_ps1)' adds git-related stuff
# '\W' adds the name of the current directory
export PS1="$purple\u$green\$(__git_ps1)$blue \W $ $reset"

cd ~/gitdir

Toggle between two worlds

After opening the terminal, should you desire git prompts use: . .gitrc.

After running . .gitrc, should you want regular prompts in the same terminal session, simply use Ctrl+Shift+T. A new terminal tab will open with the regular ~/.bashrc without the ~/.gitrc modifications.

DO NOT make ~/.gitrc executable

There is no need to use chmod a+x on ~/.gitrc because you are "sourcing" it. This means it runs within the current shell and not as a sub-shell process which must be marked as executable but also means variables disappear after running.

In essence marking ~/.gitrc as an executable and then calling it with .gitrc instead of . .gitrc will break it. Keeping ~/.gitrc a regular file ensures you don't accidentally execute it as a command.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.