Sathish Manohar
Sathish Manohar

Reputation: 6119

How to check if Git is installed from .bashrc

I'm using Git, I've changed the following line in .bashrc, To show the current checkedout branch in prompt, when pwd is a Git Repo. Operating System I'm using is: Ubuntu 32bit

# Original PS1 Line
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '

I'm using this line to display current branch of git repo in shell prompt, instead of, the above line.

# PS1 Line to show current Git Branch in the Prompt
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\[\033[01;32m\]$(__git_ps1 " (%s)")\[\033[00m\]\$ '

The Problem is when I give it to friends, Shell gives error __git_ps1: command not found, while navigating between directories, as the script checks for git branch on changing directories. How do I check if Git is installed and perform the branch check only if git is installed?

Edit: As suggested by ayckoster, I cameup with the following lines of code:

if [ "$color_prompt" = yes ]; then
    git --version
    GIT_IS_AVAILABLE=$?
    if [ $GIT_IS_AVAILABLE -eq 0 ]; then
        # PS1 Line to show current Git Branch in the Prompt
        PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\[\033[01;32m\]$(__git_ps1 " (%s)")\[\033[00m\]\$ '
    else
        # Original PS1 Line
        PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
    fi
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi

Now, Everytime I open the terminal I get the git --version outputted to screen, while Git is installed, and I get the following error, while opening terminal when Git is not installed:

The program 'git' is currently not installed.  You can install it by typing:
sudo apt-get install git

How do I clear this? Thanks.

Final Edit:

This is the code I came up with finally, Feel Free to use this code in your .bashrc to display current git branch in your shell prompt

if [ "$color_prompt" = yes ]; then
    if git --version &>/dev/null; then
        # PS1 Line to show current Git Branch in the Prompt
        PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\[\033[01;32m\]$(__git_ps1 " (%s)")\[\033[00m\]\$ '
    else
        # Original PS1 Line
        PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
    fi
else
    if git --version &>/dev/null; then
        # PS1 Line to show current Git Branch in the Prompt
        PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w $(__git_ps1 "(%s)")\$ '
    else
        # Original PS1 Line
            PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
    fi
fi

Upvotes: 10

Views: 28970

Answers (7)

JoelEspinal
JoelEspinal

Reputation: 11

This worked for me:

GIT_VERSION="$(git --version)"
if [ "$GIT_VERSION" != "command not found" ]; then
  echo "git already" installed
else
    echo "git is missing"
fi

Upvotes: 1

alex
alex

Reputation: 1917

#!/bin/bash
command -v git >/dev/null 2>&1 ||
{ echo >&2 "Git is not installed. Installing..";
  yum install git
}

Upvotes: 8

Matthew Rankin
Matthew Rankin

Reputation: 461147

You could use the following to conditionally change your Bash prompt based on the existence of Git:

PS1='$no-git-prompt'
if [ -a /usr/local/bin/git ] || [ -a /usr/local/git ]; then
    PS1='$git-prompt'

The Linux Tutorial Blog entry "Tutorial: Conditions in bash scripting (if statements)" shows the different if conditions available, such as -a to check for the existence of a file.

The problem I see with this solution is that it is dependent on the location of git (i.e., /usr/local/bin/ or /usr/local/ in the above example). This may not be a problem since git on Ubuntu is found in /usr/local/ when git is installed via apt-get.

If you want to not be dependent on the installed location of git, then you could use command substitution to look at the results of the which command.

PS1='$no-git-prompt'
if [ $(which git) ]; then
    PS1='$git-prompt'

However, using which has been deemed as "evil" by other answerers. (Although, I'd be interested in them fleshing out their reasoning for such statements instead of just making the declaration without providing their reasoning.)

Upvotes: 0

ayckoster
ayckoster

Reputation: 6827

Try to execute

git --version

Depending on the return value $? you can assume git is installed or not. If you get 0 everything is fine otherwise git is not installed. You can also test this.

This assumes everything is setup correctly and git is in your $PATH and the git command is not renamed.

Use it like this

git --version 2>&1 >/dev/null # improvement by tripleee
GIT_IS_AVAILABLE=$?
# ...
if [ $GIT_IS_AVAILABLE -eq 0 ]; then #...

Upvotes: 23

Foo Bah
Foo Bah

Reputation: 26251

use type:

$ type -t git
file
$ type -t nogit
$

Upvotes: 2

Mu Qiao
Mu Qiao

Reputation: 7107

I recommend using hash as it's a built-in so no need to create a new process. It also caches the command.

hash git && commands

Upvotes: 2

Jonathan Liuti
Jonathan Liuti

Reputation: 685

using which should help. If it returns nothing --> git is not installed.

Upvotes: 4

Related Questions