Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
syntax:words [2015/08/13 03:25]
bill_thomson
syntax:words [2015/08/13 03:42] (current)
bill_thomson
Line 9: Line 9:
  
 ===== Splitting the commandline ===== ===== Splitting the commandline =====
-Bash scans the command line and splits it into words, usually to put the parameters you enter for a command into the right C-memory (the ''​argv''​ vector) to later correctly call the command. These words are recognized by splitting the commandline ​at the position of special ​characters, **Space** or **Tab** (manual defines them as **blanks**). +Bash scans the command line and splits it into words, usually to put the parameters you enter for a command into the right C-memory (the ''​argv''​ vector) to later correctly call the command. These words are recognized by splitting the command line at the special ​character position, **Space** or **Tab** (the manual defines them as **blanks**). 
-For example, take the echo program. It displays all its parameters separated by a space on the terminal. When you enter an echo command ​on your Bash prompt, Bash will do as descibed above: It will look for those special characters and use them to determinate the separate parameters.+For example, take the echo program. It displays all its parameters separated by a space. When you enter an echo command ​at the Bash prompt, Bash will look for those special charactersand use them to separate ​the parameters.
  
 You don't know what I'm talking about? I'm talking about this: You don't know what I'm talking about? I'm talking about this:
Line 19: Line 19:
  
 In other words, something you do (and Bash does) everyday. The characters where Bash splits the command line (SPACE, TAB i.e. blanks) are recognized as delimiters. In other words, something you do (and Bash does) everyday. The characters where Bash splits the command line (SPACE, TAB i.e. blanks) are recognized as delimiters.
-There is no null argument generated when you have 2 or more blanks in the commandline. **A sequence of more blank characters is treated as a single blank.**+There is no null argument generated when you have 2 or more blanks in the command line. **A sequence of more blank characters is treated as a single blank.**
 Here's an example: Here's an example:
  
Line 27: Line 27:
 </​code>​ </​code>​
  
-Bash splits the commandline ​at the blanks into words, then it calls echo with **each word as a separate ​argument**. In this example, echo is called with exactly 3 arguments: "''​Hello''",​ "''​little''"​ and "''​world''"​!+Bash splits the command line at the blanks into words, then it calls echo with **each word as an argument**. In this example, echo is called with three arguments: "''​Hello''",​ "''​little''"​ and "''​world''"​!
  
 __Does that mean we can't echo more than one Space?__ Of course not! __Does that mean we can't echo more than one Space?__ Of course not!
-Bash treats ​the blanks special, but there is a way to tell Bash to not treat them special: **Escaping** and **quoting**.+Bash treats blanks ​as special ​characters, but there are two ways to tell Bash not to treat them special: **Escaping** and **quoting**.
  
-Escaping a character means, to **take away its special ​role**. Bash will literally ​use an escaped character as text, even if it's a special one. Escaping is done by preceeding the character with a backslash:+Escaping a character means, to **take away its special ​meaning**. Bash will use an escaped character as text, even if it's a special one. Escaping is done by preceeding the character with a backslash:
  
 <​code>​ <​code>​
Line 39: Line 39:
 </​code>​ </​code>​
  
-None of the above spaces will be used to split the command line into words (one, in fact, the space between echo and the rest - it's used to determinate what is the command and what should be its arguments). Thus, echo is called with exaclty ​one parameter: "''​Hello ​               little ​           world''"​.+None of the escaped ​spaces will be used to perform word splitting. Thus, echo is called with one argument: "''​Hello ​               little ​           world''"​.
  
-And as we're all lazy and want an easy life, Bash has a mechanism to "//mass-escape//" ​a complete area of the commandline: **Quoting**. +Bash has a mechanism to "​escape" ​an entire string: **Quoting**. 
-In the context of commandline-splitting, which this section is about, it doesn'​t matter which kind of quoting you use: weak quoting or strong quoting, both mask the blanks we talked about to not be special:+In the context of command-splitting, which this section is about, it doesn'​t matter which kind of quoting you use: weak quoting or strong quoting, both cause Bash to not treat spaces as special ​characters:
  
 <​code>​ <​code>​
Line 53: Line 53:
 </​code>​ </​code>​
  
-__What is it all about now?__ Well, for example imagine a program that expects a filename as parameter, like cat. Filenames can have spaces in them:+__What is it all about now?__ Well, for example imagine a program that expects a filename as an argument, like cat. Filenames can have spaces in them:
  
 <​code>​ <​code>​
Line 71: Line 71:
 </​code>​ </​code>​
  
-Nothing special, as you usually ​enter that on the commandline ​with Tab-completition, that will take care of the spaces. But there'​s another splitting ​Bash does.+If you enter that on the command line with Tab completion, that will take care of the spaces. But Bash also does another type of splitting.
  
 ===== Word splitting ===== ===== Word splitting =====
  
-For a more technical ​view, please read the [[syntax:​expansion:​wordsplit | article about word splitting]]!+For a more technical ​description, please read the [[syntax:​expansion:​wordsplit | article about word splitting]]!
  
-The first kind of splitting is done by Bash to parse the commandline ​into separate tokens. This is what was described above, it's a pure **commandline ​parsing**.+The first kind of splitting is done to parse the command line into separate tokens. This is what was described above, it's a pure **command line parsing**.
  
-After the commandline ​has been split into words, Bash will perform ​expansions, if needed - variables that occur in the commandline ​need to be expanded (substituted by their value), for example. This is where the second type of word splitting comes in - several expansions ​finally ​undergo **word splitting** (but others do not).+After the command line has been split into words, Bash will perform ​expansion, if needed - variables that occur in the command line need to be expanded (substituted by their value), for example. This is where the second type of word splitting comes in - several expansions undergo **word splitting** (but others do not).
  
-Imagine you have a filename stored in a variable ​(variables will be explained elsewhere, just believe what you see):+Imagine you have a filename stored in a variable:
  
 <​code>​ <​code>​
Line 87: Line 87:
 </​code>​ </​code>​
  
-When this variable is used, its occurance will be substituted ​by its content.+When this variable is used, its occurance will be replaced ​by its content.
  
 <​code>​ <​code>​
Line 95: Line 95:
 </​code>​ </​code>​
  
-Though this is another step where spaces make a hard lifethe **quoting** is used again to influence it. Quotes also affect ​the word splitting:+Though this is another step where spaces make things difficult, **quoting** is used to work around the difficulty. Quotes also affect word splitting:
  
 <​code>​ <​code>​
Line 120: Line 120:
 </​code>​ </​code>​
  
-The parser will scan for blanks and mark the relevant words ("​splitting the commandline"):+The parser will scan for blanks and mark the relevant words ("​splitting the command line"):
  
-^Initial ​commandline ​splitting:​^^^^^^+^Initial ​command line splitting:​^^^^^^
 ^Word 1^Word 2^Word 3^Word 4^Word 5^Word 6^ ^Word 1^Word 2^Word 3^Word 4^Word 5^Word 6^
 |''​echo''​|''​The''​|''​file''​|''​is''​|''​named''​|''​$MYFILE''​| |''​echo''​|''​The''​|''​file''​|''​is''​|''​named''​|''​$MYFILE''​|
  
-A [[syntax:pe | parameter-/variable expansion]] is part of that commandline, Bash will perform the substitution,​ and the [[syntax:​expansion:​wordsplit | word splitting]] on the results:+A [[syntax:pe | parameter/​variable expansion]] is part of that command line, Bash will perform the substitution,​ and the [[syntax:​expansion:​wordsplit | word splitting]] on the results:
  
 ^Word splitting after substitution:​^^^^^^^ ^Word splitting after substitution:​^^^^^^^
Line 132: Line 132:
 |''​echo''​|''​The''​|''​file''​|''​is''​|''​named''​|''​THE''​|''​FILE.TXT''​| |''​echo''​|''​The''​|''​file''​|''​is''​|''​named''​|''​THE''​|''​FILE.TXT''​|
  
-Now let's imagine we quoted ​the ''​$MYFILE'',​ the commandline ​now looks like:+Now let's imagine we quoted ''​$MYFILE'',​ the command line now looks like:
 <​code>​ <​code>​
 echo The file is named "​$MYFILE"​ echo The file is named "​$MYFILE"​
Line 142: Line 142:
  
 ===== See also ===== ===== See also =====
-  * Internal: [[syntax:​quoting | Quoting and character-escaping]]+  * Internal: [[syntax:​quoting | Quoting and character escaping]]
   * Internal: [[syntax:​expansion:​wordsplit | Word splitting]]   * Internal: [[syntax:​expansion:​wordsplit | Word splitting]]
   * Internal: [[syntax:​expansion:​intro | Introduction to expansions and substitutions]]   * Internal: [[syntax:​expansion:​intro | Introduction to expansions and substitutions]]
  
   * External: [[http://​www.grymoire.com/​Unix/​Quote.html | Grymore: Shellquoting]]   * External: [[http://​www.grymoire.com/​Unix/​Quote.html | Grymore: Shellquoting]]
  • syntax/words.1439436333.txt
  • Last modified: 2015/08/13 03:25
  • by bill_thomson