<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
    <channel>
        <title>Tim Visée</title>
        <link>https://timvisee.com</link>
        <description>Personal portfolio and blog of Tim Visée</description>
        <generator>Zola</generator>
        <language>en</language>
        <atom:link href="https://timvisee.com/tags/bash/rss.xml" rel="self" type="application/rss+xml"/>
        <lastBuildDate>Tue, 02 Mar 2021 00:00:00 +0000</lastBuildDate>
        
            <item>
                <title>Elegant bash conditionals</title>
                <pubDate>Tue, 02 Mar 2021 00:00:00 +0000</pubDate>
                <link>https://timvisee.com/blog/elegant-bash-conditionals/</link>
                <guid>https://timvisee.com/blog/elegant-bash-conditionals/</guid>
                <description>&lt;p&gt;The if-statement is a very basic thing, not just in bash, but in all of programming.
I see them used quite a lot in shell scripts,
even though in many cases they can be replaced with something much more elegant.&lt;&#x2F;p&gt;
&lt;p&gt;In this rather short article, I&#x27;ll show how control operators can be used
instead.
Many probably know about this, but don&#x27;t realize how to use them nicely.
This will help you write cleaner shell scripts in the future.&lt;&#x2F;p&gt;
&lt;p&gt;Here is what a simple if-statements looks like in bash:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#191919;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6699cc;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt; expression &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6699cc;&quot;&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;then
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6699cc;&quot;&gt;command
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;fi
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#6d6d6d;&quot;&gt;# or
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6699cc;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt; expression &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6699cc;&quot;&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;; then &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6699cc;&quot;&gt;command&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;; fi
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Ughh. Let&#x27;s improve!&lt;&#x2F;p&gt;
&lt;span id=&quot;continue-reading&quot;&gt;&lt;&#x2F;span&gt;&lt;h2 id=&quot;control-operators&quot;&gt;Control operators
    &lt;a class=&quot;anchor&quot; href=&quot;#control-operators&quot; aria-label=&quot;Anchor link for: control-operators&quot;&gt;&lt;&#x2F;a&gt;

&lt;&#x2F;h2&gt;
&lt;p&gt;Bash provides &lt;a rel=&quot;noopener&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;www.gnu.org&#x2F;software&#x2F;bash&#x2F;manual&#x2F;html_node&#x2F;Definitions.html#Definitions&quot;&gt;control operators&lt;&#x2F;a&gt; to build sequences of
commands.
Some of these are conditional and allow logical branching based on the success
state of the last run command.&lt;&#x2F;p&gt;
&lt;p&gt;We will just focus on these two logical operators:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;&amp;amp;&amp;amp;&lt;&#x2F;code&gt;: the AND operator, run the following command only if previous succeeded&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;||&lt;&#x2F;code&gt;: the OR operator, run the following command only if previous failed&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h2 id=&quot;exit-codes&quot;&gt;Exit codes
    &lt;a class=&quot;anchor&quot; href=&quot;#exit-codes&quot; aria-label=&quot;Anchor link for: exit-codes&quot;&gt;&lt;&#x2F;a&gt;

&lt;&#x2F;h2&gt;
&lt;p&gt;You might wonder how bash considers whether a command succeeded.
This is where &lt;a rel=&quot;noopener&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Exit_status&quot;&gt;exit codes&lt;&#x2F;a&gt; come in.
When a program exits a numeric status code is returned.
A value of &lt;code&gt;0&lt;&#x2F;code&gt; means the program succeeded, any other value means it failed.&lt;&#x2F;p&gt;
&lt;p&gt;The exit code is normally hidden.
The status of the last run command is stored in the &lt;code&gt;?&lt;&#x2F;code&gt; variable.
You may inspect it by invoking:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#191919;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#6699cc;&quot;&gt;echo &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;?
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;E.g., listing with &lt;code&gt;ls&lt;&#x2F;code&gt; normally returns &lt;code&gt;0&lt;&#x2F;code&gt;, but this value differs if the
directory doesn&#x27;t exist or if an error occurred.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#191919;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;ls ~&lt;&#x2F;span&gt;&lt;span&gt;&#x2F;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6d6d6d;&quot;&gt;# exit code: 0
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;ls ~&lt;&#x2F;span&gt;&lt;span&gt;&#x2F;nonexistent &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6d6d6d;&quot;&gt;# exit code: 2
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This will function similarly to almost any program.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;chaining-commands&quot;&gt;Chaining commands
    &lt;a class=&quot;anchor&quot; href=&quot;#chaining-commands&quot; aria-label=&quot;Anchor link for: chaining-commands&quot;&gt;&lt;&#x2F;a&gt;

&lt;&#x2F;h2&gt;
&lt;p&gt;Let&#x27;s go over some examples to show how these control operators can be used.&lt;&#x2F;p&gt;
&lt;p&gt;Imagine you want to &lt;a rel=&quot;noopener&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;bash.cyberciti.biz&#x2F;guide&#x2F;Source_command&quot;&gt;source&lt;&#x2F;a&gt; the &lt;code&gt;~&#x2F;.profile&lt;&#x2F;code&gt; file, but only if it
is readable:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#191919;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6699cc;&quot;&gt;[ &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fc9354;&quot;&gt;-r &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;~&lt;&#x2F;span&gt;&lt;span&gt;&#x2F;.profile &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6699cc;&quot;&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;; then
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6699cc;&quot;&gt;source &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;~&lt;&#x2F;span&gt;&lt;span&gt;&#x2F;.profile
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;fi
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We can simplify this using control operators:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#191919;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#6699cc;&quot;&gt;[ &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fc9354;&quot;&gt;-r &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;~&lt;&#x2F;span&gt;&lt;span&gt;&#x2F;.profile &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6699cc;&quot;&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;&amp;amp;&amp;amp; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6699cc;&quot;&gt;. &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;~&lt;&#x2F;span&gt;&lt;span&gt;&#x2F;.profile
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Only if the readability check expression is truthful&#x2F;succeeds, we want to source
the file, so we use the &lt;code&gt;&amp;amp;&amp;amp;&lt;&#x2F;code&gt; operator.&lt;&#x2F;p&gt;
&lt;br&gt;
&lt;p&gt;To require invoking user to be root, we can do the following:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#191919;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#6699cc;&quot;&gt;[ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;EUID &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fc9354;&quot;&gt;-ne&lt;&#x2F;span&gt;&lt;span&gt; 0 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6699cc;&quot;&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;&amp;amp;&amp;amp; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6699cc;&quot;&gt;echo&lt;&#x2F;span&gt;&lt;span&gt; You must be root &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;&amp;amp;&amp;amp; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6699cc;&quot;&gt;exit&lt;&#x2F;span&gt;&lt;span&gt; 1
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The &lt;code&gt;echo&lt;&#x2F;code&gt; command exists with &lt;code&gt;0&lt;&#x2F;code&gt;, so this propagates to &lt;code&gt;exit&lt;&#x2F;code&gt; if the
first expression is truthful.&lt;&#x2F;p&gt;
&lt;br&gt;
&lt;p&gt;If we&#x27;d like to print our profile file contents with a success message,
or an error message on reading failure, we can do the following:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#191919;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;cat ~&lt;&#x2F;span&gt;&lt;span&gt;&#x2F;.profile &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;&amp;amp;&amp;amp; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6699cc;&quot;&gt;echo&lt;&#x2F;span&gt;&lt;span&gt; This is your profile &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;|| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6699cc;&quot;&gt;echo&lt;&#x2F;span&gt;&lt;span&gt; Failed to read profile
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;br&gt;
&lt;p&gt;You can make these command sequences as long as you want. Useful for
building install scripts that go through a series of steps. You could define
bash functions for each step and orchestrate the installation like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#191919;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;init &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;&amp;amp;&amp;amp; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;configure &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;&amp;amp;&amp;amp; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;install &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;&amp;amp;&amp;amp; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;cleanup &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;|| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6699cc;&quot;&gt;echo&lt;&#x2F;span&gt;&lt;span&gt; Install failed
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Or format it differently to make long sequences better readable:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#191919;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;init &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;&amp;amp;&amp;amp;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;configure &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;&amp;amp;&amp;amp;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;install &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;&amp;amp;&amp;amp;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;cleanup &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;||
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6699cc;&quot;&gt;echo&lt;&#x2F;span&gt;&lt;span&gt; Install failed
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;nice-to-know&quot;&gt;Nice to know
    &lt;a class=&quot;anchor&quot; href=&quot;#nice-to-know&quot; aria-label=&quot;Anchor link for: nice-to-know&quot;&gt;&lt;&#x2F;a&gt;

&lt;&#x2F;h2&gt;
&lt;p&gt;There are &lt;a rel=&quot;noopener&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;unix.stackexchange.com&#x2F;a&#x2F;159514&#x2F;61092&quot;&gt;many&lt;&#x2F;a&gt; more control operators,
including list terminators, pipe operators, and others.&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;code&gt;[&lt;&#x2F;code&gt; (commonly used in if-statements) isn&#x27;t just a shell feature.
It is a binary on Unix-like systems, usually located at &lt;code&gt;&#x2F;usr&#x2F;bin&#x2F;[&lt;&#x2F;code&gt;,
so it can be used anywhere.
It returns the exit code &lt;code&gt;0&lt;&#x2F;code&gt; if the expression was truthful.&lt;&#x2F;p&gt;
&lt;p&gt;You can chain multiple &lt;code&gt;[ expr ] &amp;amp;&amp;amp; [ expr ]&lt;&#x2F;code&gt; expressions together, they are
commands after all.&lt;&#x2F;p&gt;
&lt;p&gt;Bash also has &lt;code&gt;[[&lt;&#x2F;code&gt;, which is
&lt;a rel=&quot;noopener&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;stackoverflow.com&#x2F;q&#x2F;13542832&#x2F;1000145&quot;&gt;different&lt;&#x2F;a&gt; from &lt;code&gt;[&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;You can wrap multiple commands with &lt;code&gt;{ expr; }&lt;&#x2F;code&gt; to run it as single expression in
your command chain. For example:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#191919;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#6699cc;&quot;&gt;[ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;EUID &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fc9354;&quot;&gt;-ne&lt;&#x2F;span&gt;&lt;span&gt; 0 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6699cc;&quot;&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;&amp;amp;&amp;amp; &lt;&#x2F;span&gt;&lt;span&gt;{ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6699cc;&quot;&gt;echo&lt;&#x2F;span&gt;&lt;span&gt; You must be root&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6699cc;&quot;&gt;exit&lt;&#x2F;span&gt;&lt;span&gt; 1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;; &lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The &lt;code&gt;true&lt;&#x2F;code&gt; and &lt;code&gt;false&lt;&#x2F;code&gt; commands do nothing more than returning &lt;code&gt;0&lt;&#x2F;code&gt; or &lt;code&gt;1&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Bash features &lt;a rel=&quot;noopener&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;www.gnu.org&#x2F;software&#x2F;bash&#x2F;manual&#x2F;html_node&#x2F;Shell-Parameter-Expansion.html#Shell-Parameter-Expansion&quot;&gt;parameter expression&lt;&#x2F;a&gt;.
You can use &lt;code&gt;${EDITOR:=nvim}&lt;&#x2F;code&gt; to set the &lt;code&gt;EDITOR&lt;&#x2F;code&gt; variable to &lt;code&gt;nvim&lt;&#x2F;code&gt; only if
it is empty, so you won&#x27;t even need conditionals.&lt;&#x2F;p&gt;
&lt;p&gt;Most other shells support similar operators. &lt;a rel=&quot;noopener&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;fishshell.com&#x2F;&quot;&gt;fish&lt;&#x2F;a&gt; uses &lt;code&gt;; and&lt;&#x2F;code&gt; and &lt;code&gt;; or&lt;&#x2F;code&gt;, but now &lt;a rel=&quot;noopener&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;fish-shell&#x2F;fish-shell&#x2F;issues&#x2F;4620&quot;&gt;supports&lt;&#x2F;a&gt; &lt;code&gt;&amp;amp;&amp;amp;&lt;&#x2F;code&gt; and &lt;code&gt;||&lt;&#x2F;code&gt; as well in modern versions.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;$_&lt;&#x2F;code&gt; (or &lt;code&gt;Alt+.&lt;&#x2F;code&gt;) is your last used argument (noted by &lt;a rel=&quot;noopener&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;lobste.rs&#x2F;s&#x2F;nao13f&#x2F;elegant_bash_conditionals#c_brp038&quot;&gt;@Diti&lt;&#x2F;a&gt;).
For example:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#191919;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;test&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fc9354;&quot;&gt; -f &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt;FILE&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;&amp;amp;&amp;amp; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6699cc;&quot;&gt;source &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;$&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;|| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#6699cc;&quot;&gt;echo &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot;$&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e9fdac;&quot;&gt;_&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fbe3bf;&quot;&gt; does not exist&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ffffff;&quot;&gt;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ff5e5e;&quot;&gt;&amp;gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#fdb082;&quot;&gt;2
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;A function or script returns the exit code of the last expression. If your last
expression is a command chain as described in this article, you might have an
unexpected exit code. See
&lt;a rel=&quot;noopener&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;www.reddit.com&#x2F;r&#x2F;linux&#x2F;comments&#x2F;lw0ofg&#x2F;elegant_bash_conditionals&#x2F;gpf1lr0&#x2F;&quot;&gt;this&lt;&#x2F;a&gt;
comments.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;closing-thoughts&quot;&gt;Closing thoughts
    &lt;a class=&quot;anchor&quot; href=&quot;#closing-thoughts&quot; aria-label=&quot;Anchor link for: closing-thoughts&quot;&gt;&lt;&#x2F;a&gt;

&lt;&#x2F;h2&gt;
&lt;p&gt;These command sequences with control operators are an elegant alternative for
simple if-statements. I think they look much better and are more expressive
looking at conditional logic.&lt;&#x2F;p&gt;
&lt;p&gt;But, don&#x27;t overuse them. For bigger statements or advanced branching, you should
fall back to if-statements.&lt;&#x2F;p&gt;
&lt;p&gt;I hope this article motivates you to make your shell scripts a little more
elegant in the future by using these operators.&lt;&#x2F;p&gt;
</description>
            </item>
        
    </channel>
</rss>

