Command substitution (Bash): verschil tussen versies

Uit De Vliegende Brigade
Naar navigatie springen Naar zoeken springen
 
(6 tussenliggende versies door dezelfde gebruiker niet weergegeven)
Regel 1: Regel 1:
 
In Bash-programming kom je al snel de volgende expressies tegen:
 
In Bash-programming kom je al snel de volgende expressies tegen:
  
* <code>`...`</code> - ''Backticks
+
* <code>`...`</code> - ''Backticks command substitution''
* <code>$(...)</code> - ''Parenthesis''
+
* <code>$(...)</code> - ''Parenthesis command substitution''
* <code>${...}</code> - ''Curly braces''.
+
* <code>${...}</code> - ''Curly braces substitution'' of ''Parameter expansion''.
  
Dit zijn drie voorbeelden van ''command substitution'' (misschien is dit onzorgvuldig geformuleerd, en geldt deze naam alleen voor backticks)
+
Dit zijn alledrie voorbeelden van ''substitution'' of ''expansion''. Het betekent zoiets als ''Evalueer eerst de expressie hierbinnen''.
 +
 
 +
Om het leven eenvoudig te maken:
 +
 
 +
* ''Backtics'' zijn achterhaald binnen Bash (ze zijn echter nog steeds onderdeel van de POSIX-standaard). Daarnaast zijn ze lastiger te gebruiken en kun je ze bv. niet ''nesten''
 +
* ''Curly braces'' zijn bedoeld voor [[Parameter Substitution (Bash) | parameter substitution]] (wo/ find-&-replace) - Niet het onderwerp van dit artikel
 +
 
 +
Gebruik daarom ''parenthesis'' ''command substitution'' - De rest van dit hoofdstuk is hiertoe beperkt, tenzij anders aangegeven.
 +
 
 +
<code>man bash</code> zegt vanaf regel 1.332 het volgende over ''command substitution'':
 +
 
 +
<pre>
 +
Command substitution allows the output of a command to replace the command name.  There are two forms:
 +
 
 +
 
 +
      $(command)
 +
or
 +
      `command`
 +
 
 +
Bash performs the expansion by executing command in a subshell environment and replacing the command substi‐
 +
tution  with  the standard output of the command, with any trailing newlines deleted.  Embedded newlines are
 +
not deleted, but they may be removed during word splitting.  The command substitution $(cat file) can be re‐
 +
placed by the equivalent but faster $(< file).
 +
 
 +
When the old-style backquote form of substitution is used, backslash retains its literal meaning except when
 +
followed by $, `, or \.  The first backquote not preceded by a backslash terminates  the  command  substitu‐
 +
tion.  When using the $(command) form, all characters between the parentheses make up the command; none are
 +
treated specially.
 +
 
 +
Command substitutions may be nested.  To nest when using the backquoted form, escape  the  inner  backquotes
 +
with backslashes.
 +
 
 +
If the substitution appears within double quotes, word splitting and pathname expansion are not performed on
 +
the results.
 +
</pre>
 +
 
 +
== Zie ook ==
 +
 
 +
* [[Parameter Substitution (Bash)]]
 +
 
 +
== Bronnen ==
 +
 
 +
* https://stackoverflow.com/questions/22709371/backticks-vs-braces-in-bash
 +
* https://www.gnu.org/software/bash/manual/html_node/Command-Substitution.html

Huidige versie van 1 aug 2021 om 19:42

In Bash-programming kom je al snel de volgende expressies tegen:

  • `...` - Backticks command substitution
  • $(...) - Parenthesis command substitution
  • ${...} - Curly braces substitution of Parameter expansion.

Dit zijn alledrie voorbeelden van substitution of expansion. Het betekent zoiets als Evalueer eerst de expressie hierbinnen.

Om het leven eenvoudig te maken:

  • Backtics zijn achterhaald binnen Bash (ze zijn echter nog steeds onderdeel van de POSIX-standaard). Daarnaast zijn ze lastiger te gebruiken en kun je ze bv. niet nesten
  • Curly braces zijn bedoeld voor parameter substitution (wo/ find-&-replace) - Niet het onderwerp van dit artikel

Gebruik daarom parenthesis command substitution - De rest van dit hoofdstuk is hiertoe beperkt, tenzij anders aangegeven.

man bash zegt vanaf regel 1.332 het volgende over command substitution:

Command substitution allows the output of a command to replace the command name.  There are two forms:


       $(command)
or
       `command`

Bash performs the expansion by executing command in a subshell environment and replacing the command substi‐
tution  with  the standard output of the command, with any trailing newlines deleted.  Embedded newlines are
not deleted, but they may be removed during word splitting.  The command substitution $(cat file) can be re‐
placed by the equivalent but faster $(< file).

When the old-style backquote form of substitution is used, backslash retains its literal meaning except when
followed by $, `, or \.  The first backquote not preceded by a backslash terminates  the  command  substitu‐
tion.   When using the $(command) form, all characters between the parentheses make up the command; none are
treated specially.

Command substitutions may be nested.  To nest when using the backquoted form, escape  the  inner  backquotes
with backslashes.

If the substitution appears within double quotes, word splitting and pathname expansion are not performed on
the results.

Zie ook

Bronnen