英文:
How does autojump change the current working directory?
问题
autojump 通过维护一个数据库,记录你在命令行中最常使用的目录。然后你可以通过快捷方式cd
到这些目录,例如,要跳转到包含"foo"的目录,我只需调用j foo
而不是cd /full/path/to/foo
。
我正在尝试理解autojump如何通过在其bash脚本中调用cd
来更改目录。据我所知,这样的脚本是在一个单独的shell中执行的。以下是autojump代码中调用cd
的部分。
例如,调用这个脚本不会更改脚本外部的目录,那么autojump是如何实现的呢?
英文:
autojump works by maintaining a database of the directories you use the most from the command line. Then you can cd
to directories via shortcuts, e.g. to jump to a directory that contains "foo", I can just call j foo
instead of cd /full/path/to/foo
.
$ pwd
/some/directory
$ j foo
/full/path/to/foo
I'm trying to understand how autojump is able to change the directory by calling cd
inside its bash script. As far as I know, such a script is executed in a separate shell. Here's the part in autojump's code that calls cd
.
For example, calling this doesn't change the directory outside of the script, so how is autojump able to achieve it?
# myscript.sh
#!/bin/bash
cd path/to/foo
$ pwd
/some/directory
$ ./myscript.sh
/some/directory
答案1
得分: 2
Normally, with cd
.
So they are not scripts, they are shell functions.
Like so:
$ cat mycdfunction.sh
mycdfunction() {
echo "jumping to my place"
cd tomyplace
}
$ . mycdfunction.sh
$ mycdfunction
jumping to my place
$ pwd
myplace
英文:
> How do zoxide and autojump change the current working directory?
Normally, with cd
.
> As far as I know, such a script is executed in a separate shell.
So they are not scripts, they are shell functions.
> How is it able to call the function from the terminal in a way that it changed the directory in the terminal?
Like so:
$ cat mycdfunction.sh
mycdfunction() {
echo "jumping to my place"
cd tomyplace
}
$ . mycdfunction.sh
$ mycdfunction
jumping to my place
$ pwd
myplace
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论