<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Bash on Cheat Sheet</title><link>/tags/bash/</link><description>Recent content in Bash on Cheat Sheet</description><generator>Hugo</generator><language>en</language><lastBuildDate>Tue, 04 Aug 2026 00:00:00 +0000</lastBuildDate><atom:link href="/tags/bash/index.xml" rel="self" type="application/rss+xml"/><item><title>BASH_SOURCE</title><link>/linux/shell/bash-source/</link><pubDate>Tue, 04 Aug 2026 00:00:00 +0000</pubDate><guid>/linux/shell/bash-source/</guid><description>&lt;h1 id="bash_source"&gt;&lt;code&gt;BASH_SOURCE&lt;/code&gt;&lt;a class="anchor" href="#bash_source"&gt;#&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;&lt;code&gt;BASH_SOURCE&lt;/code&gt; is an array variable that contains the source filenames where the corresponding shell function or script is defined.&lt;/p&gt;
&lt;h3 id="bash_source0-vs-0"&gt;&lt;code&gt;${BASH_SOURCE[0]}&lt;/code&gt; vs &lt;code&gt;$0&lt;/code&gt;&lt;a class="anchor" href="#bash_source0-vs-0"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;While &lt;code&gt;$0&lt;/code&gt; is often used to get the name of the script, it behaves differently depending on how the script is invoked. &lt;code&gt;${BASH_SOURCE[0]}&lt;/code&gt; is more reliable for finding the actual file path.&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th style="text-align: left"&gt;Context&lt;/th&gt;
 &lt;th style="text-align: left"&gt;&lt;code&gt;$0&lt;/code&gt;&lt;/th&gt;
 &lt;th style="text-align: left"&gt;&lt;code&gt;${BASH_SOURCE[0]}&lt;/code&gt;&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;Interactive Shell&lt;/strong&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;&lt;code&gt;-bash&lt;/code&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;(empty)&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;Executing Script&lt;/strong&gt; (&lt;code&gt;./app.sh&lt;/code&gt;)&lt;/td&gt;
 &lt;td style="text-align: left"&gt;&lt;code&gt;./app.sh&lt;/code&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;&lt;code&gt;./app.sh&lt;/code&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;Sourcing Script&lt;/strong&gt; (&lt;code&gt;source app.sh&lt;/code&gt;)&lt;/td&gt;
 &lt;td style="text-align: left"&gt;&lt;code&gt;-bash&lt;/code&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;&lt;code&gt;app.sh&lt;/code&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;h3 id="why-use-bash_source0"&gt;Why use &lt;code&gt;${BASH_SOURCE[0]}&lt;/code&gt;?&lt;a class="anchor" href="#why-use-bash_source0"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;When a script is &lt;strong&gt;sourced&lt;/strong&gt; (using &lt;code&gt;source&lt;/code&gt; or &lt;code&gt;.&lt;/code&gt;), &lt;code&gt;$0&lt;/code&gt; remains the name of the calling shell (eg. &lt;code&gt;-bash&lt;/code&gt;), whereas &lt;code&gt;${BASH_SOURCE[0]}&lt;/code&gt; correctly points to the script being sourced. This makes it the standard way to determine the script&amp;rsquo;s directory regardless of how it was executed.&lt;/p&gt;</description></item><item><title>Redirection</title><link>/linux/shell/redirection/</link><pubDate>Tue, 16 Jun 2026 00:00:00 +0000</pubDate><guid>/linux/shell/redirection/</guid><description>&lt;h1 id="redirection"&gt;Redirection&lt;a class="anchor" href="#redirection"&gt;#&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;The order of redirection matters because the shell processes them from left to right.&lt;/p&gt;
&lt;h2 id="-devnull-21-vs-21--devnull"&gt;&lt;code&gt;&amp;gt; /dev/null 2&amp;gt;&amp;amp;1&lt;/code&gt; vs &lt;code&gt;2&amp;gt;&amp;amp;1 &amp;gt; /dev/null&lt;/code&gt;&lt;a class="anchor" href="#-devnull-21-vs-21--devnull"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;h3 id="1--devnull-21"&gt;1. &lt;code&gt;&amp;gt; /dev/null 2&amp;gt;&amp;amp;1&lt;/code&gt;&lt;a class="anchor" href="#1--devnull-21"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Both &lt;strong&gt;stdout&lt;/strong&gt; and &lt;strong&gt;stderr&lt;/strong&gt; are suppressed.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;code&gt;&amp;gt; /dev/null&lt;/code&gt;: Redirects stdout to &lt;code&gt;/dev/null&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;2&amp;gt;&amp;amp;1&lt;/code&gt;: Redirects stderr to the current location of stdout (which is now &lt;code&gt;/dev/null&lt;/code&gt;).&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id="2-21--devnull"&gt;2. &lt;code&gt;2&amp;gt;&amp;amp;1 &amp;gt; /dev/null&lt;/code&gt;&lt;a class="anchor" href="#2-21--devnull"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Only &lt;strong&gt;stdout&lt;/strong&gt; is suppressed; &lt;strong&gt;stderr&lt;/strong&gt; is still printed to the terminal.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;code&gt;2&amp;gt;&amp;amp;1&lt;/code&gt;: Redirects stderr to the current location of stdout (the terminal).&lt;/li&gt;
&lt;li&gt;&lt;code&gt;&amp;gt; /dev/null&lt;/code&gt;: Redirects stdout to &lt;code&gt;/dev/null&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id="heading"&gt;&lt;code&gt;&amp;amp;&amp;gt;&lt;/code&gt;&lt;a class="anchor" href="#heading"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;A Bash shortcut to redirect both stdout and stderr to the same destination. &lt;code&gt;&amp;amp;&amp;gt; &amp;lt;file&amp;gt;&lt;/code&gt; is equivalent to &lt;code&gt;&amp;gt; &amp;lt;file&amp;gt; 2&amp;gt;&amp;amp;1&lt;/code&gt;.&lt;/p&gt;</description></item><item><title>Special Variables</title><link>/linux/shell/special-variables/</link><pubDate>Tue, 16 Jun 2026 00:00:00 +0000</pubDate><guid>/linux/shell/special-variables/</guid><description>&lt;h1 id="special-variables"&gt;Special Variables&lt;a class="anchor" href="#special-variables"&gt;#&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;Commonly used special shell parameters and operators.&lt;/p&gt;
&lt;h2 id="parameters"&gt;Parameters&lt;a class="anchor" href="#parameters"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th style="text-align: left"&gt;Variable&lt;/th&gt;
 &lt;th style="text-align: left"&gt;Description&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;code&gt;$$&lt;/code&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;PID of the current shell&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;code&gt;$?&lt;/code&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Exit status of the last executed command (&lt;code&gt;0&lt;/code&gt; for success)&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;code&gt;$#&lt;/code&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Number of positional parameters&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;code&gt;$*&lt;/code&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;All positional parameters (as a single word)&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;code&gt;$@&lt;/code&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;All positional parameters (as separate words)&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;code&gt;$!&lt;/code&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;PID of the last background command&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;code&gt;$0&lt;/code&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Name of the script or shell (See &lt;a href="../../linux/shell/bash-source/"&gt;&lt;code&gt;BASH_SOURCE&lt;/code&gt;&lt;/a&gt;)&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;code&gt;$1&lt;/code&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;First positional parameter&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;h3 id="-vs-"&gt;&lt;code&gt;$*&lt;/code&gt; vs &lt;code&gt;$@&lt;/code&gt;&lt;a class="anchor" href="#-vs-"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Both &lt;code&gt;$*&lt;/code&gt; and &lt;code&gt;$@&lt;/code&gt; represent all positional parameters starting from &lt;code&gt;$1&lt;/code&gt;. They &lt;strong&gt;do not include&lt;/strong&gt; &lt;code&gt;$0&lt;/code&gt;.&lt;/p&gt;</description></item><item><title>Scripting Tips</title><link>/linux/shell/script-tips/</link><pubDate>Sun, 24 May 2026 00:00:00 +0000</pubDate><guid>/linux/shell/script-tips/</guid><description>&lt;h1 id="scripting-tips"&gt;Scripting Tips&lt;a class="anchor" href="#scripting-tips"&gt;#&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;Best practices and useful tools for shell scripting.&lt;/p&gt;
&lt;h3 id="set--e"&gt;&lt;code&gt;set -e&lt;/code&gt;&lt;a class="anchor" href="#set--e"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Add &lt;code&gt;set -e&lt;/code&gt; at the beginning of your script to make it exit immediately if any command fails. This prevents error propagation.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="background-color:#f7f7f7;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#57606a"&gt;#!/bin/bash
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#6639ba"&gt;set&lt;/span&gt; -e&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id="if-with-grep"&gt;&lt;code&gt;if&lt;/code&gt; with &lt;code&gt;grep&lt;/code&gt;&lt;a class="anchor" href="#if-with-grep"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Since &lt;a href="../../linux/cli/grep/"&gt;&lt;code&gt;grep&lt;/code&gt;&lt;/a&gt; returns an exit code of &lt;code&gt;0&lt;/code&gt; on success and &lt;code&gt;1&lt;/code&gt; on failure, it can be used directly in an &lt;code&gt;if&lt;/code&gt; condition. Use &lt;code&gt;-q, --quiet&lt;/code&gt; to suppress output.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="background-color:#f7f7f7;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#cf222e"&gt;if&lt;/span&gt; grep -q &lt;span style="color:#0a3069"&gt;&amp;#34;pattern&amp;#34;&lt;/span&gt; &amp;lt;file&amp;gt;&lt;span style="color:#1f2328"&gt;;&lt;/span&gt; &lt;span style="color:#cf222e"&gt;then&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#6639ba"&gt;echo&lt;/span&gt; &lt;span style="color:#0a3069"&gt;&amp;#34;Pattern found&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#cf222e"&gt;fi&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id="other-useful-tools"&gt;Other Useful Tools&lt;a class="anchor" href="#other-useful-tools"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="../../linux/cli/trap/"&gt;&lt;code&gt;trap&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="../../linux/cli/awk/"&gt;&lt;code&gt;awk&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="../../linux/cli/xargs/"&gt;&lt;code&gt;xargs&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</description></item><item><title>set -o vi</title><link>/linux/shell/bash-vi-mode/</link><pubDate>Sun, 24 May 2026 00:00:00 +0000</pubDate><guid>/linux/shell/bash-vi-mode/</guid><description>&lt;h1 id="set--o-vi"&gt;&lt;code&gt;set -o vi&lt;/code&gt;&lt;a class="anchor" href="#set--o-vi"&gt;#&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;You can use &lt;code&gt;vim&lt;/code&gt; keybindings in Bash to edit commands.&lt;/p&gt;
&lt;h3 id="configuration"&gt;Configuration&lt;a class="anchor" href="#configuration"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Add the following to your &lt;code&gt;~/.bashrc&lt;/code&gt; file:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="background-color:#f7f7f7;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#6639ba"&gt;set&lt;/span&gt; -o vi&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id="motivation"&gt;Motivation&lt;a class="anchor" href="#motivation"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;For &lt;code&gt;vi&lt;/code&gt; users, leveraging editor muscle memory with commands like &lt;code&gt;x&lt;/code&gt;, &lt;code&gt;dB&lt;/code&gt;, &lt;code&gt;w&lt;/code&gt;, and &lt;code&gt;b&lt;/code&gt; is more intuitive than memorizing shell-native shortcuts like &lt;code&gt;Ctrl+h&lt;/code&gt;, &lt;code&gt;Ctrl+w&lt;/code&gt;, &lt;code&gt;Alt+f&lt;/code&gt;, and &lt;code&gt;Alt+b&lt;/code&gt;.&lt;/p&gt;
&lt;h3 id="key-features-and-tips"&gt;Key Features and Tips&lt;a class="anchor" href="#key-features-and-tips"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Movement:&lt;/strong&gt; Supports &lt;code&gt;w&lt;/code&gt;, &lt;code&gt;W&lt;/code&gt;, &lt;code&gt;b&lt;/code&gt;, &lt;code&gt;B&lt;/code&gt; (words) and &lt;code&gt;f&lt;/code&gt;, &lt;code&gt;F&lt;/code&gt;, &lt;code&gt;t&lt;/code&gt;, &lt;code&gt;T&lt;/code&gt; (character search).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;History Search:&lt;/strong&gt; In Normal mode, press &lt;code&gt;/&lt;/code&gt; to search your command history.
&lt;ul&gt;
&lt;li&gt;eg. Typing &lt;code&gt;/^kubectl&lt;/code&gt; finds recent &lt;code&gt;kubectl&lt;/code&gt; commands. Use &lt;code&gt;n&lt;/code&gt; and &lt;code&gt;N&lt;/code&gt; to navigate results.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Open in Editor (&lt;code&gt;v&lt;/code&gt;):&lt;/strong&gt; Pressing &lt;code&gt;v&lt;/code&gt; in Normal mode opens the current command in the editor defined by &lt;code&gt;&amp;lt;editor&amp;gt;&lt;/code&gt; (eg. &lt;code&gt;vim&lt;/code&gt;). Saving and exiting (&lt;code&gt;:wq&lt;/code&gt;) will execute the command immediately. This is extremely useful for complex multi-line or long commands.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="limitations"&gt;Limitations&lt;a class="anchor" href="#limitations"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;In-line search using &lt;code&gt;/&lt;/code&gt; is not supported (it is mapped to history search); use &lt;code&gt;f&lt;/code&gt;, &lt;code&gt;t&lt;/code&gt;, etc., instead.&lt;/li&gt;
&lt;/ul&gt;</description></item><item><title>Shell Processing Order</title><link>/linux/shell/process-order/</link><pubDate>Sun, 24 May 2026 00:00:00 +0000</pubDate><guid>/linux/shell/process-order/</guid><description>&lt;h1 id="shell-processing-order"&gt;Shell Processing Order&lt;a class="anchor" href="#shell-processing-order"&gt;#&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;The logical sequence Bash follows when processing a command.&lt;/p&gt;
&lt;h3 id="tokenization"&gt;Tokenization&lt;a class="anchor" href="#tokenization"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;The shell reads the input and breaks it into tokens based on metacharacters.&lt;/p&gt;
&lt;h3 id="alias-expansion"&gt;Alias Expansion&lt;a class="anchor" href="#alias-expansion"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Checks if the first word of the command is an alias and replaces it.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Bypassing Aliases:&lt;/strong&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;\COMMAND&lt;/code&gt;: Using a backslash (eg. &lt;code&gt;\ls&lt;/code&gt;) bypasses aliases.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;command COMMAND&lt;/code&gt;: Using the &lt;code&gt;command&lt;/code&gt; built-in bypasses both aliases and shell functions, ensuring the system binary or built-in is executed.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="expansion"&gt;Expansion&lt;a class="anchor" href="#expansion"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Expansions are performed in the following order:&lt;/p&gt;</description></item></channel></rss>