<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>bash &amp;mdash; alan</title>
    <link>https://www.franzoni.eu/alan/tag:bash</link>
    <description>Alan&#39;s musings</description>
    <language>en</language>
    <pubDate>Tue, 17 Nov 2026 00:40:52 +0000</pubDate>
    <item>
      <title>Shell scripting: short or long format options?</title>
      <link>https://www.franzoni.eu/alan/shell-scripting-when-to-use-the-long-or-the-shor</link>
      <description>&lt;![CDATA[&#xA;&#xA;This is something I get asked quite a lot, so I wanted to write a piece about it.&#xA;&#xA;This is an extract from the manpage from GNU grep:&#xA;&#xA;NAME&#xA;       grep, egrep, fgrep, rgrep - print lines matching a pattern&#xA;&#xA;SYNOPSIS&#xA;       grep [OPTIONS] PATTERN [FILE...]&#xA;       grep [OPTIONS] [-e PATTERN]...  [-f FILE]...  [FILE...]&#xA;&#xA;DESCRIPTION&#xA;       grep  searches  the  named  input  FILEs  for lines containing a match to the given PATTERN.  If no files are specified, or if the file “-” is given, grep searches standard input.  By&#xA;       default, grep prints the matching lines.&#xA;&#xA;       In addition, the variant programs egrep, fgrep and rgrep are the same as grep -E, grep -F, and grep -r, respectively.  These variants are deprecated, but  are  provided  for  backward&#xA;       compatibility.&#xA;&#xA;OPTIONS&#xA;   Generic Program Information&#xA;       --help Output a usage message and exit.&#xA;&#xA;       -V, --version&#xA;              Output the version number of grep and exit.&#xA;&#xA;   Matcher Selection&#xA;       -E, --extended-regexp&#xA;              Interpret PATTERN as an extended regular expression (ERE, see below).&#xA;&#xA;       -F, --fixed-strings&#xA;              Interpret PATTERN as a list of fixed strings (instead of regular expressions), separated by newlines, any of which is to be matched.&#xA;&#xA;       -G, --basic-regexp&#xA;              Interpret PATTERN as a basic regular expression (BRE, see below).  This is the default.&#xA;&#xA;       -P, --perl-regexp&#xA;              Interpret the pattern as a Perl-compatible regular expression (PCRE).  This is highly experimental and grep -P may warn of unimplemented features.&#xA;&#xA;So, -F is a short format form, while --fixed-strings is a long format form.&#xA;&#xA;When should I use either form?&#xA;&#xA;My take is: when working interactively, do whatever you like. Whenever you are writing a script to be reused, strive to use the long format options as much as possible.&#xA;&#xA;Why?&#xA;&#xA;Nobody cares about what you do on your computer. But whenever you write a script that should, later on, be used and read by other people - or just yourself in the future - the long form is often self-explanatory. Instead of opening the manpage and looking for cryptic -K -X -i, you can just read the option! It will for sure save more time than what you spend while typing a few more chars.&#xA;&#xA;There&#39;re a few exceptions to this rule:&#xA;&#xA;if the option is very common. Feel free to use -E and -P for grep, as they&#39;re very well known and widespread options for this command.&#xA;if you need portability between different OSes (e.g. MacOS+Linux+BSD) and the long format option is not available somewhere; this sometimes happens.&#xA;&#xA;Why is this cool?&#xA;&#xA;Consider this command, which is the recommended way to install the great MacOS package manager Homebrew:&#xA;&#xA;/usr/bin/ruby -e &#34;$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)&#34;&#xA;&#xA;Such curl command is quite common in many installers; can you tell what such options do? Can&#39;t you? Consider this line:&#xA;&#xA;/usr/bin/ruby -e &#34;$(curl --fail --silent --show-error --location https://raw.githubusercontent.com/Homebrew/install/master/install)&#34;&#xA;&#xA;I bet you can now understand what it does; maybe the last flag is a bit vague (it tells curl to follow redirects via Location headers), but the overall feeling is simply... great!&#xA;&#xA;Photo by Alex Holyoake on Unsplash&#xA;&#xA;#Ollivander #shell #bash&#xA;]]&gt;</description>
      <content:encoded><![CDATA[<p><img src="https://files.franzoni.eu/import/alanslair/2017/09/alex-holyoake-213910--1-.jpg" alt=""></p>

<p>This is something I get asked quite a lot, so I wanted to write a piece about it.</p>

<p>This is an extract from the manpage from GNU grep:</p>

<pre><code>NAME
       grep, egrep, fgrep, rgrep - print lines matching a pattern

SYNOPSIS
       grep [OPTIONS] PATTERN [FILE...]
       grep [OPTIONS] [-e PATTERN]...  [-f FILE]...  [FILE...]

DESCRIPTION
       grep  searches  the  named  input  FILEs  for lines containing a match to the given PATTERN.  If no files are specified, or if the file “-” is given, grep searches standard input.  By
       default, grep prints the matching lines.

       In addition, the variant programs egrep, fgrep and rgrep are the same as grep -E, grep -F, and grep -r, respectively.  These variants are deprecated, but  are  provided  for  backward
       compatibility.

OPTIONS
   Generic Program Information
       --help Output a usage message and exit.

       -V, --version
              Output the version number of grep and exit.

   Matcher Selection
       -E, --extended-regexp
              Interpret PATTERN as an extended regular expression (ERE, see below).

       -F, --fixed-strings
              Interpret PATTERN as a list of fixed strings (instead of regular expressions), separated by newlines, any of which is to be matched.

       -G, --basic-regexp
              Interpret PATTERN as a basic regular expression (BRE, see below).  This is the default.

       -P, --perl-regexp
              Interpret the pattern as a Perl-compatible regular expression (PCRE).  This is highly experimental and grep -P may warn of unimplemented features.
</code></pre>

<p>So, <code>-F</code> is a short format form, while <code>--fixed-strings</code> is a long format form.</p>

<p>When should I use either form?</p>

<p>My take is: when working interactively, do whatever you like. Whenever you are writing a <strong>script to be reused</strong>, strive to use the <strong>long format options as much as possible</strong>.</p>

<p>Why?</p>

<p>Nobody cares about what you do on your computer. But whenever you write a script that should, later on, be used and read by other people – or just yourself in the future – <strong>the long form is often self-explanatory</strong>. Instead of opening the manpage and looking for cryptic <code>-K -X -i</code>, you can just read the option! It will for sure save more time than what you spend while typing a few more chars.</p>

<p>There&#39;re a few exceptions to this rule:</p>
<ul><li>if the option is very common. Feel free to use <code>-E</code> and <code>-P</code> for <code>grep</code>, as they&#39;re very well known and widespread options for this command.</li>
<li>if you need portability between different OSes (e.g. MacOS+Linux+BSD) and the long format option is not available somewhere; this sometimes happens.</li></ul>

<h4 id="why-is-this-cool">Why is this cool?</h4>

<p>Consider this command, which is the recommended way to install the great MacOS package manager <a href="https://brew.sh" rel="nofollow">Homebrew</a>:</p>

<pre><code>/usr/bin/ruby -e &#34;$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)&#34;
</code></pre>

<p>Such <code>curl</code> command is quite common in many installers; <strong>can you tell what such options do?</strong> Can&#39;t you? Consider this line:</p>

<pre><code>/usr/bin/ruby -e &#34;$(curl --fail --silent --show-error --location https://raw.githubusercontent.com/Homebrew/install/master/install)&#34;
</code></pre>

<p>I bet you can now understand what it does; maybe the last flag is a bit vague (it tells <code>curl</code> to follow redirects via <code>Location</code> headers), but the overall feeling is simply... great!</p>

<p><em>Photo by Alex Holyoake on Unsplash</em></p>

<p><a href="/alan/tag:Ollivander" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">Ollivander</span></a> <a href="/alan/tag:shell" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">shell</span></a> <a href="/alan/tag:bash" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">bash</span></a></p>
]]></content:encoded>
      <guid>https://www.franzoni.eu/alan/shell-scripting-when-to-use-the-long-or-the-shor</guid>
      <pubDate>Thu, 07 Sep 2017 13:23:00 +0000</pubDate>
    </item>
    <item>
      <title>badblocks: test your mass storage!</title>
      <link>https://www.franzoni.eu/alan/badblocks-test-your-mass-storage-2</link>
      <description>&lt;![CDATA[Are you unsure whether your mass storage works properly? Do you suspect it&#39;s faulty somewhere?&#xA;&#xA;Verify it with a nifty tool, included in most Linux distros and in macOS if you install the ext2fs tools:&#xA;&#xA;sudo badblocks -swv -t random -b 4096 -c 2048 DEVICE&#xA;&#xA;It will perform a read-write test of your mass storage device, and tell you what&#39;s wrong.&#xA;&#xA;WARNING: everything will be deleted during the test! Make sure you&#39;ve already backupped your data!&#xA;&#xA;#MostlyUnixish #tips #bash&#xA;]]&gt;</description>
      <content:encoded><![CDATA[<p>Are you unsure whether your mass storage works properly? Do you suspect it&#39;s faulty somewhere?</p>

<p>Verify it with a nifty tool, included in most Linux distros and in macOS if you install the ext2fs tools:</p>

<pre><code>sudo badblocks -swv -t random -b 4096 -c 2048 &lt;DEVICE&gt;
</code></pre>

<p>It will perform a read-write test of your mass storage device, and tell you what&#39;s wrong.</p>

<p><strong>WARNING:</strong> everything will be deleted during the test! Make sure you&#39;ve already backupped your data!</p>

<p><a href="/alan/tag:MostlyUnixish" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">MostlyUnixish</span></a> <a href="/alan/tag:tips" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">tips</span></a> <a href="/alan/tag:bash" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">bash</span></a></p>
]]></content:encoded>
      <guid>https://www.franzoni.eu/alan/badblocks-test-your-mass-storage-2</guid>
      <pubDate>Mon, 26 Sep 2016 10:29:58 +0000</pubDate>
    </item>
    <item>
      <title>Quick log for bash scripts, with line limit</title>
      <link>https://www.franzoni.eu/alan/quick-log-for-bash-scripts-with-line-limit</link>
      <description>&lt;![CDATA[Sometimes you need to write a quick shell script; many a time, you need to put it on a server, and launch it every now and then, or run it in a cron job. That script may end up doing something important and/or altering the state of the server machine.&#xA;&#xA;If that script starts being using often enough, you&#39;ll want logging as well, and that can be a complex beast to tame, for logs require rotation and deletion, otherwise the beast will eat up&#xA;all your disk space. Setting up logrotate for a single shell script looks like an overkill to me most of the times.&#xA;&#xA;So what? Many a times logging will be neglected, and you&#39;ll lose the ability to track what happened and why.&#xA;&#xA;This is my quick recipe for a basic bash logging solution with line-based truncation: the logfile will never exceed RETAIN\NUM\LINES lines (actually, it will never exceed such size when the script is launched, so the number of lines when the script has run is RETAIN\NUM\LINES + number of lines logged by the script).&#xA;&#xA;Whenever you use the log function you&#39;ll get output on stdout as well, complete with a timestamp.&#xA;&#xA;!/bin/bash&#xA;&#xA;set LOGFILE to the full path of your desired logfile; make sure&#xA;you have write permissions there. set RETAINNUMLINES to the&#xA;maximum number of lines that should be retained at the beginning&#xA;of your program execution.&#xA;execute &#39;logsetup&#39; once at the beginning of your script, then&#xA;use &#39;log&#39; how many times you like.&#xA;&#xA;LOGFILE=quicklog.sh.log&#xA;RETAINNUMLINES=10&#xA;&#xA;function logsetup {&#xA;    TMP=$(tail -n $RETAINNUM_LINES $LOGFILE 2  /dev/null) &amp;&amp; echo &#34;${TMP}&#34;   $LOGFILE&#xA;    exec     (tee -a $LOGFILE)&#xA;    exec 2  &amp;1&#xA;}&#xA;&#xA;function log {&#xA;    echo &#34;[$(date --rfc-3339=seconds)]: $&#34;&#xA;}&#xA;&#xA;logsetup&#xA;&#xA;log hello this is a log&#xA;&#xA;Launch this a lot of times and you&#39;ll end up with this in quicklog.sh.log*:&#xA;&#xA;[2016-09-26 11:44:09+02:00]: hello this is a log&#xA;[2016-09-26 11:44:09+02:00]: hello this is a log&#xA;[2016-09-26 11:44:09+02:00]: hello this is a log&#xA;[2016-09-26 11:44:09+02:00]: hello this is a log&#xA;[2016-09-26 11:44:10+02:00]: hello this is a log&#xA;[2016-09-26 11:44:10+02:00]: hello this is a log&#xA;[2016-09-26 11:44:10+02:00]: hello this is a log&#xA;[2016-09-26 11:44:11+02:00]: hello this is a log&#xA;[2016-09-26 11:44:11+02:00]: hello this is a log&#xA;[2016-09-26 11:44:11+02:00]: hello this is a log&#xA;[2016-09-26 11:44:14+02:00]: hello this is a log&#xA;&#xA;Enjoy!&#xA;&#xA;UPDATED on September 26th, 2016 to employ the rfc-3339 date option, which makes the logline quite more readable and greppable.&#xA;&#xA;#MostlyUnixish #bash #scripting #shell&#xA;]]&gt;</description>
      <content:encoded><![CDATA[<p>Sometimes you need to write a quick shell script; many a time, you need to put it on a server, and launch it every now and then, or run it in a cron job. That script may end up doing something important and/or altering the state of the server machine.</p>

<p>If that script starts being using often enough, you&#39;ll want logging as well, and that can be a complex beast to tame, for logs require rotation and deletion, otherwise the beast will eat up
all your disk space. Setting up <a href="http://linuxcommand.org/man_pages/logrotate8.html" rel="nofollow">logrotate</a> for a single shell script looks like an overkill to me most of the times.</p>

<p>So what? Many a times logging will be neglected, and you&#39;ll lose the ability to track what happened and why.</p>

<p>This is my quick recipe for a basic bash logging solution with line-based truncation: the logfile will never exceed <strong>RETAIN_NUM_LINES</strong> lines (actually, it will never exceed such size when the script is launched, so the number of lines when the script has run is RETAIN_NUM_LINES + number of lines logged by the script).</p>

<p>Whenever you use the <em>log</em> function you&#39;ll get output on stdout as well, complete with a timestamp.</p>

<pre><code class="language-bash">#!/bin/bash

# set LOGFILE to the full path of your desired logfile; make sure
# you have write permissions there. set RETAIN_NUM_LINES to the
# maximum number of lines that should be retained at the beginning
# of your program execution.
# execute &#39;logsetup&#39; once at the beginning of your script, then
# use &#39;log&#39; how many times you like.

LOGFILE=quicklog.sh.log
RETAIN_NUM_LINES=10

function logsetup {
    TMP=$(tail -n $RETAIN_NUM_LINES $LOGFILE 2&gt;/dev/null) &amp;&amp; echo &#34;${TMP}&#34; &gt; $LOGFILE
    exec &gt; &gt;(tee -a $LOGFILE)
    exec 2&gt;&amp;1
}

function log {
    echo &#34;[$(date --rfc-3339=seconds)]: $*&#34;
}

logsetup

log hello this is a log
</code></pre>

<p>Launch this a lot of times and you&#39;ll end up with this in <em>quicklog.sh.log</em>:</p>

<pre><code>[2016-09-26 11:44:09+02:00]: hello this is a log
[2016-09-26 11:44:09+02:00]: hello this is a log
[2016-09-26 11:44:09+02:00]: hello this is a log
[2016-09-26 11:44:09+02:00]: hello this is a log
[2016-09-26 11:44:10+02:00]: hello this is a log
[2016-09-26 11:44:10+02:00]: hello this is a log
[2016-09-26 11:44:10+02:00]: hello this is a log
[2016-09-26 11:44:11+02:00]: hello this is a log
[2016-09-26 11:44:11+02:00]: hello this is a log
[2016-09-26 11:44:11+02:00]: hello this is a log
[2016-09-26 11:44:14+02:00]: hello this is a log
</code></pre>

<p>Enjoy!</p>

<p><strong>UPDATED</strong> on September 26th, 2016 to employ the rfc-3339 date option, which makes the logline quite more readable and greppable.</p>

<p><a href="/alan/tag:MostlyUnixish" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">MostlyUnixish</span></a> <a href="/alan/tag:bash" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">bash</span></a> <a href="/alan/tag:scripting" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">scripting</span></a> <a href="/alan/tag:shell" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">shell</span></a></p>
]]></content:encoded>
      <guid>https://www.franzoni.eu/alan/quick-log-for-bash-scripts-with-line-limit</guid>
      <pubDate>Tue, 08 Oct 2013 20:47:00 +0000</pubDate>
    </item>
    <item>
      <title>Disown is your friend</title>
      <link>https://www.franzoni.eu/alan/disown-is-your-friend</link>
      <description>&lt;![CDATA[Bash builtin disown is a not-so-well known \job control command\] [http://www.gnu.org/software/bash/manual/html\node/Job-Control-Builtins.html), and yet&#xA;it&#39;s an extremely useful one.&#xA;&#xA;So what? You&#39;ve started a remote ssh session, launched some command which is taking very, very long to complete, and you just want to go home, or maybe you feel your connectivity may drop, and so on... and you forgot to launch screen (possibly with \[byobu\] https://launchpad.net/byobu))&#xA;or tmux before starting your work!&#xA;&#xA;Now you&#39;re desperate and you&#39;d just like to go home, but you fear your longstanding jobs will quit if you close your terminal.&#xA;&#xA;Just background your job, launch disown and all the jobs in background will receive no notification whatsoever of your saying goodbye to them; of course you won&#39;t be able to read their output to the terminal.&#xA;&#xA;#MostlyUnixish #linux #bash #ssh&#xA;]]&gt;</description>
      <content:encoded><![CDATA[<p>Bash builtin <em>disown</em> is a not-so-well known [job control command] <a href="http://www.gnu.org/software/bash/manual/html_node/Job-Control-Builtins.html" rel="nofollow">http://www.gnu.org/software/bash/manual/html_node/Job-Control-Builtins.html</a>), and yet
it&#39;s an extremely useful one.</p>

<p>So what? You&#39;ve started a remote ssh session, launched some command which is taking very, very long to complete, and you just want to go home, or maybe you feel your connectivity may drop, and so on... and you forgot to launch <a href="http://www.gnu.org/software/screen" rel="nofollow">screen</a> (possibly with [byobu] <a href="https://launchpad.net/byobu" rel="nofollow">https://launchpad.net/byobu</a>))
or <a href="http://tmux.sourceforge.net" rel="nofollow">tmux</a> before starting your work!</p>

<p>Now you&#39;re desperate and you&#39;d just like to go home, but you fear your longstanding jobs will quit if you close your terminal.</p>

<p>Just background your job, launch <em>disown</em> and all the jobs in background will receive no notification whatsoever of your saying goodbye to them; of course you won&#39;t be able to read their output to the terminal.</p>

<p><a href="/alan/tag:MostlyUnixish" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">MostlyUnixish</span></a> <a href="/alan/tag:linux" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">linux</span></a> <a href="/alan/tag:bash" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">bash</span></a> <a href="/alan/tag:ssh" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">ssh</span></a></p>
]]></content:encoded>
      <guid>https://www.franzoni.eu/alan/disown-is-your-friend</guid>
      <pubDate>Wed, 22 Aug 2012 20:35:00 +0000</pubDate>
    </item>
  </channel>
</rss>