<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-us"><generator uri="https://gohugo.io/" version="0.101.0">Hugo</generator><title type="html">shell on A Scripter's Notes</title><subtitle type="html">Emacs, scripting and anything text oriented.</subtitle><link href="https://scripter.co/categories/shell/" rel="alternate" type="text/html" title="HTML"/><link href="https://scripter.co/categories/shell/index.xml" rel="alternate" type="application/rss+xml" title="RSS"/><link href="https://scripter.co/categories/shell/atom.xml" rel="self" type="application/atom+xml" title="Atom"/><link href="https://scripter.co/categories/shell/jf2feed.json" rel="alternate" type="application/jf2feed+json" title="jf2feed"/><updated>2026-04-22T08:24:57-04:00</updated><author><name>Kaushal Modi</name><email>kaushal.modi@gmail.com</email></author><id>https://scripter.co/categories/shell/</id><entry><title type="html">grep -Po</title><link href="https://scripter.co/grep-po/?utm_source=atom_feed" rel="alternate" type="text/html"/><link href="https://scripter.co/golang-quirk-number-strings-starting-with-0-are-octals/?utm_source=atom_feed" rel="related" type="text/html" title='  Golang Quirk: Number-strings starting with "0" are Octals   '/><link href="https://scripter.co/generics-not-exactly-in-systemverilog/?utm_source=atom_feed" rel="related" type="text/html" title="Generics (not exactly) in SystemVerilog"/><link href="https://scripter.co/sidenotes-using-ox-hugo/?utm_source=atom_feed" rel="related" type="text/html" title="Sidenotes using ox-hugo"/><link href="https://scripter.co/sidenotes-using-only-css/?utm_source=atom_feed" rel="related" type="text/html" title="Sidenotes using only CSS"/><link href="https://scripter.co/notes/string-fns-nim-vs-python/?utm_source=atom_feed" rel="related" type="text/html" title="String Functions: Nim vs Python"/><id>https://scripter.co/grep-po/</id><author><name>Kaushal Modi</name></author><published>2022-02-16T21:34:00-05:00</published><updated>2022-02-16T21:34:00-05:00</updated><content type="html"><![CDATA[<blockquote>Using <code>grep</code> to do substring extraction in shell scripts.</blockquote><div class="ox-hugo-toc toc">
<div class="heading">Table of Contents</div>
<ul>
<li><a href="#grep-po-problem-statement">Problem statement</a></li>
<li><a href="#solution-using-grep-po">Solution using <code>grep -Po</code></a></li>
<li><a href="#arriving-to-this-solution">Arriving to this solution</a></li>
<li><a href="#summary">Summary</a></li>
</ul>
</div>
<!--endtoc-->
<p>I like <a href="https://en.wikipedia.org/wiki/Regular_expression">regular expressions</a>
<span class="sidenote-number"><small class="sidenote">
I recommend using <a href="https://regex101.com/">https://regex101.com/</a> to practice regular
expressions of different flavors (PCRE2, PCRE, Python, etc.) whether
or not you are new to using <abbr aria-label=" regular expression" tabindex=0>regex</abbr>.
</small></span>
as they allow me to be concise and specific about what I need to
search.</p>
<p>And I have liked using regular expressions for many years, ever since
I learned Perl about fifteen years back. I am writing this post as I
am remembering the delight I felt when I realized that I can use the
familiar Perl regular expressions to do string parsing in shell
scripts. I am not exactly sure, but I probably learned about this
<code>grep -Po</code> trick from <em>stackexchange</em> (<a href="#citeproc_bib_item_1">camh, 2011</a>).</p>

<h2 id="grep-po-problem-statement">Problem statement&nbsp;<a class="headline-hash no-text-decoration" href="#grep-po-problem-statement">#</a></h2>


<p>I could be parsing a log file with a line like <code>web report: https://foo.bar/detail.html</code> and I need to extract the
<code>https://foo.bar</code> part to a shell script variable.</p>

<h2 id="solution-using-grep-po">Solution using <code>grep -Po</code>&nbsp;<a class="headline-hash no-text-decoration" href="#solution-using-grep-po">#</a></h2>


<div class="note">
<p>This solution requires a GNU <code>grep</code> version supporting <code>-P</code>, that&rsquo;s
compiled with <code>libpcre</code>.
<span class="sidenote-number"><small class="sidenote">
<em>GNU grep</em> gained the PCRE (<code>-P</code>) feature back <a href="https://git.savannah.gnu.org/cgit/grep.git/commit/?id=05860b2d966701a5a9f70a650d32b30ae2612eeb">in 2000</a>.
</small></span>
Also I have never come across a system or
used one that did not have such a <code>grep</code> version installed.</p>
</div>
<p>I&rsquo;ll throw the solution out here and then dig into the details.</p>
<p><a id="code-snippet--grepPo-example"></a></p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell"><span class="line"><span class="cl"><span class="nb">echo</span> <span class="s2">&#34;def\nabc&#34;</span> <span class="p">|</span> grep -Po <span class="s1">&#39;a\K.(?=c)&#39;</span> <span class="c1"># =&gt; b</span>
</span></span></code></pre></div><div class="src-block-caption">
  <span class="src-block-number"><a href="#code-snippet--grepPo-example">Code Snippet 1</a>:</span>
  Extracting "b" from "abc" using <code>grep -Po</code>
</div>
<p>The <em>grep</em> switches used here are:</p>
<dl>
<dt><code>-P</code></dt>
<dd>Use (P)erl regular expressions. This allows us to use the
<a href="https://www.regular-expressions.info/lookaround.html"><em>look around</em> regex</a> syntax like <code>(?=..)</code> and special characters like
<code>\K</code> (<a href="#citeproc_bib_item_2">“perlre - Perl regular expressions,” n.d.</a>).</dd>
<dt><code>-o</code></dt>
<dd>Print only the matched portion to the (o)utput</dd>
</dl>

<h2 id="arriving-to-this-solution">Arriving to this solution&nbsp;<a class="headline-hash no-text-decoration" href="#arriving-to-this-solution">#</a></h2>


<p>Now I&rsquo;ll start with a basic example and build up to the <a href="#code-snippet--grepPo-example">above
solution</a>.</p>
<dl>
<dt>Problem</dt>
<dd>Let&rsquo;s say I have this text with two lines &ldquo;def&rdquo; and &ldquo;abc&rdquo;
and I want<span class="org-target" id="org-target--wanted-grep-output"></span> to output whatever character is between &ldquo;a&rdquo; and &ldquo;c&rdquo;.</dd>
</dl>
<!--listend-->
<ul>
<li>
<p>Below, the regular expression for matching any character between &ldquo;a&rdquo;
and &ldquo;c&rdquo; ( <code>'a.c'</code> ) is correct, but that will output the whole input
because the <em>grep</em> of that regex succeeded.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell"><span class="line"><span class="cl"><span class="nb">echo</span> <span class="s2">&#34;def\nabc&#34;</span> <span class="p">|</span> grep <span class="s1">&#39;a.c&#39;</span> <span class="c1"># =&gt; def\nabc</span>
</span></span></code></pre></div></li>
<li>
<p>Now we add the <em>grep</em> <code>-o</code> switch so that it outputs only the
matched portion. As the regex is <code>'a.c'</code>​, the <code>-o</code> switch will
output every part of the input that matched that. So the output is
&ldquo;abc&rdquo;. It&rsquo;s still not what we <a href="#org-target--wanted-grep-output">wanted</a>.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell"><span class="line"><span class="cl"><span class="nb">echo</span> <span class="s2">&#34;def\nabc&#34;</span> <span class="p">|</span> grep -o <span class="s1">&#39;a.c&#39;</span> <span class="c1"># =&gt; abc</span>
</span></span></code></pre></div></li>
<li>
<p>Now we bring in the powerful Perl regex feature <em>positive
lookahead</em>.
<span class="sidenote-number"><small class="sidenote">
Positive lookahead is used when you want to match something <span class="underline">only
if</span> it&rsquo;s followed by something else. It&rsquo;s syntax looks like <code>q(?=u)</code>
where that expression matches if a <code>q</code> is followed by a <code>u</code>, without
making the <code>u</code> part of the match &ndash; <a href="https://www.regular-expressions.info/lookaround.html">reference</a>.
</small></span>
But this is still not exactly what we want because &ldquo;a&rdquo; is still
considered as part of the match. Now the output is &ldquo;ab&rdquo;.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell"><span class="line"><span class="cl"><span class="nb">echo</span> <span class="s2">&#34;abc&#34;</span> <span class="p">|</span> grep -Po <span class="s1">&#39;a.(?=c)&#39;</span> <span class="c1"># =&gt; ab</span>
</span></span></code></pre></div></li>
<li>
<p>We only need a special character that marks a point in the regex
that tells &ldquo;don&rsquo;t consider anything before this as part of the
match&rdquo;. The <code>\K</code> special construct described in the <a href="https://perldoc.perl.org/perlre#Lookaround-Assertions">Perl regular
expressions doc</a> as:</p>
<blockquote>
<p>There is a special form of this construct, called <code>\K</code> (available
since Perl 5.10.0), which causes the regex engine to &ldquo;keep&rdquo;
everything it had matched prior to the <code>\K</code> and not include it in
matched string. This effectively provides non-experimental
variable-length lookbehind of any length.</p>
</blockquote>
<p>And, thus we have the final solution:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell"><span class="line"><span class="cl"><span class="nb">echo</span> <span class="s2">&#34;abc&#34;</span> <span class="p">|</span> grep -Po <span class="s1">&#39;a\K.(?=c)&#39;</span> <span class="c1"># =&gt; b</span>
</span></span></code></pre></div></li>
</ul>

<h2 id="summary">Summary&nbsp;<a class="headline-hash no-text-decoration" href="#summary">#</a></h2>


<p>Taking the example from the <a href="#grep-po-problem-statement">problem statement</a>, this will work:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl"><span class="nv">string</span><span class="o">=</span><span class="s2">&#34;web report: https://foo.bar/detail.html&#34;</span>
</span></span><span class="line"><span class="cl"><span class="nv">substring</span><span class="o">=</span><span class="k">$(</span>grep -Po <span class="s1">&#39;web report:\s*\K.*?(?=/detail\.html)&#39;</span> <span class="o">&lt;&lt;&lt;</span> <span class="s2">&#34;</span><span class="si">${</span><span class="nv">string</span><span class="si">}</span><span class="s2">&#34;</span><span class="k">)</span>
</span></span><span class="line"><span class="cl"><span class="nb">echo</span> <span class="s2">&#34;</span><span class="si">${</span><span class="nv">substring</span><span class="si">}</span><span class="s2">&#34;</span>
</span></span></code></pre></div><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">https://foo.bar
</span></span></code></pre></div>
<h2 id="references">References&nbsp;<a class="headline-hash no-text-decoration" href="#references">#</a></h2>


<div class="csl-bib-body">
  <div class="csl-entry"><a id="citeproc_bib_item_1"></a>camh. (2011). Can grep output only specified groupings that match? [Website]. In <i>Unix stackexchange</i>. <a href="https://unix.stackexchange.com/a/13472/57923">https://unix.stackexchange.com/a/13472/57923</a></div>
  <div class="csl-entry"><a id="citeproc_bib_item_2"></a>perlre - Perl regular expressions. (n.d.). [Website]. In <i>Perldoc 5.34.0</i>. Retrieved February 16, 2022, from <a href="https://perldoc.perl.org/perlre">https://perldoc.perl.org/perlre</a></div>
</div>
]]></content><category scheme="https://scripter.co/categories/unix" term="unix" label="unix"/><category scheme="https://scripter.co/categories/shell" term="shell" label="shell"/><category scheme="https://scripter.co/tags/grep" term="grep" label="grep"/><category scheme="https://scripter.co/tags/regex" term="regex" label="regex"/><category scheme="https://scripter.co/tags/string" term="string" label="string"/><category scheme="https://scripter.co/tags/perl" term="perl" label="perl"/><category scheme="https://scripter.co/tags/100daystooffload" term="100daystooffload" label="100DaysToOffload"/></entry><entry><title type="html">Count Down Timer in Shell</title><link href="https://scripter.co/count-down-timer-in-shell/?utm_source=atom_feed" rel="alternate" type="text/html"/><link href="https://scripter.co/check-if-a-command-exists-from-shell-script/?utm_source=atom_feed" rel="related" type="text/html" title="Check If a Command/Executable Exists from Shell Script"/><id>https://scripter.co/count-down-timer-in-shell/</id><author><name>Kaushal Modi</name></author><published>2017-01-09T08:02:25-05:00</published><updated>2017-01-09T08:02:25-05:00</updated><content type="html"><![CDATA[<blockquote><p>I was working on a <code>tcsh</code> script that did some cool stuff. But if a
user ran that script not knowing the true impact of the script, it
could make some bad irreversible changes.</p>
<p>While I could simply echo a warning statement and put a <code>sleep 10</code>, I
wanted the wait time to be shown <strong>live</strong>.</p>
</blockquote><div class="ox-hugo-toc toc">
<div class="heading">Table of Contents</div>
<ul>
<li><a href="#explanation">Explanation</a></li>
<li><a href="#result">Result</a></li>
<li><a href="#bash-implementation">Bash implementation</a></li>
</ul>
</div>
<!--endtoc-->
<p>So here&rsquo;s what worked pretty nicely &mdash; The warning message is shown to
the user, and the actual wait time countdown is also displayed.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-tcsh" data-lang="tcsh"><span class="line"><span class="cl"><span class="c">#!/usr/bin/env tcsh</span>
</span></span><span class="line"><span class="cl"><span class="nb">set </span><span class="nv">wait_time</span> <span class="o">=</span> 10 <span class="c"># seconds</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="nb">echo</span> <span class="s2">&#34;Are you sure you meant to run this script?&#34;</span>
</span></span><span class="line"><span class="cl"><span class="nb">echo</span> <span class="s2">&#34;This script does something drastic that you would severely regret if you happened to run this script by mistake!&#34;</span>
</span></span><span class="line"><span class="cl"><span class="nb">echo</span> <span class="s2">&#34;&#34;</span>
</span></span><span class="line"><span class="cl"><span class="nb">set </span><span class="nv">temp_cnt</span> <span class="o">=</span> <span class="k">${</span><span class="nv">wait_time</span><span class="k">}</span>
</span></span><span class="line"><span class="cl"><span class="c"># https://www.cyberciti.biz/faq/csh-shell-scripting-loop-example/</span>
</span></span><span class="line"><span class="cl"><span class="k">while</span> <span class="o">(</span> <span class="k">${</span><span class="nv">temp_cnt</span><span class="k">}</span> &gt;<span class="o">=</span> 1 <span class="o">)</span>
</span></span><span class="line"><span class="cl">    printf <span class="s2">&#34;\rYou have %2d second(s) remaining to hit Ctrl+C to cancel that operation!&#34;</span> <span class="k">${</span><span class="nv">temp_cnt</span><span class="k">}</span>
</span></span><span class="line"><span class="cl">    sleep 1
</span></span><span class="line"><span class="cl">    @ temp_cnt--
</span></span><span class="line"><span class="cl"><span class="k">end
</span></span></span><span class="line"><span class="cl"><span class="k"></span><span class="nb">echo</span> <span class="s2">&#34;&#34;</span>
</span></span></code></pre></div>
<h2 id="explanation">Explanation&nbsp;<a class="headline-hash no-text-decoration" href="#explanation">#</a></h2>


<ul>
<li>The <code>while</code> loop runs for <code>$wait_time</code> times; each time waiting for
a second (<code>sleep 1</code>) and then decrementing the temporary counter
<code>$temp_cnt</code>.</li>
<li><code>printf</code> is chosen instead of <code>echo -n</code> because I wanted to have the
seconds number always hold 2 character places (<code>%2d</code>).</li>
<li>The <code>\r</code> character in <code>printf</code> makes the magic here. It represents
<em>carriage return</em> i.e. The cursor will return to the beginning of
the line, and then print the following string, <strong>overwriting</strong>
whatever there was on that line earlier.
<ul>
<li><code>printf</code> acts like <code>echo -n</code> i.e. a newline is not inserted
automatically at the end of the printed message. In order to add a
newline at the end for <code>printf</code>, you need to do so explicitly by
adding a <code>\n</code> character.</li>
</ul>
</li>
</ul>

<h2 id="result">Result&nbsp;<a class="headline-hash no-text-decoration" href="#result">#</a></h2>


<p><em><a href="https://asciinema.org/a/4vk5dayfbj4k19ghra6k67mmw">Click here</a> to see the animation on asciinema.org.</em></p>

<h2 id="bash-implementation">Bash implementation&nbsp;<a class="headline-hash no-text-decoration" href="#bash-implementation">#</a></h2>


<p>Below is a re-implementation of the above in bash.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl"><span class="cp">#!/usr/bin/env bash
</span></span></span><span class="line"><span class="cl"><span class="cp"></span><span class="nv">wait_time</span><span class="o">=</span><span class="m">10</span> <span class="c1"># seconds</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="nb">echo</span> <span class="s2">&#34;Are you sure you meant to run this script?&#34;</span>
</span></span><span class="line"><span class="cl"><span class="nb">echo</span> <span class="s2">&#34;This script does something drastic that you would severely regret if you happened to run this script by mistake!&#34;</span>
</span></span><span class="line"><span class="cl"><span class="nb">echo</span> <span class="s2">&#34;&#34;</span>
</span></span><span class="line"><span class="cl"><span class="nv">temp_cnt</span><span class="o">=</span><span class="si">${</span><span class="nv">wait_time</span><span class="si">}</span>
</span></span><span class="line"><span class="cl"><span class="k">while</span> <span class="o">[[</span> <span class="si">${</span><span class="nv">temp_cnt</span><span class="si">}</span> -gt <span class="m">0</span> <span class="o">]]</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="k">do</span>
</span></span><span class="line"><span class="cl">    <span class="nb">printf</span> <span class="s2">&#34;\rYou have %2d second(s) remaining to hit Ctrl+C to cancel that operation!&#34;</span> <span class="si">${</span><span class="nv">temp_cnt</span><span class="si">}</span>
</span></span><span class="line"><span class="cl">    sleep <span class="m">1</span>
</span></span><span class="line"><span class="cl">    <span class="o">((</span>temp_cnt--<span class="o">))</span>
</span></span><span class="line"><span class="cl"><span class="k">done</span>
</span></span><span class="line"><span class="cl"><span class="nb">echo</span> <span class="s2">&#34;&#34;</span>
</span></span></code></pre></div>]]></content><category scheme="https://scripter.co/categories/unix" term="unix" label="unix"/><category scheme="https://scripter.co/categories/shell" term="shell" label="shell"/><category scheme="https://scripter.co/tags/bash" term="bash" label="bash"/><category scheme="https://scripter.co/tags/tcsh" term="tcsh" label="tcsh"/><category scheme="https://scripter.co/tags/countdown" term="countdown" label="countdown"/><category scheme="https://scripter.co/tags/timer" term="timer" label="timer"/></entry><entry><title type="html">Check If a Command/Executable Exists from Shell Script</title><link href="https://scripter.co/check-if-a-command-exists-from-shell-script/?utm_source=atom_feed" rel="alternate" type="text/html"/><id>https://scripter.co/check-if-a-command-exists-from-shell-script/</id><author><name>Kaushal Modi</name></author><published>2016-11-23T17:07:26-05:00</published><updated>2016-11-23T17:07:26-05:00</updated><content type="html"><![CDATA[<blockquote>Shell script snippets to check if you have an executable or binary
installed in <code>PATH</code>.</blockquote><div class="ox-hugo-toc toc">
<div class="heading">Table of Contents</div>
<ul>
<li><a href="#bash-shell">Bash Shell</a></li>
<li><a href="#tcsh-shell">Tcsh Shell</a></li>
</ul>
</div>
<!--endtoc-->
<p>I often need to check if a particular executable is present in the
<code>PATH</code> before I can proceed with what I am doing in a shell
script. Also, I need to work with both <code>tcsh</code> and <code>bash</code>
scripts. Below presents the solution that has worked for these shell
scripts for me.</p>

<h2 id="bash-shell">Bash Shell&nbsp;<a class="headline-hash no-text-decoration" href="#bash-shell">#</a></h2>


<p>The below solution using <code>hash</code> was with the help of <a href="https://stackoverflow.com/a/677212/1219634">this SO solution</a>.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-sh" data-lang="sh"><span class="line"><span class="cl"><span class="k">if</span> ! <span class="nb">hash</span> some_exec 2&gt;/dev/null
</span></span><span class="line"><span class="cl"><span class="k">then</span>
</span></span><span class="line"><span class="cl">    <span class="nb">echo</span> <span class="s2">&#34;&#39;some_exec&#39; was not found in PATH&#34;</span>
</span></span><span class="line"><span class="cl"><span class="k">fi</span>
</span></span></code></pre></div><p>Here is the <em>tl;dr</em> from the above SO solution:</p>
<blockquote>
<p>Where bash is your shell/hashbang, consistently use <code>hash</code> (for
commands) or <code>type</code> (to consider built-ins &amp; keywords). When writing a
POSIX script, use <code>command -v</code>.</p>
</blockquote>

<h2 id="tcsh-shell">Tcsh Shell&nbsp;<a class="headline-hash no-text-decoration" href="#tcsh-shell">#</a></h2>


<p>As it turns out, the <code>tcsh</code> shell does not have the same <code>hash</code>
command as the <code>bash</code> shell.</p>
<p>But the below solution using <code>where</code> which I found with the help of
<a href="https://stackoverflow.com/a/22058620/1219634">this SO solution</a> works fine.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-tcsh" data-lang="tcsh"><span class="line"><span class="cl"><span class="k">if</span> <span class="o">(</span> <span class="sb">`</span><span class="nb">where </span>some_exec<span class="sb">`</span> <span class="o">==</span> <span class="s2">&#34;&#34;</span> <span class="o">)</span> <span class="k">then
</span></span></span><span class="line"><span class="cl"><span class="k">    </span><span class="nb">echo</span> <span class="s2">&#34;&#39;some_exec&#39; was not found in PATH&#34;</span>
</span></span><span class="line"><span class="cl"><span class="k">endif</span>
</span></span></code></pre></div>]]></content><category scheme="https://scripter.co/categories/unix" term="unix" label="unix"/><category scheme="https://scripter.co/categories/shell" term="shell" label="shell"/><category scheme="https://scripter.co/tags/bash" term="bash" label="bash"/><category scheme="https://scripter.co/tags/tcsh" term="tcsh" label="tcsh"/><category scheme="https://scripter.co/tags/executable" term="executable" label="executable"/><category scheme="https://scripter.co/tags/exists" term="exists" label="exists"/><category scheme="https://scripter.co/tags/binary" term="binary" label="binary"/></entry><entry><title type="html">Get current directory name without path</title><link href="https://scripter.co/get-current-directory-name-without-path/?utm_source=atom_feed" rel="alternate" type="text/html"/><id>https://scripter.co/get-current-directory-name-without-path/</id><published>2014-03-04T13:48:01-05:00</published><updated>2014-03-04T13:48:01-05:00</updated><content type="html"><![CDATA[<p>This post shows how to get the current directory name without the
preceeding path, using <code>awk</code> or <code>rev</code>+<code>cut</code> or the boring <code>basename</code>.</p>
<ul>
<li>awk</li>
</ul>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-sh" data-lang="sh"><span class="line"><span class="cl"><span class="nb">pwd</span> <span class="p">|</span> awk -F/ <span class="s1">&#39;{print $NF}&#39;</span>
</span></span></code></pre></div><ul>
<li>rev and cut</li>
</ul>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-sh" data-lang="sh"><span class="line"><span class="cl"><span class="nb">pwd</span> <span class="p">|</span> rev <span class="p">|</span> cut -d/ -f <span class="m">1</span> <span class="p">|</span> rev
</span></span></code></pre></div><ul>
<li>basename</li>
</ul>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-sh" data-lang="sh"><span class="line"><span class="cl">basename <span class="sb">`</span><span class="nb">pwd</span><span class="sb">`</span>
</span></span></code></pre></div>]]></content><category scheme="https://scripter.co/categories/awk" term="awk" label="awk"/><category scheme="https://scripter.co/categories/shell" term="shell" label="shell"/></entry></feed>