User Tools

Site Tools


bash:quotes

Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
bash:quotes [2019/12/07 12:00] peterbash:quotes [2020/07/15 09:30] (current) – external edit 127.0.0.1
Line 51: Line 51:
 ---- ----
  
-===== Concatinating Quotes =====+==== Escaping Quotes ==== 
 + 
 +Putting a **backslash** character **\** in front of a quote removes its special meaning.  
 + 
 +This works inside double quotes, or in the absence of quotes. 
 + 
 +It does not work inside single quotes.  
 + 
 +==== Example ==== 
 + 
 +<code bash> 
 +echo 'Don'\''t walk!' 
 +echo "Don't walk!" 
 +echo $'Don\'t talk!' 
 +</code> 
 + 
 +  * **1st line:**  A single quoted string that can contain anything other than single-quotes.  In this case, an escaped, unquoted single-quote is concatenated with the argument between two single quoted strings. 
 +  * **2nd line:**  A double-quoted string with all expansions and double-quotes within escaped. 
 +  * **3rd line:**  A less portable equivalent using $'...': 
 + 
 +**$(...)**-style command substitutions are unique in that the quoting of their contents is completely independent to their surroundings.  This means you don't have to worry about nested quote escaping problems. 
 + 
 +---- 
 + 
 +===== Concatenating Quotes =====
  
 The various types of quotes can be combined, or concatenated, if needed. The various types of quotes can be combined, or concatenated, if needed.
Line 106: Line 130:
  
 ---- ----
 +
 +===== Prevent field splitting and ignore glob pattern characters =====
 +
 +<code bash>
 +cp $file $destination         # WRONG
 +cp -- "$file" "$destination"  # Right
 +</code>
 +
 +In this example, the double quotes protect the value of each parameter from undergoing word splitting or globbing should it happen to contain whitespace or wildcard characters (* or ? or [...]).
 +
 +Without the quotes, a filename like hot stuff.mp3 would be split into two words, and each word would be passed to the cp command as a separate argument.  or, a filename that contains * with whitespace around it would produce one word for every file in the current directory.  That is not what we want.
 +
 +With the quotes, every character in the value of the filename parameter is treated literally, and the whole value becomes the second argument to the cp command.
 +
 +<WRAP info>
 +When in doubt, always double-quote your parameter expansions.
 +</WRAP>
 + 
 +
 +
 +----
 +
  
 ===== Example - using back quotes within single quotes ===== ===== Example - using back quotes within single quotes =====
bash/quotes.1575720054.txt.gz · Last modified: 2020/07/15 09:30 (external edit)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki