bash 如何默认到其他目录而不是主目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7017143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 20:51:58  来源:igfitidea点击:

How to default to other directory instead of home directory

bashshellgit-bash

提问by Xi Zhang

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:

我正在 Windows 机器上开发。linux 命令行唯一需要的地方是 Git Bash。问题是:当我打开它时,我在主目录中。我必须将目录更改为我的工作区,例如:

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.

我可以将它包装在一个 .sh 文件中,这样我就不必再手动输入了吗?这应该很简单,但我对 Linux 命令行的了解为零。如果您能带我了解如何创建该 .sh 文件,我真的很感激。

采纳答案by Keith Thompson

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

只需将该行写入文件“cd.sh”,然后从 shell 提示符执行此操作:

. ./cd.sh

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

或者您可以在$HOME/.bashrc文件中创建别名或函数:

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:

如果目录名称包含空格或其他 shell 元字符,则需要引号;即使它们不是必需的,添加它们也无妨:

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 .bashrcafter the function definition, your shell will start in that directory. Or you can just use the cdcommand directly in your .bashrcif you aren't going to need to use the function later.

你的.bashrc函数定义后,你的shell会在该目录中开始。或者cd.bashrc如果您以后不需要使用该功能,则可以直接在您的命令中使用该命令。

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

(名称foo只是一个示例;您应该选择一个更有意义的名称。)

回答by David Siegal

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.

这是一个更像 Windows 的解决方案:右键单击用于启动 git bash 的 Windows 快捷方式,然后单击“属性”。将“Start In”的值更改为所需的工作区路径。

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

编辑:还要检查 Target 值是否不包括 --cd-to-home 选项,如下面的评论中所述。

回答by orip

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

将该行添加到.bashrc主目录中的文件中(如果文件不存在,则创建该文件):

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

回答by Juri

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

我使用ConEmu (在 Windows 上强烈推荐),我有一个启动 Git Bash 的任务,比如

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

请注意底部的“启动目录...”按钮。它-new_console:d:<path>在 Git Bash 的启动命令中添加了一个。让它指向你喜欢的任何地方

回答by Krish

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-homeor it will continue to start in the directory specified in your HOME variable
  1. 右键单击 git bash -> 属性
  2. 在“快捷方式”选项卡中 -> 在字段中开始 -> 输入您的用户定义路径
  3. 确保 Target 字段不包含--go-to-home,否则它将继续在 HOME 变量中指定的目录中启动

Thats it.

就是这样。

回答by Francis

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

我也只是将快捷方式图标的“开始于”设置更改为: %HOMEDRIVE%/xampp/htdocs/

回答by Usagi

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.

现在,当您打开 shell 时,它将从 home 向上移动三个目录并更改为 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.)

此代码只是将行“cd ../../../d/work_space_for_my_company/project/code_source”附加到名为“.bashrc”的文件中。如果文件不存在,“>>”将创建一个文件,然后追加。.bashrc 文件可用于在启动/登录时运行命令(即加载模块等)

回答by geneorama

(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.

在 Windows 中执行此操作的真正简单方法是创建一个名为 HOME 的环境变量,该变量指向所需的主目录。

  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.
  1. 右键单击我的电脑,然后选择属性
  2. 选择高级系统设置(位置因 Windows 版本而异)
  3. 在系统属性中,选择高级选项卡
  4. 在高级选项卡上,选择环境变量(底部按钮)
  5. 在“系统变量”下检查您是否已经有一个名为 HOME 的变量。如果是这样,请通过突出显示变量名称并单击编辑来编辑该变量。使新变量名称成为所需的路径。
  6. 如果 HOME 不存在,请单击系统变量下的“新建”并创建一个名为 HOME 的新变量,其值为所需路径。

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).

注意:这可能会改变其他事情的工作方式。例如,对我来说,它改变了我的 .ssh 配置文件所在的位置。就我而言,我希望我的家是 U:\,因为这是我放置项目工作和应用程序设置的主要位置(即它确实是我的“家”目录)。

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!

编辑 2017 年 6 月 23 日:这个答案偶尔会得到赞成票,我想警告人们,虽然这可能“有效”,但我同意 @AnthonyRaymond 不推荐这样做。这更像是一个临时修复或修复,如果您不在乎其他事情是否会中断。改变你的家不会造成主动损害(比如删除你的硬盘驱动器),但它可能会在以后造成潜在的烦恼。当您开始遇到烦人的问题时,您可能不会记得这个变化……所以您以后可能会挠头!

回答by Michael Stramel

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.

右键单击 Git Bash 应用程序链接,转到“属性”并将“起始位置”修改为您希望它开始的位置。

回答by spottedmahn

From a Pinned Start Menu Item in Windows 10

Windows 10 中固定的开始菜单项

  1. Open the file location of the pinned shortcut
  2. Open the shortcut properties
    1. Remove --cd-to-homearg
    2. Update Start inpath
  3. Re-pin to start menu via recently added
  1. 打开固定快捷方式的文件位置
  2. 打开快捷方式属性
    1. 删除--cd-to-home参数
    2. 更新Start in路径
  3. 通过最近添加的重新固定到开始菜单

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...

感谢所有其他答案如何做到这一点!想提供Win 10使用说明...