Bash如何在其他目录上下文中执行命令? [英] How can Bash execute a command in a different directory context?

查看:146
本文介绍了Bash如何在其他目录上下文中执行命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通用命令,可以从非常特定的目录中调用.该程序中只有一个可执行文件位于/bin中,并且当前工作目录对于正确运行它非常重要.该脚本会影响运行在该目录中的文件.

I have a common command that gets called from within very specific directories. There is only one executable sitting in /bin for this program, and the current working directory is very important for running it correctly. The script affects the files that live inside the directory it is run within.

现在,我还有一个自定义的shell脚本,该脚本在一个目录中执行某些操作,但是我需要像在另一个目录中一样调用上面提到的命令.

Now, I also have a custom shell script that does some things in one directory, but I need to call that command mentioned above as if it was in another directory.

您如何在Shell脚本中执行此操作?

How do you do this in a shell script?

推荐答案

为此,您可以使用cd内置,或pushdpopd内置.例如:

You can use the cd builtin, or the pushd and popd builtins for this purpose. For example:

# do something with /etc as the working directory
cd /etc
:

# do something with /tmp as the working directory
cd /tmp
:

您可以像任何其他命令一样使用内置命令,并且可以在脚本中随意更改目录上下文.

You use the builtins just like any other command, and can change directory context as many times as you like in a script.

这篇关于Bash如何在其他目录上下文中执行命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆