What is the $() [dollar sign followed by parentheses] for in BASH?

Asked

Viewed 738 times

5

I’ve seen some examples of code in the BASH where it’s used $() to be able to execute a command.

For example:

 sudo chown -R $(whoami) .

What this means followed by the enclosed command in parentheses, i.e. $(comando)?

3 answers

6


Command Substitution $()

The $() has a function very similar to crase used in shell: (``).

The $(), called Command Substitution allows a command to be executed and has an output, i.e., it is executed and returns the result of that command.

This process is done in a subshell, which is a "child process" launched by the shell, causing a parallel process to be executed, running subtasks or subprocesses simultaneously.

It is usually used to return a value, to be used in the main command it is called, having the meaning of:

"first do what’s between $() and then evaluate what’s on the rest of the line".

Example 1:

#!/bin/bash
resultado=$(uname -m)
if [ $resultado = "x86_64" ]; then
  echo "sua versão é de 64bits"
else
  echo "sua versão é de 32bits"
fi
#retorna sua versão é de 32 bits

Example 2:

echo "Hoje é $(date)." #retorna Hoje é Qua Set 5 13:40:50 - 03 2018

Example 3:

ls $(pwd)/Documentos/ #retorna /home/debian/Documentos/Projetos

When you want to execute a command nested in $(), just go using $() for each command you want to do, not worrying about the escape character, since the parentheses define this.

Example:

echo $(uname -m)$(echo $(pwd)$(echo $(whoami))) #retorna i686/home/debiandebian

Command Substitution ``

The crase, which is also a command substitution, differs in the part of command nesting, it suffers from ambiguity and "gets confused" in the execution of the command, because each crase may be being opened or being closed.

Example:

echo `pwd `whoami`` #retorna /home/debian/whoami

To execute commands with it, you need to use the backslash \ to escape the commands and run together, which for code readability and execution, becomes much more complex than using $().

Example:

echo `echo \`pwd\`\`whoami\`` #retorna /home/debian debian

$() in arithmetic expressions

You can find it too in a arithmetic expression, to calculate a certain value, but you will need to use two opening brackets and two closing brackets instead of a single.

Example:

echo $((1 + 2)) # retorna 3
  • It was one of the best answers so far. + 1

2

Dollar sign followed by parentheses $() is one of the ways to execute parallel commands in BASH using subshell, returning the result of the execution to the parent command.

What would be a subshell?

Subshell are subprocesses created from a main script (parent process) to run in parallel, this processing happens in the background as the subshell being an almost identical copy of the parent process. In the case of processors with multiple cores the operating system will do the distribution of subshells tasks between the processor cores.

That is, a subshell is a process child of a shell.

There are other ways to use subshell in Bash, one of the most common is the commands preceded by Pipes | every command that comes after a pipe is executed in a subshell.

Example of subshell using dollar sign and parentheses:

sudo apt-get --reinstall install linux-headers-$(uname -r)

Example of subshell using pipe:

In this example the grep command after the pipe | is executed in a subshell:

ls -lah | grep php

Reference: http://aurelio.net/shell/pocketknife/#blocks

  • 1

    I added an example to the answer to make it more didactic. If you don’t agree with editing, consider reversing it from the history.

  • A subshell would be a way of executing the command between parentheses $(comando) in another parallel process. With this we can create several tasks running in parallel in the background, returning only the final result to the parent process.

2

Very similar to crase ``.

Is called command substitution (Posix Specification) and executes the command in a subshell. The command between the $() or crase (``) is executed in a subshell and the output is placed in the original command.

# Setting a variable to the contents of a text file.
File_contents1=$(cat $file1) 
File_contents2=$(<$file2) # Bash permits this also.

Unlike the crases, the $() can be "recursive", you can use one inside the other.

For example:

word_count=$( wc -w $(echo * | awk '{print $8}') )

Free translation from here :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.