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

4

I have seen some code examples in BASH where $() is used to execute a command.

For example:

 sudo chown -R $(whoami) .

What does this dollar sign followed by the command encapsulated by the parenthesis, ie $(comando) ?

    
asked by anonymous 05.09.2018 / 16:40

3 answers

6

Command Substitution $ ()

$() has a function very similar to shell used: ('') .

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

This process is done in a subshell , which is a "child process" released by shell, causing it to run a parallel process, running subtasks or subprocesses simultaneously.

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

  

"first do what is between $() and then evaluate what is in 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 nested command in $() , simply use $() for each command you want to do, not worrying about 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 from the part of command nesting , it suffers from ambiguity and "gets in the way" of executing the command, because each chase may be opening or closing.

Example:

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

To execute commands with it, you must 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 also find it in a arithmetic expression to calculate a certain value , but you will need to use two opening parentheses and two closing parentheses instead of a single one.

Example:

echo $((1 + 2)) # retorna 3
    
05.09.2018 / 19:22
2

Very similar to crase ''.

It is called command substitution (posix specification ) and run the command on a subshell. The command between $() or crase ('') runs on a subshell and the output is placed on 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 ciphers, $() can be "recursive", you can use one inside the other.

For example:

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

Free translation from here :)

    
05.09.2018 / 16:52
2

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

What would a subshell be?

Subshell are subprocesses created from a parent script (parent process) to run in parallel, this processing happens in the background as the subshell is a nearly identical copy of the parent process. In the case of multi-core processors the operating system will distribute subshells tasks between processor cores.

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

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

Subshell example 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 runs in a subshell:

ls -lah | grep php
  

Reference: link

    
05.09.2018 / 16:50