简体   繁体   English

如何在bash --login -i上更改bash中的目录

[英]How to change directory in bash upon bash --login -i

I am using Cygwin and wish to run it with diff params so I can change dir as soon as I launch in Windows. 我正在使用Cygwin并希望使用diff params运行它,所以我可以在Windows中启动后立即更改dir。

For example 例如

bash --login -i ; cd /

or 要么

bash --login -i ; cd /tmp

but the cd does not work, how can I pass a change directory to 但是cd不起作用,如何将更改目录传递给

bash --login -i

FYI I can't put the cd in .bash, it has to be passed in 仅供参考我无法将cd放入.bash中,必须将其传入

tried this with no luck 试了这个没有运气

bash --login -i -s cd /tmp;

One hack is to use -c to change the directory, then immediately start a new shell in place of the first one. 一个hack是使用-c来更改目录,然后立即启动一个新shell来代替第一个。 The working directory is inherited. 工作目录是继承的。 Note that any shell (that supports exec ) could be used to start the new process; 请注意, 任何 shell(支持exec )都可用于启动新进程; once the working directory has been changed, then you can start the interactive login instance of bash . 一旦工作目录被更改,那么您可以启动bash的交互式登录实例。

bash -c "cd /tmp; exec bash --login -i"

I use .bashrc and .bash_logout to recover previous locations. 我使用.bashrc.bash_logout来恢复以前的位置。 In .bash_logout I store the current directory in a file: .bash_logout我将当前目录存储在一个文件中:

if [ -d ~/.recent-locations ]; then
    pwd > ~/.recent-locations/locus-$RANDOM
fi

and in .bashrc I consume the stored locations: .bashrc我使用存储的位置:

if [ -d .recent-locations ]; then
    for x in $(ls .recent-locations); do
        dstdir=$(cat .recent-locations/$x)
        rm .recent-locations/$x
        cd $dstdir
        break
    done
fi

For this to work you need to logout explicitly using Ctrl-D or logout ; 为此,您需要使用Ctrl-Dlogout明确logout ; on the other hand if you don't want to record your location you simply shut down the terminal window via the window manager. 另一方面,如果您不想记录您的位置,只需通过窗口管理器关闭终端窗口即可。

ok so I found a solution to what I needed. 好的,所以我找到了我需要的解决方案。 in Cygwin .bashrc I put 在Cygwin .bashrc我放了

"$OLDPWD" and it will automatically switch me to the last directory I was in Windows before forks Cygin, "$OLDPWD" ,它将自动切换到我在Windows中的最后一个目录,然后分叉,

Hopefully helps others, 希望能帮助别人,

Sean 肖恩

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM