autojump如何更改当前工作目录?

huangapple go评论60阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年2月6日 06:42:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75356008.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定