bash 使用 Cygwin 设置默认路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8524612/
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
Setting default path with Cygwin
提问by Puppy
I've got a Cygwin installation, and I'd like it to start Bash in a certain directory whenever I start up. How can I achieve this?
我有一个 Cygwin 安装,我希望它在启动时在某个目录中启动 Bash。我怎样才能做到这一点?
采纳答案by Kevin
In your ~/.bashrc, You can either change your [tried and it didn't work] add a $HOMEto that directory, or you cancdto that directory at the end of the file.
在您的~/.bashrc, 您可以将您的[尝试过但没有成功]$HOME目录更改为该目录,或者您可以cd在文件末尾向该目录添加一个。
回答by Marc-Antoine Dubé
In your ~/.bash_profileyou may simply write cd /cygdrive/c/path/to/where/you/want/cygwin/to/start. You will find this file in your cygwin installation folder, under <path_to_cygwin>\home\<user>\.bash_profile. (In my case: C:\cygwin64\home\User\.bash_profile).
在你的~/.bash_profile你可以简单地写cd /cygdrive/c/path/to/where/you/want/cygwin/to/start. 您将在 cygwin 安装文件夹中的<path_to_cygwin>\home\<user>\.bash_profile. (就我而言:)C:\cygwin64\home\User\.bash_profile。
回答by user236976
python script
蟒蛇脚本
!!before use add .bashrs any string to the end!!
!!在使用之前添加 .bashrs 任何字符串到最后!!
use name_script.py c:\path
使用 name_script.py c:\path
path_bachrc - path to .bashrc
path_bachrc - .bashrc 的路径
cmd - path to cygwin.bat
cmd - cygwin.bat 的路径
#***********************************************#
# [email protected] #
#***********************************************#
import argparse
import subprocess
import os
path_bachrc = 'c:/PP/cygwin/home/adm/.bashrc'
cmd = 'c:\PP\cygwin\Cygwin.bat'
def delEndLineFromFile(filename):
with open(filename, 'r') as f:
aList = f.readlines()
bList = aList[0:-1]
with open(filename, 'w') as fd:
fd.writelines(bList)
parser = argparse.ArgumentParser()
parser.add_argument("newPath", type=str, help="New path in .bachrc cygwin")
args = parser.parse_args();
delEndLineFromFile(path_bachrc);
p = args.newPath;
pNew = 'cd /cygdrive/' + p[:1] + p[2:].replace('\', '/')
print(pNew)
with open(path_bachrc, 'a') as f:
f.write(pNew)
PIPE = subprocess.PIPE
p = subprocess.Popen(cmd, shell = True)
回答by Warren Young
Bash on Cygwin starts up in your home folder, just like on Linux, which Cygwin mimics as closely as it can. So, you simply need to change your home folder.
Cygwin 上的 Bash 会在您的主文件夹中启动,就像在 Linux 上一样,Cygwin 尽可能模仿。因此,您只需要更改您的主文件夹。
(Note that your Cygwin folder need not be the same as your Windows user home folder. By default, they are different, but you could make them the same by putting something like /cygdrive/c/Users/myidinto your Cygwin user entry in /etc/passwd.)
(请注意,您的 Cygwin 文件夹不必与您的 Windows 用户主文件夹相同。默认情况下,它们是不同的,但您可以通过将类似的内容/cygdrive/c/Users/myid放入 中的 Cygwin 用户条目中来使它们相同/etc/passwd。)

