<?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">timer on A Scripter's Notes</title><subtitle type="html">Emacs, scripting and anything text oriented.</subtitle><link href="https://scripter.co/tags/timer/" rel="alternate" type="text/html" title="HTML"/><link href="https://scripter.co/tags/timer/index.xml" rel="alternate" type="application/rss+xml" title="RSS"/><link href="https://scripter.co/tags/timer/atom.xml" rel="self" type="application/atom+xml" title="Atom"/><link href="https://scripter.co/tags/timer/jf2feed.json" rel="alternate" type="application/jf2feed+json" title="jf2feed"/><updated>2026-04-22T08:24:58-04:00</updated><author><name>Kaushal Modi</name><email>kaushal.modi@gmail.com</email></author><id>https://scripter.co/tags/timer/</id><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></feed>