File: escapingsection.html

package info (click to toggle)
abs-guide 10-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 6,952 kB
  • sloc: sh: 14,129; makefile: 81
file content (983 lines) | stat: -rw-r--r-- 19,387 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML
><HEAD
><TITLE
>Escaping</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+
"><LINK
REL="HOME"
TITLE="Advanced Bash-Scripting Guide"
HREF="index.html"><LINK
REL="UP"
TITLE="Quoting"
HREF="quoting.html"><LINK
REL="PREVIOUS"
TITLE="Quoting"
HREF="quoting.html"><LINK
REL="NEXT"
TITLE="Exit and Exit Status"
HREF="exit-status.html"><META
HTTP-EQUIV="Content-Style-Type"
CONTENT="text/css"><LINK
REL="stylesheet"
HREF="common/kde-common.css"
TYPE="text/css"><META
HTTP-EQUIV="Content-Type"
CONTENT="text/html; charset=iso-8859-1"><META
HTTP-EQUIV="Content-Language"
CONTENT="en"><LINK
REL="stylesheet"
HREF="common/kde-localised.css"
TYPE="text/css"
TITLE="KDE-English"><LINK
REL="stylesheet"
HREF="common/kde-default.css"
TYPE="text/css"
TITLE="KDE-Default"></HEAD
><BODY
CLASS="SECT1"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#AA0000"
VLINK="#AA0055"
ALINK="#AA0000"
STYLE="font-family: sans-serif;"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="quoting.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Chapter 5. Quoting</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="exit-status.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="ESCAPINGSECTION"
></A
>5.2. Escaping</H1
><P
><A
NAME="ESCP"
></A
><I
CLASS="FIRSTTERM"
>Escaping</I
> is a method
	of quoting single characters. The <SPAN
CLASS="TOKEN"
>escape</SPAN
>
	(<SPAN
CLASS="TOKEN"
>\</SPAN
>) preceding a character tells the shell to
	interpret that character literally.</P
><DIV
CLASS="CAUTION"
><TABLE
CLASS="CAUTION"
WIDTH="100%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="common/caution.png"
HSPACE="5"
ALT="Caution"></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
>With certain commands and utilities, such as <A
HREF="internal.html#ECHOREF"
>echo</A
> and <A
HREF="sedawk.html#SEDREF"
>sed</A
>, escaping a character may have the
	opposite effect - it can toggle on a special meaning for that
	character.</P
></TD
></TR
></TABLE
></DIV
><DIV
CLASS="VARIABLELIST"
><P
><B
><A
NAME="SPM"
></A
>Special meanings of certain
	escaped characters</B
></P
><DL
><DT
>used with <B
CLASS="COMMAND"
>echo</B
> and
	<B
CLASS="COMMAND"
>sed</B
></DT
><DD
><P
></P
></DD
><DT
><SPAN
CLASS="TOKEN"
>\n</SPAN
></DT
><DD
><P
>means newline</P
></DD
><DT
><SPAN
CLASS="TOKEN"
>\r</SPAN
></DT
><DD
><P
>means return</P
></DD
><DT
><SPAN
CLASS="TOKEN"
>\t</SPAN
></DT
><DD
><P
>means tab</P
></DD
><DT
><SPAN
CLASS="TOKEN"
>\v</SPAN
></DT
><DD
><P
> means vertical tab</P
></DD
><DT
><SPAN
CLASS="TOKEN"
>\b</SPAN
></DT
><DD
><P
>means backspace</P
></DD
><DT
><SPAN
CLASS="TOKEN"
>\a</SPAN
></DT
><DD
><P
>means <I
CLASS="FIRSTTERM"
>alert</I
> (beep or flash)</P
></DD
><DT
><SPAN
CLASS="TOKEN"
>\0xx</SPAN
></DT
><DD
><P
><A
NAME="OCTALREF"
></A
>translates to the
              octal <A
HREF="special-chars.html#ASCIIDEF"
>ASCII</A
>
              equivalent of <TT
CLASS="REPLACEABLE"
><I
>0nn</I
></TT
>, where
              <TT
CLASS="REPLACEABLE"
><I
>nn</I
></TT
> is a string of digits</P
><DIV
CLASS="IMPORTANT"
><TABLE
CLASS="IMPORTANT"
WIDTH="90%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="common/important.png"
HSPACE="5"
ALT="Important"></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
><A
NAME="STRQ"
></A
></P
><P
>The <TT
CLASS="USERINPUT"
><B
>$' ... '</B
></TT
>
             <A
HREF="quoting.html#QUOTINGREF"
>quoted</A
> string-expansion
              construct is a mechanism that uses escaped octal or hex values
	      to assign ASCII characters to variables, e.g.,
	      <B
CLASS="COMMAND"
>quote=$'\042'</B
>.</P
></TD
></TR
></TABLE
></DIV
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="ESCAPED"
></A
><P
><B
>Example 5-2. Escaped Characters</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>   1&nbsp;#!/bin/bash
   2&nbsp;# escaped.sh: escaped characters
   3&nbsp;
   4&nbsp;#############################################################
   5&nbsp;### First, let's show some basic escaped-character usage. ###
   6&nbsp;#############################################################
   7&nbsp;
   8&nbsp;# Escaping a newline.
   9&nbsp;# ------------------
  10&nbsp;
  11&nbsp;echo ""
  12&nbsp;
  13&nbsp;echo "This will print
  14&nbsp;as two lines."
  15&nbsp;# This will print
  16&nbsp;# as two lines.
  17&nbsp;
  18&nbsp;echo "This will print \
  19&nbsp;as one line."
  20&nbsp;# This will print as one line.
  21&nbsp;
  22&nbsp;echo; echo
  23&nbsp;
  24&nbsp;echo "============="
  25&nbsp;
  26&nbsp;
  27&nbsp;echo "\v\v\v\v"      # Prints \v\v\v\v literally.
  28&nbsp;# Use the -e option with 'echo' to print escaped characters.
  29&nbsp;echo "============="
  30&nbsp;echo "VERTICAL TABS"
  31&nbsp;echo -e "\v\v\v\v"   # Prints 4 vertical tabs.
  32&nbsp;echo "=============="
  33&nbsp;
  34&nbsp;echo "QUOTATION MARK"
  35&nbsp;echo -e "\042"       # Prints " (quote, octal ASCII character 42).
  36&nbsp;echo "=============="
  37&nbsp;
  38&nbsp;
  39&nbsp;
  40&nbsp;# The $'\X' construct makes the -e option unnecessary.
  41&nbsp;
  42&nbsp;echo; echo "NEWLINE and (maybe) BEEP"
  43&nbsp;echo $'\n'           # Newline.
  44&nbsp;echo $'\a'           # Alert (beep).
  45&nbsp;                     # May only flash, not beep, depending on terminal.
  46&nbsp;
  47&nbsp;# We have seen $'\nnn" string expansion, and now . . .
  48&nbsp;
  49&nbsp;# =================================================================== #
  50&nbsp;# Version 2 of Bash introduced the $'\nnn' string expansion construct.
  51&nbsp;# =================================================================== #
  52&nbsp;
  53&nbsp;echo "Introducing the \$\' ... \' string-expansion construct . . . "
  54&nbsp;echo ". . . featuring more quotation marks."
  55&nbsp;
  56&nbsp;echo $'\t \042 \t'   # Quote (") framed by tabs.
  57&nbsp;# Note that  '\nnn' is an octal value.
  58&nbsp;
  59&nbsp;# It also works with hexadecimal values, in an $'\xhhh' construct.
  60&nbsp;echo $'\t \x22 \t'  # Quote (") framed by tabs.
  61&nbsp;# Thank you, Greg Keraunen, for pointing this out.
  62&nbsp;# Earlier Bash versions allowed '\x022'.
  63&nbsp;
  64&nbsp;echo
  65&nbsp;
  66&nbsp;
  67&nbsp;# Assigning ASCII characters to a variable.
  68&nbsp;# ----------------------------------------
  69&nbsp;quote=$'\042'        # " assigned to a variable.
  70&nbsp;echo "$quote Quoted string $quote and this lies outside the quotes."
  71&nbsp;
  72&nbsp;echo
  73&nbsp;
  74&nbsp;# Concatenating ASCII chars in a variable.
  75&nbsp;triple_underline=$'\137\137\137'  # 137 is octal ASCII code for '_'.
  76&nbsp;echo "$triple_underline UNDERLINE $triple_underline"
  77&nbsp;
  78&nbsp;echo
  79&nbsp;
  80&nbsp;ABC=$'\101\102\103\010'           # 101, 102, 103 are octal A, B, C.
  81&nbsp;echo $ABC
  82&nbsp;
  83&nbsp;echo
  84&nbsp;
  85&nbsp;escape=$'\033'                    # 033 is octal for escape.
  86&nbsp;echo "\"escape\" echoes as $escape"
  87&nbsp;#                                   no visible output.
  88&nbsp;
  89&nbsp;echo
  90&nbsp;
  91&nbsp;exit 0</PRE
></TD
></TR
></TABLE
><HR></DIV
><P
>A more elaborate example:</P
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="BASHEK"
></A
><P
><B
>Example 5-3. Detecting key-presses</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>   1&nbsp;#!/bin/bash
   2&nbsp;# Author: Sigurd Solaas, 20 Apr 2011
   3&nbsp;# Used in ABS Guide with permission.
   4&nbsp;# Requires version 4.2+ of Bash.
   5&nbsp;
   6&nbsp;key="no value yet"
   7&nbsp;while true; do
   8&nbsp;  clear
   9&nbsp;  echo "Bash Extra Keys Demo. Keys to try:"
  10&nbsp;  echo
  11&nbsp;  echo "* Insert, Delete, Home, End, Page_Up and Page_Down"
  12&nbsp;  echo "* The four arrow keys"
  13&nbsp;  echo "* Tab, enter, escape, and space key"
  14&nbsp;  echo "* The letter and number keys, etc."
  15&nbsp;  echo
  16&nbsp;  echo "    d = show date/time"
  17&nbsp;  echo "    q = quit"
  18&nbsp;  echo "================================"
  19&nbsp;  echo
  20&nbsp;
  21&nbsp; # Convert the separate home-key to home-key_num_7:
  22&nbsp; if [ "$key" = $'\x1b\x4f\x48' ]; then
  23&nbsp;  key=$'\x1b\x5b\x31\x7e'
  24&nbsp;  #   Quoted string-expansion construct. 
  25&nbsp; fi
  26&nbsp;
  27&nbsp; # Convert the separate end-key to end-key_num_1.
  28&nbsp; if [ "$key" = $'\x1b\x4f\x46' ]; then
  29&nbsp;  key=$'\x1b\x5b\x34\x7e'
  30&nbsp; fi
  31&nbsp;
  32&nbsp; case "$key" in
  33&nbsp;  $'\x1b\x5b\x32\x7e')  # Insert
  34&nbsp;   echo Insert Key
  35&nbsp;  ;;
  36&nbsp;  $'\x1b\x5b\x33\x7e')  # Delete
  37&nbsp;   echo Delete Key
  38&nbsp;  ;;
  39&nbsp;  $'\x1b\x5b\x31\x7e')  # Home_key_num_7
  40&nbsp;   echo Home Key
  41&nbsp;  ;;
  42&nbsp;  $'\x1b\x5b\x34\x7e')  # End_key_num_1
  43&nbsp;   echo End Key
  44&nbsp;  ;;
  45&nbsp;  $'\x1b\x5b\x35\x7e')  # Page_Up
  46&nbsp;   echo Page_Up
  47&nbsp;  ;;
  48&nbsp;  $'\x1b\x5b\x36\x7e')  # Page_Down
  49&nbsp;   echo Page_Down
  50&nbsp;  ;;
  51&nbsp;  $'\x1b\x5b\x41')  # Up_arrow
  52&nbsp;   echo Up arrow
  53&nbsp;  ;;
  54&nbsp;  $'\x1b\x5b\x42')  # Down_arrow
  55&nbsp;   echo Down arrow
  56&nbsp;  ;;
  57&nbsp;  $'\x1b\x5b\x43')  # Right_arrow
  58&nbsp;   echo Right arrow
  59&nbsp;  ;;
  60&nbsp;  $'\x1b\x5b\x44')  # Left_arrow
  61&nbsp;   echo Left arrow
  62&nbsp;  ;;
  63&nbsp;  $'\x09')  # Tab
  64&nbsp;   echo Tab Key
  65&nbsp;  ;;
  66&nbsp;  $'\x0a')  # Enter
  67&nbsp;   echo Enter Key
  68&nbsp;  ;;
  69&nbsp;  $'\x1b')  # Escape
  70&nbsp;   echo Escape Key
  71&nbsp;  ;;
  72&nbsp;  $'\x20')  # Space
  73&nbsp;   echo Space Key
  74&nbsp;  ;;
  75&nbsp;  d)
  76&nbsp;   date
  77&nbsp;  ;;
  78&nbsp;  q)
  79&nbsp;  echo Time to quit...
  80&nbsp;  echo
  81&nbsp;  exit 0
  82&nbsp;  ;;
  83&nbsp;  *)
  84&nbsp;   echo You pressed: \'"$key"\'
  85&nbsp;  ;;
  86&nbsp; esac
  87&nbsp;
  88&nbsp; echo
  89&nbsp; echo "================================"
  90&nbsp;
  91&nbsp; unset K1 K2 K3
  92&nbsp; read -s -N1 -p "Press a key: "
  93&nbsp; K1="$REPLY"
  94&nbsp; read -s -N2 -t 0.001
  95&nbsp; K2="$REPLY"
  96&nbsp; read -s -N1 -t 0.001
  97&nbsp; K3="$REPLY"
  98&nbsp; key="$K1$K2$K3"
  99&nbsp;
 100&nbsp;done
 101&nbsp;
 102&nbsp;exit $?</PRE
></TD
></TR
></TABLE
><HR></DIV
><P
>See also <A
HREF="bash2.html#EX77"
>Example 37-1</A
>.</P
></DD
><DT
><SPAN
CLASS="TOKEN"
>\"</SPAN
></DT
><DD
><P
> gives the quote its literal meaning</P
><P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>   1&nbsp;echo "Hello"                     # Hello
   2&nbsp;echo "\"Hello\" ... he said."    # "Hello" ... he said.</PRE
></TD
></TR
></TABLE
></P
></DD
><DT
><SPAN
CLASS="TOKEN"
>\$</SPAN
></DT
><DD
><P
>gives the dollar sign its literal meaning
	      (variable name following <SPAN
CLASS="TOKEN"
>\$</SPAN
> will not be
	      referenced)</P
><P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>   1&nbsp;echo "\$variable01"           # $variable01
   2&nbsp;echo "The book cost \$7.98."  # The book cost $7.98.</PRE
></TD
></TR
></TABLE
></P
></DD
><DT
><SPAN
CLASS="TOKEN"
>\\</SPAN
></DT
><DD
><P
>gives the backslash its literal meaning</P
><P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>   1&nbsp;echo "\\"  # Results in \
   2&nbsp;
   3&nbsp;# Whereas . . .
   4&nbsp;
   5&nbsp;echo "\"   # Invokes secondary prompt from the command-line.
   6&nbsp;           # In a script, gives an error message.
   7&nbsp;
   8&nbsp;# However . . .
   9&nbsp;
  10&nbsp;echo '\'   # Results in \</PRE
></TD
></TR
></TABLE
></P
></DD
></DL
></DIV
><DIV
CLASS="NOTE"
><TABLE
CLASS="NOTE"
WIDTH="100%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="common/note.png"
HSPACE="5"
ALT="Note"></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
>The behavior of <SPAN
CLASS="TOKEN"
>\</SPAN
> depends on whether
	it is escaped, <A
HREF="variables.html#SNGLQUO"
>strong-quoted</A
>,
	<A
HREF="variables.html#DBLQUO"
>weak-quoted</A
>, or appearing within
	<A
HREF="commandsub.html#COMMANDSUBREF"
>command substitution</A
> or a
	<A
HREF="here-docs.html#HEREDOCREF"
>here document</A
>.

	<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>   1&nbsp;                      #  Simple escaping and quoting
   2&nbsp;echo \z               #  z
   3&nbsp;echo \\z              # \z
   4&nbsp;echo '\z'             # \z
   5&nbsp;echo '\\z'            # \\z
   6&nbsp;echo "\z"             # \z
   7&nbsp;echo "\\z"            # \z
   8&nbsp;
   9&nbsp;                      #  Command substitution
  10&nbsp;echo `echo \z`        #  z
  11&nbsp;echo `echo \\z`       #  z
  12&nbsp;echo `echo \\\z`      # \z
  13&nbsp;echo `echo \\\\z`     # \z
  14&nbsp;echo `echo \\\\\\z`   # \z
  15&nbsp;echo `echo \\\\\\\z`  # \\z
  16&nbsp;echo `echo "\z"`      # \z
  17&nbsp;echo `echo "\\z"`     # \z
  18&nbsp;
  19&nbsp;                      # Here document
  20&nbsp;cat &#60;&#60;EOF              
  21&nbsp;\z                      
  22&nbsp;EOF                   # \z
  23&nbsp;
  24&nbsp;cat &#60;&#60;EOF              
  25&nbsp;\\z                     
  26&nbsp;EOF                   # \z
  27&nbsp;
  28&nbsp;# These examples supplied by Stphane Chazelas.</PRE
></TD
></TR
></TABLE
>
      </P
><P
>Elements of a string assigned to a variable may be escaped, but
        the escape character alone may not be assigned to a variable.
	<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>   1&nbsp;variable=\
   2&nbsp;echo "$variable"
   3&nbsp;# Will not work - gives an error message:
   4&nbsp;# test.sh: : command not found
   5&nbsp;# A "naked" escape cannot safely be assigned to a variable.
   6&nbsp;#
   7&nbsp;#  What actually happens here is that the "\" escapes the newline and
   8&nbsp;#+ the effect is        variable=echo "$variable"
   9&nbsp;#+                      invalid variable assignment
  10&nbsp;
  11&nbsp;variable=\
  12&nbsp;23skidoo
  13&nbsp;echo "$variable"        #  23skidoo
  14&nbsp;                        #  This works, since the second line
  15&nbsp;                        #+ is a valid variable assignment.
  16&nbsp;
  17&nbsp;variable=\ 
  18&nbsp;#        \^    escape followed by space
  19&nbsp;echo "$variable"        # space
  20&nbsp;
  21&nbsp;variable=\\
  22&nbsp;echo "$variable"        # \
  23&nbsp;
  24&nbsp;variable=\\\
  25&nbsp;echo "$variable"
  26&nbsp;# Will not work - gives an error message:
  27&nbsp;# test.sh: \: command not found
  28&nbsp;#
  29&nbsp;#  First escape escapes second one, but the third one is left "naked",
  30&nbsp;#+ with same result as first instance, above.
  31&nbsp;
  32&nbsp;variable=\\\\
  33&nbsp;echo "$variable"        # \\
  34&nbsp;                        # Second and fourth escapes escaped.
  35&nbsp;                        # This is o.k.</PRE
></TD
></TR
></TABLE
>
      </P
></TD
></TR
></TABLE
></DIV
><P
>Escaping a space can prevent word splitting in a command's argument list.
        <TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>   1&nbsp;file_list="/bin/cat /bin/gzip /bin/more /usr/bin/less /usr/bin/emacs-20.7"
   2&nbsp;# List of files as argument(s) to a command.
   3&nbsp;
   4&nbsp;# Add two files to the list, and list all.
   5&nbsp;ls -l /usr/X11R6/bin/xsetroot /sbin/dump $file_list
   6&nbsp;
   7&nbsp;echo "-------------------------------------------------------------------------"
   8&nbsp;
   9&nbsp;# What happens if we escape a couple of spaces?
  10&nbsp;ls -l /usr/X11R6/bin/xsetroot\ /sbin/dump\ $file_list
  11&nbsp;# Error: the first three files concatenated into a single argument to 'ls -l'
  12&nbsp;#        because the two escaped spaces prevent argument (word) splitting.</PRE
></TD
></TR
></TABLE
></P
><P
><A
NAME="ESCNEWLINE"
></A
></P
><P
>The <SPAN
CLASS="TOKEN"
>escape</SPAN
> also provides a means of writing a
	multi-line command. Normally, each separate line constitutes
	a different command, but an <SPAN
CLASS="TOKEN"
>escape</SPAN
> at the end
	of a line <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>escapes the newline character</I
></SPAN
>,
	and the command sequence continues on to the next line.</P
><P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>   1&nbsp;(cd /source/directory &#38;&#38; tar cf - . ) | \
   2&nbsp;(cd /dest/directory &#38;&#38; tar xpvf -)
   3&nbsp;# Repeating Alan Cox's directory tree copy command,
   4&nbsp;# but split into two lines for increased legibility.
   5&nbsp;
   6&nbsp;# As an alternative:
   7&nbsp;tar cf - -C /source/directory . |
   8&nbsp;tar xpvf - -C /dest/directory
   9&nbsp;# See note below.
  10&nbsp;# (Thanks, Stphane Chazelas.)</PRE
></TD
></TR
></TABLE
>
        
	<DIV
CLASS="NOTE"
><TABLE
CLASS="NOTE"
WIDTH="100%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="common/note.png"
HSPACE="5"
ALT="Note"></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
>If a script line ends with a <SPAN
CLASS="TOKEN"
>|</SPAN
>, a pipe
	  character, then a <SPAN
CLASS="TOKEN"
>\</SPAN
>, an escape, is not strictly
	  necessary. It is, however, good programming practice to always
	  escape the end of a line of code that continues to the
	  following line.</P
></TD
></TR
></TABLE
></DIV
></P
><P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>   1&nbsp;echo "foo
   2&nbsp;bar" 
   3&nbsp;#foo
   4&nbsp;#bar
   5&nbsp;
   6&nbsp;echo
   7&nbsp;
   8&nbsp;echo 'foo
   9&nbsp;bar'    # No difference yet.
  10&nbsp;#foo
  11&nbsp;#bar
  12&nbsp;
  13&nbsp;echo
  14&nbsp;
  15&nbsp;echo foo\
  16&nbsp;bar     # Newline escaped.
  17&nbsp;#foobar
  18&nbsp;
  19&nbsp;echo
  20&nbsp;
  21&nbsp;echo "foo\
  22&nbsp;bar"     # Same here, as \ still interpreted as escape within weak quotes.
  23&nbsp;#foobar
  24&nbsp;
  25&nbsp;echo
  26&nbsp;
  27&nbsp;echo 'foo\
  28&nbsp;bar'     # Escape character \ taken literally because of strong quoting.
  29&nbsp;#foo\
  30&nbsp;#bar
  31&nbsp;
  32&nbsp;# Examples suggested by Stphane Chazelas.</PRE
></TD
></TR
></TABLE
></P
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="quoting.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="exit-status.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Quoting</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="quoting.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Exit and Exit Status</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>