The echo command is a built-in Linux feature that prints out arguments as the standard output1. The echo command can take various options and arguments to display different types of information. One of the arguments that can be used with the echo command is $$, which represents the process ID (PID) of the current shell2. A process ID is a unique number that identifies a running process in the system. The current shell is the shell that is executing the echo command. For example, if you are using the Bash shell and run the following command:
echo $$
The output will show the PID of the Bash shell, such as:
1234
The echo
commandcanbeusefultocheckwhichshellyouareusing,ortofindoutthePIDofthecurrentshellfordebuggingormonitoringpurposes.Theecho
command is different from the following commands:
echo $!: This command displays the PID of the last command executed in the background2. A background command is a command that runs without blocking the shell, allowing you to continue using the shell while the command executes. For example, if you run the following command:
sleep 10 &
This command will put the sleep command, which pauses the execution for 10 seconds, in the background. The output will show the PID of the sleep command, such as:
1 2345
If you then run the following command:
echo $!
The output will show the same PID of the sleep command, such as:
2345
echo $?: This command displays the exit status of the last command executed2. The exit status is a number that indicates whether the command was successful or not. A zero exit status means the command was successful, while a non-zero exit status means the command failed or encountered an error. For example, if you run the following command:
ls /home
This command will list the contents of the /home directory. If the command succeeds, the output will show the files and directories in the /home directory, such as:
alice bob charlie
If you then run the following command:
echo $?
The output will show the exit status of the ls command, which is zero, meaning the command was successful:
0
echo $0: This command displays the name of the current shell or script2. The name of the current shell is the name of the executable file that runs the shell, such as bash, zsh, ksh, etc. The name of the current script is the name of the file that contains the script, such as script.sh, script.py, etc. For example, if you are using the Bash shell and run the following command:
echo $0
The output will show the name of the current shell, such as:
bash
References: