Difference Between “$$” Vs “$” in Bash Scripting [With Examples]

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.

Key Takeaways

  • 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.

Free Downloads

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.

Steps to Follow >

❶ At first, launch an Ubuntu Terminal.

❷ Write the following command to open a file in Nano:

nano back_process.sh
EXPLANATION
  • nano: Opens a file in the Nano text editor.
  • back_process.sh: Name of the file.

❸ 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"
EXPLANATION

This Bash script begins by capturing the Process ID (PID) of the current shell using the $$ parameter and stores it in the cpu_task_pid variable. It then proceeds to print a message to the console, displaying the PID along with the text “PID of CPU-intensive task” with the help of an echo command.

❹ 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
EXPLANATION
  • chmod: Changes the permissions of files and directories.
  • u+x: Here, u refers to the “user” or the owner of the file and +x specifies the permission being added, in this example, the “execute” permission. When u+x is added to the file permissions, it grants the user (owner) permission to execute (run) the file.
  • back_process.sh: File name.

❻ Run the script by the following command:

./back_process.sh

Managing Background Processes Using $$ ParameterUpon 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

You can follow the steps of Example  01 of section A, to save & make the script executable.

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"
EXPLANATION

First, the code creates a variable named temp_dir and assigns it a value, which is a directory path /home/miran/Desktop/my_temp_dir_$$. In this path, the $$ parameter represents the Process ID (PID) of the currently running shell, ensuring that the directory name is unique to each script execution. Next, the script utilizes the mkdir command to create a directory with the path stored in the temp_dir variable.

Now run the following command to get a list of files and folders in your current directory.

ls -l
EXPLANATION

The given ls -l command lists files and directories in the current directory in a long format. In this format, it provides detailed information about each file and directory, including permissions, owner, group, file size, modification date, and the name of the file or directory.

Then use the following command to execute the bash file and create a unique directory.

./temporary_directories.sh

Creating Unique Temporary Directories Using the $$ ParameterNow, 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.

You can follow the steps of Example  01 of section A, to save & make the script executable.

Script (accessing_value.sh) >

#!/bin/bash 

greeting="Hello, World!"
echo $greeting
EXPLANATION

The provided Bash code begins by declaring a variable named greeting and assigning it the string value “Hello, World!“. It then proceeds to echo command with a dollar sign ($) to print the value of the greeting variable. This dollar sign signifies that the value stored in the variable greeting should be substituted with its actual content when the script is executed. This results in the text “Hello, World!” being displayed in the terminal when the script is executed.

Run the following command to execute the bash file.

./accessing_value.sh

Accessing Variable Values with the Dollar SignAs 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:

You can follow the steps of Example  01 of section A, to save & make the script executable.

Script (command_substitution.sh) >

#!/bin/bash 

current_date=$(date)
echo "Today is: $current_date"
EXPLANATION

In the provided Bash script, the date command is executed within command substitution, denoted by $(date). This captures the current date and time and assigns it to the current_date variable. Subsequently, the script uses the echo command to print the text “Today is:” followed by the value of the current_date variable, which contains the current date and time, to the standard output.

Use the following command to run the bash file in your command line.

./command_substitution.sh

Executing Command Substitution with the Dollar SignUpon 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:

You can follow the steps of Example  01 of section A, to save & make the script executable.

Script (arithmetic_expansion.sh) >

#!/bin/bash

x=5
y=3
result=$((x + y))
echo "Result: $result"
EXPLANATION

In the provided Bash script, two variables, x and y, are assigned the values 5 and 3, respectively. To perform addition, the result variable is defined using arithmetic expansion as $((x + y)), which calculates the sum of x and y. In this case, the result will hold the value 8, which is the result of adding 5 and 3. Finally, the echo command displays the text “Result:” followed by the value of the result variable.

Run the following code to execute the bash file.

./arithmetic_expansion.sh

Implementing Arithmetic Expansion with the Dollar Sign

Upon execution, the code returns Result: 8 as the standard output of the given code.

Conclusion

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

When to use $@ vs $* in bash?
Use $@ when you want to treat each commandline argument as a separate entity with proper quoting, preserving spaces within arguments. Use $* when you want to treat all arguments as a single entity, often useful for passing them as a single string to a command.
What is the difference between $() and $[] in Bash script?
In Bash scripting, $() is used for command substitution, where the output of a command is captured and used as a value, while ${} is used for parameter expansion, allowing you to manipulate variables and their values.
What is difference between $$ and $!?
$$ represents the process ID of the current shell, whereas $! represents the process ID of the last executed background command.
What does 0 >& 1 mean?
0 > & 1 in Bash represents redirecting standard input (file descriptor 0) to standard output (file descriptor 1). It effectively makes the output of a command (stdout) the same as the input (stdin), often used for piping or combining the two streams.

Related Articles


<< Go Back to Parameters in Bash Scripting | Bash Scripting Tutorial

4.8/5 - (39 votes)
Mohammad Shah Miran

Hey, I'm Mohammad Shah Miran, previously worked as a VBA and Excel Content Developer at SOFTEKO, and for now working as a Linux Content Developer Executive in LinuxSimply Project. I completed my graduation from Bangladesh University of Engineering and Technology (BUET). As a part of my job, i communicate with Linux operating system, without letting the GUI to intervene and try to pass it to our audience.

Leave a Comment