更改 cd 默认目录 (bash)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24039048/
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
Change cd default directory (bash)
提问by Jay
I'm looking for a way to change the default directory of cd, and I'm wondering if this is possible. I tried adding
我正在寻找一种方法来更改 cd 的默认目录,我想知道这是否可行。我尝试添加
alias "cd=cd ~/Documents/Github"
to .bashrc, but this obviously doesn't work because it breaks the cd command so that you cannot use cd for anything but going to that directory. Is there some export that I could put in .bashrc to do this? I do not want to change my home directory, just the default directory cd goes to. I ask because I regularly use cd to change to my default directory that I am programming in and would like to not have to type cd ~/workspace
or cd
and then cd workspace
every time I want to change to the directory ~/workspace
.
到 .bashrc,但这显然不起作用,因为它破坏了 cd 命令,因此除了转到该目录之外,您不能将 cd 用于任何其他用途。是否有一些导出可以放入 .bashrc 中来执行此操作?我不想改变我的主目录,只是默认目录 cd 去。我问是因为我经常使用 cd 来切换到我正在编程的默认目录,并且不想每次我想切换到目录时都输入cd ~/workspace
or 。cd
cd workspace
~/workspace
回答by mklement0
Here's a simpler alternative using an alias:
这是使用别名的更简单的替代方法:
alias cd='HOME=~/Documents/Github cd'
- While this redefines
$HOME
, it does so ONLY for thecd
command, so should be safe(*). - This also obviates the need for any custom parameter pre-parsing.
- 虽然这重新定义了
$HOME
,但它只对cd
命令这样做,所以应该是安全的(*)。 - 这也消除了对任何自定义参数预解析的需要。
If you place this in ~/.bashrc
, you should get the desired behavior.
如果你把它放在 中~/.bashrc
,你应该得到想要的行为。
Note that, by default, this alias will NOT be in effect in scripts(non-interactive shells), as alias expansion is by default disabledthere (use shopt -s expand_aliases
to explicitly enable).
请注意,默认情况下,此别名在脚本(非交互式 shell)中无效,因为别名扩展在那里默认禁用(用于shopt -s expand_aliases
显式启用)。
(*) @chepner points out one restriction: with the alias in place you won't be able to do HOME=/somewhere/else cd
, i.e., you won't be able to redefine (override) $HOME
again, ad-hoc. As he further states, though, it's hard to imagine why anyone would want to do that.
(*) @chepner 指出了一个限制:使用别名你将无法做到HOME=/somewhere/else cd
,即,你将无法$HOME
再次重新定义(覆盖),即席。然而,正如他进一步指出的那样,很难想象为什么有人会想要这样做。
回答by chepner
You can shadow the builtin cd
command with a shell function. Add the following to your .bashrc
file.
您可以cd
使用 shell 函数隐藏内置命令。将以下内容添加到您的.bashrc
文件中。
cd () {
if [ $# = 0 ]; then
builtin cd ~/Documents/Github
else
builtin cd "$@"
fi
}
This isn't perfect, as it assumes you will never call the function as
这并不完美,因为它假设您永远不会将该函数称为
* cd -L
* cd -P
* cd -LP
* etc
(that is, using one or more of the supported options without an explicit directory.)
(即,在没有显式目录的情况下使用一个或多个受支持的选项。)
UPDATE
更新
This might be a little more comprehensive, but I haven't tested it.
这可能更全面一些,但我还没有测试过。
cd () {
local -a args
while getopts LP opt "$@"; do
case $opt in
-*) args+=( "$opt" ) ;;
esac
done
shift $((OPTIND-1))
# Assume at most one directory argument, since
# that is all cd uses
args+=( "${1:-~/Documents/Github}" )
builtin cd "${args[@]}"
}
回答by Pavel
this is default home location
这是默认的家庭位置
$ pwd
/home/######
now you can reset it like this
现在你可以像这样重置它
$ export HOME=/tmp
$ cd
$ pwd
/tmp
now it's up to you where to put the new $HOME definition - .bashrc
, or whatever
现在由您决定将新的 $HOME 定义放在哪里 -.bashrc
或其他任何内容