我想创建一个命令来更改目录,在后台运行一个进程,然后返回到原始目录。从特定目录启动进程非常重要(它使用运行目录作为相对路径)。
我尝试运行它,但出现以下错误:
cd ~/work; myapp &> /dev/null &; cd -
-bash: syntax error near unexpected token `;'
Run Code Online (Sandbox Code Playgroud)
我可以运行以下任一命令。
# Without the "&" that cause the process to run in the background
cd ~/work; myapp &> /dev/null; cd -
# Without the " cd -" which returns my to the original directory
cd ~/work; myapp &> /dev/null &
Run Code Online (Sandbox Code Playgroud)
这样做的目的是能够将此命令添加到我的别名中。
在同一个子 shell 中运行cd命令myapp并且不需要返回cd:
( cd ~/work; myapp &>/dev/null ) &
Run Code Online (Sandbox Code Playgroud)
括号,(...),创建一个子 shell。您可以在子 shell 中自由更改目录 ( cd) 或更改环境,这不会对父 shell 产生影响。因此,事后无需cd返回。
让我们从目录开始/tmp/1:
$ pwd
/tmp/1
Run Code Online (Sandbox Code Playgroud)
现在,让我们cd在后台 shell 中运行示例命令,然后再次检查目录:
$ ( cd work; date &>/dev/null ) &
[1] 11942
$ pwd
/tmp/1
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1445 次 |
| 最近记录: |