是否可以从脚本更改当前目录?
我想在Bash中创建一个目录导航实用程序。我创建了一个测试脚本,如下所示:
#!/bin/bash
cd /home/artemb
当我从Bash shell执行脚本时,当前目录不会改变。是否有可能从脚本更改当前shell目录?
是否可以从脚本更改当前目录?
我想在Bash中创建一个目录导航实用程序。我创建了一个测试脚本,如下所示:
#!/bin/bash
cd /home/artemb
当我从Bash shell执行脚本时,当前目录不会改变。是否有可能从脚本更改当前shell目录?
当前回答
我还创建了一个名为goat的实用程序,您可以使用它来简化导航。
你可以在GitHub上查看源代码。
从v2.3.1开始,使用概述如下所示:
# Create a link (h4xdir) to a directory:
goat h4xdir ~/Documents/dev
# Follow a link to change a directory:
cd h4xdir
# Follow a link (and don't stop there!):
cd h4xdir/awesome-project
# Go up the filesystem tree with '...' (same as `cd ../../`):
cd ...
# List all your links:
goat list
# Delete a link (or more):
goat delete h4xdir lojban
# Delete all the links which point to directories with the given prefix:
goat deleteprefix $HOME/Documents
# Delete all saved links:
goat nuke
# Delete broken links:
goat fix
其他回答
启动脚本时,将创建一个只继承您的环境的新进程。当它结束时,它就结束了。您当前的环境保持不变。
相反,你可以这样开始你的脚本:
. myscript.sh
的。将评估当前环境中的脚本,因此它可能会被更改
我已经做了一个脚本来更改目录。来看看:https://github.com/ygpark/dj
在你的shell脚本中添加以下cd行:
exec $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 松材线虫病
拖鞋!
我还创建了一个名为goat的实用程序,您可以使用它来简化导航。
你可以在GitHub上查看源代码。
从v2.3.1开始,使用概述如下所示:
# Create a link (h4xdir) to a directory:
goat h4xdir ~/Documents/dev
# Follow a link to change a directory:
cd h4xdir
# Follow a link (and don't stop there!):
cd h4xdir/awesome-project
# Go up the filesystem tree with '...' (same as `cd ../../`):
cd ...
# List all your links:
goat list
# Delete a link (or more):
goat delete h4xdir lojban
# Delete all the links which point to directories with the given prefix:
goat deleteprefix $HOME/Documents
# Delete all saved links:
goat nuke
# Delete broken links:
goat fix