Bash Scripting
  • scripting list
  • How to Make Bash Script Executable Using Chmod
  • Shell Script to Check Linux Server Health
  • How to Concatenate String Variables in Bash [Join Strings]
  • How to Do Bash Variable Substitution (Parameter Substitution)
  • Bash Parameter Expansion with Cheat Sheet
  • Bash getopts with Examples
  • How to Pass all Arguments in Bash Scripting
  • Bash Function Return Value
  • Bash Loop Through Lines in a File
  • Bash readarray with Examples
  • Bash let with Examples
  • Bash expr with Examples
  • Bash read password
  • Bash for Loop Range Variable
  • Bash Arrays Explained
  • Ways to Find Bash Array Length
  • Bash Split String by Delimiter
  • Bash if Options
  • Bash If Statements for String Comparison
  • Debugging Techniques for Bash Scripts
  • Determining if a Bash String is Empty
  • Bash if Statement with Multiple Conditions
  • Meaning of Shebang in Bash Scripts
  • How to Comment Code in Bash Script
  • How to Read CSV File in Bash
  • Bash Scripting: How to Check if File Exists
  • Bash If Else Statements: Examples and Syntax
  • Bash Scripting: How to Check if Directory Exists
  • Bash eval Command with Examples
  • How to Use Sleep Command in Bash Scripting
  • Bash Associative Arrays with Examples
  • Bash Script for Counting Lines in a File
  • How to Use While Loop in Bash for Efficient Scripting
  • Bash basename Command with Examples
  • How to Create Multiline Strings in Bash
  • How to Use Bash if With && Operator
  • 50 Bash Script Examples to Kickstart Your Learning
  • Case statement in Bash Shell Scripting
  • Trimming White Space in Bash
  • How to Extract Filename from Absolute Path in Bash
  • How to Get Directory Path in Bash
  • Extract Extension from File Path in Bash
  • Extract Filename without Extension from Full Path in Bash
  • Bash for Each File in a Directory
  • Bash for Loop with Array
  • Bash Continue – Using with for Loop
  • Bash Backticks vs Dollar Parentheses $()
  • How to Assign Variable in Bash
  • How to Assign Variable in Bash
  • Bash Division Explained
  • Bash Modulo (Division Remainder)
  • Bash While Read Line by Line
  • Bash shift Command
  • Bash Looping Through Array of Strings
  • Bash read Command with Examples
  • Bash Check Empty Array
  • Using Bash For Loops to Iterate Over a List of Strings
  • Bash Break – Using with For Loop
  • How to Use seq With for Loop in Bash
  • How to Use $@ in Bash Scripting
  • Get the Current Script Directory in Bash
Powered by GitBook
On this page
  • Bash backticks
  • Dollar Parentheses $()
  • Backticks vs Dollar Parentheses $()
  • Conclusion

Bash Backticks vs Dollar Parentheses $()

PreviousBash Continue – Using with for LoopNextHow to Assign Variable in Bash

Last updated 1 year ago

There are two forms of command substitution which you can use in bash. One is with dollar-parentheses and the other is using backticks. The command enclosed by $() syntax will not work with the old bourne shell. The original Bourne shell only used to support backticks. However, all shells now support backticks as well as dollar-parentheses.

Bash backticks

Backticks, also referred to as the back quote (`) character, allows you to store the output of a command in a variable. With backticks you can run the command in the system and assign the output to continue the logic. Backticks act as a bridge between two commands, which means that the second command’s action depends upon the first one. Many feel that the backtick operators have been deprecated. However, this is not true. The backtick operator is available in bash, and many coders use it. It can get complicated if backticks are nested where the internal commands also make use of the backticks.

Let’s look at the following example:

#!/bin/bashlist=`ls`count=`ls -1 | wc -l`dateToday=`date`echo [$dateToday] has $count entries and they are: $listecho [`date`] also has `ls -1 | wc -l` entries and they are: `ls` sum=`expr 5 + 2`echo Sum is $sum

In this example, we are declaring variables and assigning the output of each of those commands to a variable. We can also use the results of the commands surrounded by backticks in other commands as shown above.

Dollar Parentheses $()

The dollar-parentheses is a popular command substitution and applies to all POSIX-conformant shells. The command in the braces of $() is executed in a subshell and the output is then placed in the original command.

Example:

#!/bin/bashlist=$(ls)count=$(ls -1 | wc -l)dateToday=(date)echo [$dateToday] has $count entries and they are: $listecho [$(date)] also has $(ls -1 | wc -l) entries and they are: $(ls) sum=$((5+2))echo Sum is $sumecho sum+count is $(($sum+$count))

As we can see above, variables are getting assigned the values on the basis of the output of the respective commands. These variables can be processed as per our needs. The result of $() can also be directly used in other commands

Backticks vs Dollar Parentheses $()

Backticks ( ` ) have a visual disadvantage as they can be easily confused by single quotes (‘). With newer shells, both backticks and dollar-parentheses are equivalent. Within backticks the backslash quotes are evaluated before other kinds of internal quotes are applied. $() operators are much more convenient to use when you need to nest multiple commands. The piece of code inside the $ parentheses operator is considered as a bash command.

Example:

#!/bin/bashpath=/world/country/asia/japan/tokyo echo "-----With dollar parentheses-----" echo $(basename $(dirname $(dirname $path))) echo "-----With backticks-----" echo `basename \`dirname \\\`dirname $path \\\`\``

As you can see, both backticks and dollar parentheses operator display the same output. However, with the amount of escaping needed with backticks, it makes the task challenging and confusing. Nesting backtick operators inside other backtick operators require escaping and work if the nested back quotes are escaped. On the contrary, the dollar parentheses operator is readable as well as understandable.

Conclusion

  • Both backticks and dollar-parentheses operators are equivalent and referred to as command substitution operators.

  • Dollar-parentheses operators are convenient to use.

  • Nesting backtick operators inside other backtick operators require escaping, making it visually confusing.

Backticks vs Dollar Parentheses
bash Dollar Parentheses $() example
Bash backticks example