How to default to other directory instead of home directory

BashShellGit Bash

Bash Problem Overview


I am developing on a windows machine. The only place I need for linux command line is Git Bash. The problem is: When I open it, I am in the home directory. I have to change the directory to my workspace, like:

cd ../../../d/work_space_for_my_company/project/code_source

Can I wrap this in a .sh file so I don't have to hand-type it anymore? This should be simple but I have zero knowledge about Linux command line. I am really appreciated If you can walk me through how to create that .sh file.

Bash Solutions


Solution 1 - Bash

Here's a more Windows-ish solution: Right click on the Windows shortcut that you use to launch git bash, and click Properties. Change the value of "Start In" to your desired workspace path.

Edit: Also check that the Target value does not include the --cd-to-home option as noted in the comments below.

Solution 2 - Bash

Just write that line to a file "cd.sh", then do this from your shell prompt:

. ./cd.sh

Or you can create an alias or function in your $HOME/.bashrc file:

foo() { cd /d/work_space_for_my_company/project/code_source ; }

If the directory name includes spaces or other shell metacharacters, you'll need quotation marks; it won't hurt to add them even if they're not necessary:

foo() { cd "/d/Work Space/project/code_source" ; }

(Note that I've omitted the ../../..; you don't need it.)

EDIT: If you add a line

foo

to your .bashrc after the function definition, your shell will start in that directory. Or you can just use the cd command directly in your .bashrc if you aren't going to need to use the function later.

(The name foo is just an example; you should pick a more meaningful name.)

Solution 3 - Bash

Add the line to the .bashrc file in the home directory (create the file if it doesn't exist):

cd ~
touch .bashrc
echo "cd ~/Desktop/repos/" >> .bashrc

Solution 4 - Bash

I use ConEmu (strongly recommended on Windows) where I have a task for starting Git Bash like

enter image description here

Note the button "Startup dir..." in the bottom. It adds a -new_console:d:<path> to the startup command of the Git Bash. Make it point to wherever you like

Solution 5 - Bash

This may help you.

image description

  1. Right click on git bash -> properties
  2. In Shorcut tab -> Start in field -> enter your user defined path
  3. Make sure the Target field does not include --go-to-home or it will continue to start in the directory specified in your HOME variable

Thats it.

Solution 6 - Bash

I also just changed the "Start in" setting of the shortcut icon to: %HOMEDRIVE%/xampp/htdocs/

Solution 7 - Bash

(Please read warning below)

Really simple way to do this in Windows (works with git bash, possibly others) is to create an environmental variable called HOME that points to your desired home directory.

  1. Right click on my computer, and choose properties
  2. Choose advanced system settings (location varies by Windows version)
  3. Within system properties, choose the advanced tab
  4. On the advanced tab, choose Environmental Variables (bottom button)
  5. Under "system variable" check to see if you already have a variable called HOME. If so, edit that variable by highlighting the variable name and clicking edit. Make the new variable name the desired path.
  6. If HOME does not already exist, click "new" under system variables and create a new variable called HOME whose value is desired path.

Environmental Variable

NOTE: This may change the way other things work. For example, for me it changes where my .ssh config files live. In my case, I wanted my home to be U:, because that's my main place that I put project work and application settings (i.e. it really is my "home" directory).

EDIT June 23, 2017: This answer continues to get occasional upvotes, and I want to warn people that although this may "work", I agree with @AnthonyRaymond that it's not recommended. This is more of a temporary fix or a fix if you don't care if other things break. Changing your home won't cause active damage (like deleting your hard drive) but it's likely to cause insidious annoyances later. When you do start to have annoying problems down the road, you probably won't remember this change... so you're likely to be scratching your head later on!

Solution 8 - Bash

This will do it assuming you want this to happen each time you open the command line:

echo cd ../../../d/work_space_for_my_company/project/code_source >> ~/.bashrc

Now when you open the shell it will move up three directories from home and change to code_source.

This code simply appends the line "cd ../../../d/work_space_for_my_company/project/code_source" to a file named ".bashrc". The ">>" creates a file if it does not exist and then appends. The .bashrc file is useful for running commands at start-up/log-in time (i.e. loading modules etc.)

Solution 9 - Bash

Right-click the Git Bash application link go to Properties and modify the Start in location to be the location you want it to start from.

Solution 10 - Bash

From a Pinned Start Menu Item in Windows 10

  1. Open the file location of the pinned shortcut
  2. Open the shortcut properties
  3. Remove --cd-to-home arg
  4. Update Start in path
  5. Re-pin to start menu via recently added

open file location screenshot

open shortcut properites screenshot

update shortcut properites screenshot

pin via recently added screnshot


Thanks to all the other answers for how to do this! Wanted to provide Win 10 instructions...

Solution 11 - Bash

For windows: Follow these steps-

  1. Go to windows home> Right click on "Git Bash" application.
  2. Properties> Shortcut
  3. Change these two settings: (a) Delete --cd-to-home from target (b) Type folder path you want to start with git in "Start in".

This worked for me:)

Solution 12 - Bash

If you want to have projects choice list when u open GIT bash:

  • edit ppath in code header to your git projects path, put this code into .bashrc file and copy it into your $HOME dir (in Win Vista / 7 it is usually c:\Users$YOU)

.

#!/bin/bash
ppath="/d/-projects/-github"
cd $ppath
unset PROJECTS
PROJECTS+=(".")
i=0

echo
echo -e "projects:\n-------------"

for f in *
do
	if [ -d "$f" ]
	then
		PROJECTS+=("$f")
		echo -e $((++i)) "- \e[1m$f\e[0m"
	fi
done


if [ ${#PROJECTS[@]} -gt 1 ]
then
	echo -ne "\nchoose project: "
	read proj
	case "$proj" in
		[0-`expr ${#PROJECTS[@]} - 1`]) cd "${PROJECTS[proj]}" ;;
		*) echo " wrong choice" ;;
	esac
else
	echo "there is no projects"
fi
unset PROJECTS
  • you may want set this file as executable inside GIT bash chmod +x .bashrc (but its probably redundant, since this file is stored on ntfs filesystem)

Solution 13 - Bash

If you type this command: echo cd d:/some/path >> ~/.bashrc

Appends the line cd d:/some/path to .bashrc. The >> creates a file if it doesn’t exist and then appends.

Solution 14 - Bash

My Git Bash shortcut on Windows complained when I put the cd to my work directory into ~/.bashrc

WARNING: Found ~/.bashrc but no ~/.bash_profile, ~/.bash_login or ~/.profile.

This looks like an incorrect setup.
A ~/.bash_profile that loads ~/.bashrc will be created for you.

So git created this .bash_profile:

$ cat ~/.bash_profile
# generated by Git for Windows
test -f ~/.profile && . ~/.profile
test -f ~/.bashrc && . ~/.bashrc

Which does the job.

Alternatively, you can just remove the .bashrc again and put the cd command into .bash_profile:

$ rm ~/.bashrc
$ echo "cd Source/Repos" >~/.bash_profile

$ cat ~/.bash_profile
cd Source/Repos

Once this is done you can close the Window and re-open it using your desktop shortcut and the prompt will tell you that your location is now where you wanted it - looks like this is my case:

Administrator@raptor1 MINGW64 ~/Source/Repos
$

Solution 15 - Bash

it must be cd d:/work_space_for_....

without the : it doesn't work for me

Solution 16 - Bash

Another solution for Windows users will be to copy the Git Bash.lnk file to the directory you need to start from and launch it from there.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionXi 张熹View Question on Stackoverflow
Solution 1 - BashDavid SiegalView Answer on Stackoverflow
Solution 2 - BashKeith ThompsonView Answer on Stackoverflow
Solution 3 - BashoripView Answer on Stackoverflow
Solution 4 - BashJuriView Answer on Stackoverflow
Solution 5 - BashKrishView Answer on Stackoverflow
Solution 6 - BashFrancisView Answer on Stackoverflow
Solution 7 - BashgeneoramaView Answer on Stackoverflow
Solution 8 - BashUsagiView Answer on Stackoverflow
Solution 9 - BashMichael StramelView Answer on Stackoverflow
Solution 10 - BashspottedmahnView Answer on Stackoverflow
Solution 11 - BashMd Nehaluddin HaiderView Answer on Stackoverflow
Solution 12 - Bashs1w_View Answer on Stackoverflow
Solution 13 - BashmtpultzView Answer on Stackoverflow
Solution 14 - Bashdr. rAIView Answer on Stackoverflow
Solution 15 - BashlarsboView Answer on Stackoverflow
Solution 16 - Bashskip405View Answer on Stackoverflow