aboutsummaryrefslogtreecommitdiff
blob: dba34523418cd3d57a29f651c55020ba8af93599 (plain)
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
<?xml version="1.0" encoding="UTF-8"?>
<guide self="tools-reference/echo/">
<chapter>
<title><c>echo</c>  print strings</title>
<body>

<p>
The <c>echo</c> command can be used to print strings. The standard
usage is <c>echo firstString secondString ...</c>. Also, it provides
additional parameters for formatting of the output.
</p>
</body>

<section>
<title>Abuse of <c>echo</c></title>
<body>
<p>
If you find yourself about to use <c>echo</c> in an ebuild, stop and
reconsider. It is almost always unnecessary.
</p>

<p>
First of all, for printing messages in standard Portage scripts, you
can use the <c>einfo</c>, and <c>eerror</c> functions along with their
corresponding functions, <c>einfon</c>, <c>eerrorn</c>, etc, which are
the same as the former, but they won't print the trailing newline
(<c>\n</c>).
</p>

<p>
All usage of the form <c>echo ${somevar} | grep substring</c> just to
check if the content of the <c>${somevar}</c> variable contains
<c>substring</c>, or more often, <c>echo ${somevar} | command</c>,
is deprecated and should be (and in most cases, can be) avoided:
doing so involves for no reason an additional shell session and a pipe.
The "here strings" section describes the preferred way
of dealing with such cases.
</p>
</body>
</section>

<section>
<title>Here strings</title>
<body>
<p>
As of &gt;=bash-2.05b, the so-called "here strings" have been
introduced. Using "here strings", you can pass contents of an
environment variable to the standard input of an application, using
<c>&lt;&lt;&lt;word</c> redirection: what actually happens is
that <c>bash</c> expands <c>word</c> and passes the result to the standard
input.
</p>

<p>
A common example would be verifying if a variable <c>${foo}</c>
contains the bar substring with the following construct: <c>grep bar
&lt;&lt;&lt; ${foo}</c>. This replaces the deprecated and more
wasteful behaviour of using <c>echo ${foo} | grep bar</c>.
</p>
</body>
</section>

<section>
<title>Standard usage of <c>echo</c></title>
<body>
<p>
In standard calls, the <c>echo</c> command with no additional options,
outputs the arguments passed to the standard output, separated by
whitespace and with a trailing newline character (<c>\n</c>).
</p>

<p>
If one wants to exclude the trailing newline character, the <c>-n</c>
option can be passed, as in: <c>echo -n "no trailing newline"</c>.
</p>

<p>
Special characters, like tabs (<c>\t</c>), newline characters
(<c>\n</c>), carriage return characters (<c>\r</c>) (i.e. for
translating newlines from DOS format to Unix format), and any other
ASCII character given by its code value) can be passed to the echo
command with the <c>-e</c> option. For example, to output three
strings, each on different lines, in a single <c>echo</c> command,
use: <c>echo -e "first line\nsecond line\nthird line"</c>.
</p>

<p>
Other escape sequences and additional parameters for the <c>echo</c>
command are available in the <c>bash(1)</c> man page.
</p>

</body>
</section>
</chapter>
</guide>