命令替换如何工作

安德烈(Andrej)

我正在阅读man bash手册页,第Special Parameters

   $      Expands to the process ID of the shell.  In a () subshell, it expands to the process ID of the cur‐
          rent shell, not the subshell.
   !      Expands to the process ID of the job most recently placed into the background, whether executed  as
          an asynchronous command or using the bg builtin (see JOB CONTROL below).

但是我不明白这是在说什么。有人可以在一个例子中解释一下吗?可以说我这样做:

mkdir -p /dir1/dir2/dir3/
cd !$

我最终进入目录

/dir1/dir2/dir3/

第二个命令(cd !$)的工作原理如何?

我是

$$并且$!是论点bash脚本可以使用。

这些参数会自动为您填充。

尝试这个

#!/bin/bash
echo "My own process id : $$"
ping -n 100 google.com &>/dev/null & 
# Here ping command is run in background -> see the & at the end.
# It detaches itself from the stdin and I have suppressed the output
# using the &>/dev/null
echo "Process id of the command last run in background : $!"

这给了我输出:

My own process id : 812
Process id of the command last run in background : 7608

知道后台命令的进程ID是一件好事,因为您稍后可以执行以下操作:

kill -9 7608 #killing the background process.

说完上述内容后,请执行以下操作:

cd $$

没有道理。可以?

但是,在终端中,如果您这样做了echo $$,您将获得bash与终端本身关联shell的进程ID 是的 !!爷爷

在此处输入图片说明

另外,您可以利用$!终端机中的通俗易懂的功能。

在此处输入图片说明

它为我提供了最后一个后台进程的进程ID,ls以及是否已完成的额外信息。太酷了 是不是

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章