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

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

#!/bin/bash
cd /home/artemb

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


启动脚本时,将创建一个只继承您的环境的新进程。当它结束时,它就结束了。您当前的环境保持不变。

相反,你可以这样开始你的脚本:

. myscript.sh

的。将评估当前环境中的脚本,因此它可能会被更改


使用pushd,当前目录被推送到目录堆栈上,并被更改为给定的目录,popd获取堆栈顶部的目录并更改为该目录。

pushd ../new/dir > /dev/null
# do something in ../new/dir
popd > /dev/null

你需要把你的脚本转换成一个shell函数:

#!/bin/bash
#
# this script should not be run directly,
# instead you need to source it from your .bashrc,
# by adding this line:
#   . ~/bin/myprog.sh
#

function myprog() {
  A=$1
  B=$2
  echo "aaa ${A} bbb ${B} ccc"
  cd /proc
}

原因是每个进程都有自己的当前目录,当您从shell执行一个程序时,它是在一个新进程中运行的。标准的“cd”,“pushd”和“popd”内置在shell解释器中,因此它们会影响shell进程。

通过使程序成为shell函数,您将添加自己的进程内命令,然后任何目录更改都会反映到shell进程中。


鉴于答案的不可读性和过于复杂,我认为这是请求者应该做的

将该脚本添加到PATH 运行脚本。scriptname

的。(.)将确保脚本不在子shell中运行。


如果你正在使用bash,你可以尝试alias:

在.bashrc文件中添加这一行:

alias p='cd /home/serdar/my_new_folder/path/'

当你在命令行上写“p”时,它会改变目录。


把上面的内容放在一起,就可以创建一个别名

alias your_cmd=". your_cmd"

如果你不想每次都写开头的"." 你想把你的脚本源到shell环境, 或者如果你只是不想记住,那就必须这么做 脚本才能正确工作。


如果您运行bash脚本,那么它将在其当前环境或其子环境上操作,而不会在父环境上操作。

如果目标是运行命令: goto.sh /home/test 然后在/home/test中交互式地工作,一种方法是在你的脚本中运行一个bash交互式子shell:

#!/bin/bash
cd $1
exec bash

这样,您将处于/home/test中,直到退出(退出或按Ctrl+C)这个shell。


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


基本上我们使用cd..从每个目录返回。我想通过给出一次需要返回的目录数量来使它更容易。您可以使用alias命令使用单独的脚本文件来实现这一点。例如:

code.sh

#!/bin/sh
 _backfunc(){
 if [ "$1" -eq 1 ]; then
  cd ..
 elif [ "$1" -eq 2 ]; then
  cd ../..
 elif [ "$1" -eq 3 ]; then
  cd ../../..
 elif [ "$1" -eq 4 ]; then
  cd ../../../..
 elif ["$1" -eq 10]; then
  cd /home/arun/Documents/work
 fi
 }
alias back='_backfunc'   

在当前shell中使用source code.sh后,您可以使用:

$back 2 

从当前目录后退两步。详细解释在这里。这里还解释了如何将代码放入~/中。Bashrc,以便每个打开的新shell都将自动具有这个新的别名命令。您可以通过添加更多if条件和不同的参数来修改代码,从而添加新的命令以进入特定的目录。你也可以从git中获取代码。


简单地去

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命令


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

假设在个人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 松材线虫病

拖鞋!


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

在你的情况下:

cd /home/artemb

保存为:

echo cd /home/artemb

然后启动它:

\`./the_script\`

然后使用相同的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

在你的shell脚本中添加以下cd行:

exec $SHELL

声明你的路径:

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

这是我目前为bash做这件事的方式(在Debian上测试)。也许有更好的办法:

不要用exec bash这样做,例如:

#!/bin/bash
cd $1
exec bash

because while it appears to work, after you run it and your script finishes, yes you'll be in the correct directory, but you'll be in it in a subshell, which you can confirm by pressing Ctrl+D afterwards, and you'll see it exits the subshell, putting you back in your original directory. This is usually not a state you want a script user to be left in after the script they run returns, because it's non-obvious that they're in a subshell and now they basically have two shells open when they thought they only had one. They might continue using this subshell and not realize it, and it could have unintended consequences. If you really want the script to exit and leave open a subshell in the new directory, it's better if you change the PS1 variable so the script user has a visual indicator that they still have a subshell open. Here's an example I came up with. It is two files, an outer.sh which you call directly, and an inner.sh which is sourced inside the outer.sh script. The outer script sets two variables, then sources the inner script, and afterwards it echoes the two variables (the second one has just been modified by the inner script). Afterwards it makes a temp copy of the current user's ~/.bashrc file, adds an override for the PS1 variable in it, as well as a cleanup routine, and finally it runs exec bash --rcfile pointing at the .bashrc.tmp file to initialize bash with a modified environment, including the modified prompt and the cleanup routine. After outer.sh exits, you'll be left inside a subshell in the desired directory (in this case testdir/ which was entered into by the inner.sh script) with a visual indicator making it clear to you, and if you exit out of the subshell, the .bashrc.tmp file will be deleted by the cleanup routine, and you'll be back in the directory you started in. Maybe there's a smarter way to do it, but that's the best way I could figure out in about 40 minutes of experimenting:

文件1:outer.sh

#!/bin/bash

var1="hello"
var2="world"

source inner.sh

echo $var1
echo $var2

cp ~/.bashrc .bashrc.tmp

echo 'export PS1="(subshell) $PS1"' >> .bashrc.tmp

cat <<EOS >> .bashrc.tmp
cleanup() {
    echo "cleaning up..."
    rm .bashrc.tmp
}

trap 'cleanup' 0
EOS

exec bash --rcfile .bashrc.tmp

文件2:inner.sh

cd testdir
var2="bird"

然后运行:

$ mkdir testdir
$ chmod 755 outer.sh

$ ./outer.sh

它应该输出:

hello
bird

然后使用exec bash将您放到子shell中,但是带有 修改后的提示符使其更明显,如下所示:

(subshell) user@computername:~/testdir$

如果你用Ctrl-D退出子shell,它应该通过删除来清理 testdir/目录下的临时.bashrc.tmp文件 我想知道是否有比复制.bashrc文件更好的方法 就像这样,在subshell中适当地改变PS1的变量…


这是以上答案的一个简化汇编。 创建shell文件shell .sh 在脚本中,在函数中更改目录

#!/bin/bash

cd folder1/folder2/

现在运行脚本。在这之前。 . 使用当前线程/会话执行脚本。

. shellfile.sh