File: printf.html

package info (click to toggle)
fish 4.2.1-3.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 35,980 kB
  • sloc: python: 6,972; javascript: 1,407; sh: 1,009; xml: 411; ansic: 230; objc: 78; makefile: 20
file content (304 lines) | stat: -rw-r--r-- 19,381 bytes parent folder | download
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
<!DOCTYPE html>

<html lang="en" data-content_root="../">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />

    <title>printf - display text according to a format string &#8212; fish-shell 4.2.1 documentation</title>
    <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=4da8bbd6" />
    <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=f89b4716" />
    <script src="../_static/documentation_options.js?v=6fb65176"></script>
    <script src="../_static/doctools.js?v=9bcbadda"></script>
    <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link rel="next" title="prompt_hostname - print the hostname, shortened for use in the prompt" href="prompt_hostname.html" />
    <link rel="prev" title="prevd - move backward through directory history" href="prevd.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/fish.png" />
     

  </head><body><div id="fmain">  
    <div class="related" role="navigation" aria-label="Related">
      <h3>Navigation</h3>
      <ul>
    <li><img src="../_static/fish.png" alt=""
             style="width: 80px; height: 80px; vertical-align: middle; margin-top: -1px"/></li>
    <li><a href="https://fishshell.com/">fish-shell</a> &#187;</li>
    
    <a href="../index.html">fish-shell 4.2.1 documentation</a> &#187;
    

          <li class="nav-item nav-item-1"><a href="../commands.html" accesskey="U">Commands</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">printf - display text according to a format string</a></li>
    <li class="right">
        

    <div class="inline-search" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="Quick search" type="text" name="q" />
          <input type="submit" value="Go" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    </li>
    
    <div id="old-docs-notice" style="display: none">
        This documents an old version of fish.
        <a href="../../current/">See the latest release.</a>
    </div>

      </ul>
    </div>    

    <div class="document">
      <div class="sphinxsidebar" role="navigation" aria-label="Main">
        <div class="sphinxsidebarwrapper">
<div>
<h3><a href="../index.html">Documents</a></h3>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../index.html">Introduction</a></li>
<li class="toctree-l1"><a class="reference internal" href="../faq.html">Frequently asked questions</a></li>
<li class="toctree-l1"><a class="reference internal" href="../interactive.html">Interactive use</a></li>
<li class="toctree-l1"><a class="reference internal" href="../language.html">The fish language</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="../commands.html">Commands</a></li>
<li class="toctree-l1"><a class="reference internal" href="../fish_for_bash_users.html">Fish for bash users</a></li>
<li class="toctree-l1"><a class="reference internal" href="../tutorial.html">Tutorial</a></li>
<li class="toctree-l1"><a class="reference internal" href="../completions.html">Writing your own completions</a></li>
<li class="toctree-l1"><a class="reference internal" href="../prompt.html">Writing your own prompt</a></li>
<li class="toctree-l1"><a class="reference internal" href="../design.html">Design</a></li>
<li class="toctree-l1"><a class="reference internal" href="../relnotes.html">Release notes</a></li>
<li class="toctree-l1"><a class="reference internal" href="../terminal-compatibility.html">Terminal Compatibility</a></li>
<li class="toctree-l1"><a class="reference internal" href="../contributing.html">Contributing To Fish</a></li>
<li class="toctree-l1"><a class="reference internal" href="../license.html">License</a></li>
</ul>

</div>
<search id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
      <input type="submit" value="Go" />
    </form>
    </div>
</search>
<script>document.getElementById('searchbox').style.display = "block"</script>
<div>
<h4><a href="../index.html">Sections</a></h4>
<ul>
<li><a class="reference internal" href="#">printf - display text according to a format string</a><ul>
<li><a class="reference internal" href="#synopsis">Synopsis</a></li>
<li><a class="reference internal" href="#description">Description</a></li>
<li><a class="reference internal" href="#format-specifiers">Format Specifiers</a></li>
<li><a class="reference internal" href="#backslash-escapes">Backslash Escapes</a></li>
<li><a class="reference internal" href="#errors-and-return-status">Errors and Return Status</a></li>
<li><a class="reference internal" href="#example">Example</a></li>
<li><a class="reference internal" href="#see-also">See Also</a></li>
<li><a class="reference internal" href="#footnotes">Footnotes</a></li>
</ul>
</li>
</ul>

</div>
        </div>
      </div>
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <section id="printf-display-text-according-to-a-format-string">
<h1>printf - display text according to a format string<a class="headerlink" href="#printf-display-text-according-to-a-format-string" title="Link to this heading">¶</a></h1>
<section id="synopsis">
<h2>Synopsis<a class="headerlink" href="#synopsis" title="Link to this heading">¶</a></h2>
<div class="highlight-fish-synopsis notranslate"><div class="highlight"><pre><span></span><span class="nf">printf</span><span class="w"> </span><span class="no">FORMAT</span><span class="w"> </span><span class="g g-Other">[</span><span class="no">ARGUMENT</span><span class="w"> </span><span class="g g-Other">...]</span>
</pre></div>
</div>
</section>
<section id="description">
<h2>Description<a class="headerlink" href="#description" title="Link to this heading">¶</a></h2>
<p><code class="docutils literal notranslate"><span class="pre">printf</span></code> uses the format string <em>FORMAT</em> to print the <em>ARGUMENT</em> arguments. This means that it takes format specifiers in the format string and replaces each with an argument.</p>
<p>The <em>FORMAT</em> argument is re-used as many times as necessary to convert all of the given arguments. So <code class="docutils literal notranslate"><span class="pre">printf</span> <span class="pre">%s\n</span> <span class="pre">flounder</span> <span class="pre">catfish</span> <span class="pre">clownfish</span> <span class="pre">shark</span></code> will print four lines.</p>
<p>Unlike <a class="reference internal" href="echo.html"><span class="doc">echo</span></a>, <code class="docutils literal notranslate"><span class="pre">printf</span></code> does not append a new line unless it is specified as part of the string.</p>
<p>It doesn’t support any options, so there is no need for a <code class="docutils literal notranslate"><span class="pre">--</span></code> separator, which makes it easier to use for arbitrary input than <code class="docutils literal notranslate"><span class="pre">echo</span></code>. <a class="footnote-reference brackets" href="#id2" id="id1" role="doc-noteref"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></a></p>
</section>
<section id="format-specifiers">
<h2>Format Specifiers<a class="headerlink" href="#format-specifiers" title="Link to this heading">¶</a></h2>
<p>Valid format specifiers are taken from the C library function <code class="docutils literal notranslate"><span class="pre">printf(3)</span></code>:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">%d</span></code> or <code class="docutils literal notranslate"><span class="pre">%i</span></code>: Argument will be used as decimal integer (signed or unsigned)</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">%o</span></code>: An octal unsigned integer</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">%u</span></code>: An unsigned decimal integer - this means negative numbers will wrap around</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">%x</span></code> or <code class="docutils literal notranslate"><span class="pre">%X</span></code>: An unsigned hexadecimal integer</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">%f</span></code>, <code class="docutils literal notranslate"><span class="pre">%g</span></code> or <code class="docutils literal notranslate"><span class="pre">%G</span></code>: A floating-point number. <code class="docutils literal notranslate"><span class="pre">%f</span></code> defaults to 6 places after the decimal point (which is locale-dependent - e.g. in de_DE it will be a <code class="docutils literal notranslate"><span class="pre">,</span></code>). <code class="docutils literal notranslate"><span class="pre">%g</span></code> and <code class="docutils literal notranslate"><span class="pre">%G</span></code> will trim trailing zeroes and switch to scientific notation (like <code class="docutils literal notranslate"><span class="pre">%e</span></code>) if the numbers get small or large enough.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">%e</span></code> or <code class="docutils literal notranslate"><span class="pre">%E</span></code>: A floating-point number in scientific (XXXeYY) notation</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">%s</span></code>: A string</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">%b</span></code>: As a string, interpreting backslash escapes, except that octal escapes are of the form 0 or 0ooo.</p></li>
</ul>
<p><code class="docutils literal notranslate"><span class="pre">%%</span></code> signifies a literal “%”.</p>
<p>Conversion can fail, e.g. “102.234” can’t losslessly convert to an integer, causing printf to print an error. If you are okay with losing information, silence errors with <code class="docutils literal notranslate"><span class="pre">2&gt;/dev/null</span></code>.</p>
<p>A number between the <code class="docutils literal notranslate"><span class="pre">%</span></code> and the format letter specifies the width. The result will be left-padded with spaces.</p>
</section>
<section id="backslash-escapes">
<h2>Backslash Escapes<a class="headerlink" href="#backslash-escapes" title="Link to this heading">¶</a></h2>
<p>printf also knows a number of backslash escapes:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">\&quot;</span></code> double quote</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">\\</span></code> backslash</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">\a</span></code> alert (bell)</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">\b</span></code> backspace</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">\c</span></code> produce no further output</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">\e</span></code> escape</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">\f</span></code> form feed</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">\n</span></code> new line</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">\r</span></code> carriage return</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">\t</span></code> horizontal tab</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">\v</span></code> vertical tab</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">\ooo</span></code> octal number (ooo is 1 to 3 digits)</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">\xhh</span></code> hexadecimal number (hhh is 1 to 2 digits)</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">\uhhhh</span></code> 16-bit Unicode character (hhhh is 4 digits)</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">\Uhhhhhhhh</span></code> 32-bit Unicode character (hhhhhhhh is 8 digits)</p></li>
</ul>
</section>
<section id="errors-and-return-status">
<h2>Errors and Return Status<a class="headerlink" href="#errors-and-return-status" title="Link to this heading">¶</a></h2>
<p>If the given argument doesn’t work for the given format (like when you try to convert a number like 3.141592 to an integer), printf prints an error, to stderr. printf will then also return non-zero, but will still try to print as much as it can.</p>
<p>It will also return non-zero if no argument at all was given, in which case it will print nothing.</p>
<p>This printf has been imported from the printf in GNU Coreutils version 6.9. If you would like to use a newer version of printf, for example the one shipped with your OS, try <code class="docutils literal notranslate"><span class="pre">command</span> <span class="pre">printf</span></code>.</p>
</section>
<section id="example">
<h2>Example<a class="headerlink" href="#example" title="Link to this heading">¶</a></h2>
<div class="highlight-fish-docs-samples notranslate"><div class="highlight"><pre><span></span><span class="nf">printf</span><span class="w"> </span><span class="s1">&#39;%s\t%s\n&#39;</span><span class="w"> </span><span class="no">flounder</span><span class="w"> </span><span class="no">fish</span>
</pre></div>
</div>
<p>Will print “flounder    fish” (separated with a tab character), followed by a newline character. This is useful for writing completions, as fish expects completion scripts to output the option followed by the description, separated with a tab character.</p>
<div class="highlight-fish-docs-samples notranslate"><div class="highlight"><pre><span></span><span class="nf">printf</span><span class="w"> </span><span class="s1">&#39;%s: %d&#39;</span><span class="w"> </span><span class="s2">&quot;Number of bananas in my pocket&quot;</span><span class="w"> </span><span class="no">42</span>
</pre></div>
</div>
<p>Will print “Number of bananas in my pocket: 42”, <cite>without</cite> a newline.</p>
</section>
<section id="see-also">
<h2>See Also<a class="headerlink" href="#see-also" title="Link to this heading">¶</a></h2>
<ul class="simple">
<li><p>the <a class="reference internal" href="echo.html"><span class="doc">echo</span></a> command, for simpler output</p></li>
</ul>
</section>
<section id="footnotes">
<h2>Footnotes<a class="headerlink" href="#footnotes" title="Link to this heading">¶</a></h2>
<aside class="footnote-list brackets">
<aside class="footnote brackets" id="id2" role="doc-footnote">
<span class="label"><span class="fn-bracket">[</span><a role="doc-backlink" href="#id1">1</a><span class="fn-bracket">]</span></span>
<p>In fact, while fish’s <code class="docutils literal notranslate"><span class="pre">echo</span></code> supports <code class="docutils literal notranslate"><span class="pre">--</span></code>, POSIX forbids it, so other implementations can’t be used if the input contains anything starting with <code class="docutils literal notranslate"><span class="pre">-</span></code>.</p>
</aside>
</aside>
</section>
</section>


            <div class="clearer"></div>
          </div>
        </div>
      </div> 
      <div class="clearer"></div>
    </div>  
    <div class="related" role="navigation" aria-label="Related">
      <h3>Navigation</h3>
      <ul>
    <li><img src="../_static/fish.png" alt=""
             style="width: 80px; height: 80px; vertical-align: middle; margin-top: -1px"/></li>
    <li><a href="https://fishshell.com/">fish-shell</a> &#187;</li>
    
    <a href="../index.html">fish-shell 4.2.1 documentation</a> &#187;
    

          <li class="nav-item nav-item-1"><a href="../commands.html" >Commands</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">printf - display text according to a format string</a></li>
    <li class="right">
        

    <div class="inline-search" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="Quick search" type="text" name="q" />
          <input type="submit" value="Go" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    </li>
    
    <div id="old-docs-notice" style="display: none">
        This documents an old version of fish.
        <a href="../../current/">See the latest release.</a>
    </div>

      </ul>
    </div>  
    <div class="footer">
    &copy; Copyright fish-shell developers.
    <br />
    <a href="https://github.com/fish-shell/fish-shell/issues">Found a bug</a>?
    <br />

    Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 8.2.3.
    </div>
</div>

<script type="text/javascript">
 FISH_DOCS_VERSION = "4.2";

 function copy_to_clipboard(it) {
     // Find the pre tag we're interested in.
     var pre = it.target;
     while (pre.tagName != "PRE") pre = pre.parentNode;
     var txt = "";
     // Cheesy: If we have a prompt,
     // we only copy prompted lines,
     // by splitting and matching and stuff
     if (pre.querySelector('span.gp')) {
         var texts= [];
         for (var line of pre.innerText.split('\n')) {
             if (line.match(/^>_?.*/)) {
                 texts.push(line.replace(/^>_?/, ""));
             }
         }
         txt = texts.join("\n");
     } else {
         // Even cheesier: If we don't have a prompt, we remove the button text from the end.
         var txt = pre.innerText.substring(0, pre.innerText.length - it.target.innerText.length).trim();
     }

     navigator.clipboard.writeText(txt).then(function() {
         // Success - set the text to indicate it,
         // then set it back after 2 seconds.
         var span = pre.querySelector("button span");
         if (span) {
             var oldText = span.innerText;
             span.innerText = "COPIED!";
             setTimeout(function() {
                 span.innerText = oldText;
             }, 2000);
         }
     }, function() {
     });
 }

  (function () {
      // Add copy buttons to all the codeblocks.
      var codeblocks = document.querySelectorAll('div > pre');

      var button = document.createElement('button');
      var span = document.createElement('span');
      span.innerText = "COPY";
      button.appendChild(span);

      for (var i of codeblocks) {
          var newButton = button.cloneNode(true);
          newButton.addEventListener('click', copy_to_clipboard);
          i.appendChild(newButton);
      }
  })();
</script>

  </body>
</html>