$$ 在shell中是什么意思?

问题描述:

我曾经读到过,在 shell 中为临时文件获取唯一文件名的一种方法是使用双美元符号 ($$).这确实会产生一个不时变化的数字……但是如果您重复调用它,它会返回相同的数字.(解决办法就是利用时间.)

I once read that one way to obtain a unique filename in a shell for temp files was to use a double dollar sign ($$). This does produce a number that varies from time to time... but if you call it repeatedly, it returns the same number. (The solution is to just use the time.)

我很想知道 $$ 究竟是什么,以及为什么建议将其作为生成唯一文件名的一种方式.

I am curious to know what $$ actually is, and why it would be suggested as a way to generate unique filenames.

在 Bash 中 $$ 是进程 ID,如注释中所述,将其用作临时文件名是不安全的各种原因.

In Bash $$ is the process ID, as noted in the comments it is not safe to use as a temp filename for a variety of reasons.

对于临时文件名,请使用 mktemp 命令.

For temporary file names, use the mktemp command.