A detailed description of Shell $$

Source: Internet
Author: User

On the surface it means: The current bash PID, but the various shell environments to understand thoroughly.


Official explanation:

Bashpid

Expands to the process ID of the current bash process. This differs from

$$ under certain circumstances, such as subshells that does not require bash

To is re-initialized.


Outputs the PID of the current bash process. This is different in the following case, such as not getting to bash's child shell, will be reinitialized.


Example 1:

[[email protected] ~]$ ps-ef |grep bash |grep-v grep

Andy 30245 30244 0 Jul17 pts/0 00:00:00-bash

[Email protected] ~]$ echo $$

30245


Note: $$ represents the process ID of the login shell (a shell that is directly logged in by an Andy user ssh connection)


[Email protected] ~]$

Example 2:

[Email protected] std]# ps-ef |grep Bash

Root 4422 4421 0 Jul23 pts/0 00:00:02-bash

Root 13791 4422 0 22:14 pts/0 00:00:00 grep--color=auto Bash

Andy 30245 30244 0 Jul17 pts/0 00:00:00-bash

[Email protected] std]# echo $$

4422



Note: $$ is the PID that logged into the interactive shell (I was Andy Login, then sudo su-to root)



Example 3:

[Email protected] std]# echo $$

4422

[email protected] std]# cat test.sh

#/bin/bash


echo "Fei Jiao hu Shi Shell ' S process pid:$$"

[Email protected] std]# sh test.sh

Fei Jiao hu Shi Shell ' S process pid:13858


Note: This is a non-interactive shell, and the shell,$$ that is executed by the script is this PID



Example 4:

[Email protected] ~]$ echo $$

30245

[Email protected] ~]$ echo "abc" | {echo "abc"; Echo $$;}

Abc

30245

[Email protected] ~]$



Note: The Subshell in the pipeline, although it is a child shell environment, but does not get the-bash process, so the parent process is still bashid. (This is a bit of a detour, look at the official explanation of the English language to slowly understand it.) )





This article is from the "learning communication, the first job ops" blog, please be sure to keep this source http://cuidehua.blog.51cto.com/5449828/1831910

A detailed description of Shell $$

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.