Bash 4.4 提示转义当前作业数 运行

Bash 4.4 prompt escape for number of jobs currently running

我偶然发现 this post where user chepner proposed in his answer the usage of \j (as mentioned in the bash manual) 来检索当前的 运行 后台作业计数。 基本上可以归结为

num_jobs="\j"
echo ${num_jobs@P}

谁能告诉我这里到底发生了什么?例如。

与任何参数扩展一样,您必须提供参数名称,而不是任意字符串。 \j 不是参数名称;这是您要从参数扩展中获得的文本。

参数展开后,@P进一步对结果进行提示展开,从而\j被职位数代替。

$ num_jobs="\j"
$ echo "${num_jobs}"
\j
$ echo "${num_jobs@P}"
0

@ 之前的部分是您要扩展的参数的名称,它不能是您想以某种方式修改的字符串。而@P是Bash 4.4中引入的参数展开(见manual):

${<i>parameter</i>@<i>operator</i>}

The expansion is either a transformation of the value of parameter or information about parameter itself, depending on the value of operator. Each operator is a single letter:

P

The expansion is a string that is the result of expanding the value of parameter as if it were a prompt string (see Controlling the Prompt).