In Bash scripting, the symbols $$ and $ serve distinct purposes. $$ is a special type of parameter that represents the process ID (PID) of the current shell, providing a unique identifier for the script’s execution. On the other hand, the single dollar sign $ in Bash signifies variable substitution. It accesses the value stored in a variable. In this article, I will look into different examples to understand the difference between $$ vs $ in bash scripting. So Let’s Start.
- Learning the difference between $$ vs $ in bash script.
- Getting familiar with how $$ and $ can be incorporated to manage background processes, create unique IDs, do arithmetic operations, and so on.
A. Understanding the Role of $$ Parameter in Bash Script
To give an illustration of the difference between $$ vs $ in the bash script, I will discuss the $$ parameters in the bash. As mentioned previously, the $$ parameter represents the Process ID (PID) of the currently running shell and offers a unique identifier for the script’s execution. This PID can be invaluable for background processes, temporary file creation, or process tracking. Let’s have some examples regarding the matter.
Example 01: Managing Background Processes Using $$ Parameter in Bash
As the $$ parameter represents the Process ID, I am demonstrating a bash code below. This code is valuable for monitoring, managing, or even terminating background processes when necessary.
❶ At first, launch an Ubuntu Terminal.
❷ Write the following command to open a file in Nano:
❸ Copy the script mentioned below:
#!/bin/bash # Launch a CPU-intensive task in the background cpu_task_pid=$$ echo "PID of CPU-intensive task: $cpu_task_pid"
❹ Press CTRL+O and ENTER to save the file; CTRL+X to exit.
❺ Use the following command to make the file executable:
chmod u+x back_process.sh
❻ Run the script by the following command:
Upon execution, the code returns 44983 as the process ID, specific to the session when the code was executed.
Example 02: Creating Unique Temporary Directories Using the $$ Parameter
You can also employ the $$ parameter to create a unique directory in your distribution. In this example, $$ appends the PID to the temporary directory name, ensuring its uniqueness and preventing conflicts with other processes
Script (temporary_directories.sh) >
#!/bin/bash # Generate a temporary directory name using the current shell's PID temp_dir="/home/miran/Desktop/my_temp_dir_$$" mkdir "$temp_dir"
Now run the following command to get a list of files and folders in your current directory.
Then use the following command to execute the bash file and create a unique directory.
Now, Upon execution of the ls -l command, I have one visible file in my current directory. Next, as the image shows above, the temporary_directories.sh file creates a folder named my_temp_dir_42794 and if you want to run the file one more time, you will notice there is one more folder in the directory. Therefore it reinforces the idea that you will get a new folder each time you execute our bash file in the command line.
B. The Role of $ Syntax in Bash Scripts
Following the previous section to illustrate the difference between $$ vs $ in bash script, I will talk about the role of $ syntax. Different from the previously discussed command, the single dollar sign ($) in Bash signifies variable substitution. It is used to access the value stored in a variable. Similarly, you can incorporate $ syntax for command substitution or arithmetic expansion. In the following section, I will discuss all of them to give you an overall idea. So let’s jump into it.
Example 01: Accessing Variable Values in Bash Script with the $(Dollar) Sign
First of all, the simplest task a dollar sign ($) can do is, access the variable value, in other words, substitute the variable with its stored value. Here a simple bash code has been demonstrated to illustrate the idea of it. Check it out.
Script (accessing_value.sh) >
#!/bin/bash greeting="Hello, World!" echo $greeting
Run the following command to execute the bash file.
As expected the bash file returns the value stored in the greeting variable, “Hello World!” to the command line.
Example 02: Executing Command Substitution with the $(Dollar) Sign in Bash Script
Command substitution in Bash is a powerful feature that allows you to capture the output of a command and use it as a value within your script. This is done using the dollar sign ($) followed by parentheses, like this: $(command).
For example, consider the date command. If you want to capture the current date and time and assign it to a variable, you can use command substitution as follows:
Script (command_substitution.sh) >
#!/bin/bash current_date=$(date) echo "Today is: $current_date"
Use the following command to run the bash file in your command line.
Upon Execution, the code returns the current date and time at the time of its execution.
Example 03: Implementing Arithmetic Expansion with the $(Dollar) Sign
Arithmetic expansion in Bash allows you to perform mathematical calculations and use the result within your script. This is accomplished using the dollar sign ($) followed by double parentheses, like this: $((expression)).
For example, if you want to add two numbers, you can use arithmetic expansion like this:
Script (arithmetic_expansion.sh) >
#!/bin/bash x=5 y=3 result=$((x + y)) echo "Result: $result"
Run the following code to execute the bash file.
Upon execution, the code returns Result: 8 as the standard output of the given code.
In conclusion, understanding the crucial distinction between $$ vs. $ in Bash scripting is fundamental for effective script development. As you have already learned $$ parameter represents the process ID of the current shell, serving purposes like creating unique filenames or managing background processes. On the other hand, $ is a syntax that is used to access variable values, execute command substitution, perform arithmetic expansion, etc. However, If you have any questions related to the topic of the discussion, feel free to comment below. Thank You.
People Also Ask
- How to Pass All Parameters in Bash Scripts? [6 Cases]
- How to Use Positional Parameters in Bash Script? [2 Examples]
- Parsing Parameters in Bash Scripts [5 Examples]
- 4 Methods to Pass Named Parameters in a Bash Script
- What is $0 in Bash Script? [4 Practical Examples]
- How to Use Alias with Parameters in Bash Scripting? [6 Examples]
- How to Use Bash Function with Parameters? [6 Examples]