是否可以从脚本更改当前目录?

我想在Bash中创建一个目录导航实用程序。我创建了一个测试脚本,如下所示:

#!/bin/bash
cd /home/artemb

当我从Bash shell执行脚本时,当前目录不会改变。是否有可能从脚本更改当前shell目录?


当前回答

声明你的路径:

PATH='/home/artemb'     
cd ${PATH}

其他回答

我喜欢为不同的项目做同样的事情,而不需要启动新的shell。

在你的情况下:

cd /home/artemb

保存为:

echo cd /home/artemb

然后启动它:

\`./the_script\`

然后使用相同的shell访问该目录。

这种方法对我来说更容易。

假设在个人iMac上,你是管理员,当打开命令窗口时,在默认目录下/Users/jdoe,这将是进入的目录:/Users/jdoe/Desktop/Mongo/db.3.2.1/bin。

下面这些步骤可以帮助你完成工作:

vi mongobin,我进去了: cd /Users/jdoe/Desktop/Mongo/db.3.2.1/bin作为第一行。 Chmod 755 mongobin 源mongobin 松材线虫病

拖鞋!

我已经做了一个脚本来更改目录。来看看:https://github.com/ygpark/dj

简单地去

yourusername/.bashrc (or yourusername/.bash_profile on MAC) by an editor

然后把下面的代码加到最后一行:

alias yourcommand="cd /the_path_you_wish"

然后退出编辑。

然后输入:

source ~/.bashrc or source ~/.bash_profile on MAC.

现在你可以在终端中使用:your命令

声明你的路径:

PATH='/home/artemb'     
cd ${PATH}