Skip to content

Instantly share code, notes, and snippets.

@ricalo
Last active April 30, 2024 15:15
Show Gist options
  • Save ricalo/245bd396bd2e53d6a559 to your computer and use it in GitHub Desktop.
Save ricalo/245bd396bd2e53d6a559 to your computer and use it in GitHub Desktop.
Use git-bash from Android Studio terminal
if [ ! -z "${IDE}" -a "${IDE}" == "AndroidStudio" ]; then
cd $OLDPWD;
fi

I love Git for Windows. It's just easy to move between Linux, Mac and Windows if I use the same tools.

Today I was wondering, how can I use bash from the terminal in Android Studio?

I ran into a blog post that explains how to do the same thing with Cygwin.

Since the Cygwin and git for Windows are similar I decided to give it a shot. Here are the steps:

  1. Create Bash-AndroidStudio.bat included in this gist.

  2. @echo off
    set IDE=AndroidStudio
    "C:\Program Files\Git\bin\bash.exe"
    
  3. Add the following to your .bashrc file

  4. if [ ! -z "${IDE}" -a "${IDE}" == "AndroidStudio" ]; then
        cd $OLDPWD;
    fi
    
  5. In Android Studio, go to settings and add the path to your batch file in Tools > Terminal > Shell path.

Open a new terminal... voila!

@echo off
set IDE=AndroidStudio
"C:\Program Files\Git\bin\bash.exe"
@AllenVork
Copy link

I'm using win7 and could you tell me where the .bashrc file is ?

@codingwithchad
Copy link

for me I found .bashrc "C:\Users%USERNAME%\AppData\Local\GitHub\PortableGit_f02737a78695063deace08e96d5042710d3e32db\etc"

@ChaosJohn
Copy link

ChaosJohn commented Nov 20, 2017

Maybe the Bash-AndroidStudio.bat should be changed to set IDE="AndroidStudio", otherwise new sessions cannot cd to $OLDPWD.

@jamesnwarner
Copy link

jamesnwarner commented Jul 15, 2018

@AllenVork, if your 'bash.exe' is in "C:\Program Files\Git\bin\bash.exe", then the .bashrc file can be found at: "C:\Program Files\Git\etc\bash.bashrc".

@smaxwell953
Copy link

I don't have a "Terminal" option in my Tools menu in Android Studio.

@tp7309
Copy link

tp7309 commented Sep 30, 2018

@ChaosJohn,yes, you are right, and we can simply set Android Studio Terminal option to "C:\Program Files\Git\bin\bash.exe" to make bash enabled.

@Dosxx
Copy link

Dosxx commented Apr 1, 2019

Helllo!
where do you create the Bash-AndroidStudio.bat file ? in which directory ?

@pfieffer
Copy link

pfieffer commented Mar 4, 2020

Helllo!
where do you create the Bash-AndroidStudio.bat file ? in which directory ?

You can put it anywhere. I put mine in C:\Users\my_user\

@pfieffer
Copy link

pfieffer commented Mar 5, 2020

A caveat to using this is when you open the Terminal you will you have to manually cd into the current working directory

@adesamp
Copy link

adesamp commented Mar 31, 2020

first, why i can't save my changes .bashrc?
second, where should i put the code?

image

@mochadwi
Copy link

first, why i can save my changes .bashrc?

You might want to access/open the git bash windows as administrator, then modify the bash.bashrc
Also don't forget to change this:

@echo off
    set IDE=AndroidStudio
    "go to your actual directory\folder\Git\bin\bash.exe"

second, where should i put the code?

In the last/beginning line of the .bashrc, e.g:

# this is the existing bash scripts
if [ ! -z "${IDE}" -a "${IDE}" == "AndroidStudio" ]; then
    cd $OLDPWD;
fi

@adesamp

@liang799
Copy link

liang799 commented Jan 9, 2022

Why is cd $OLDPWD not executed even though I followed all the steps?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment