<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>tcsh on
A Scripter's Notes</title><link>https://scripter.co/tags/tcsh/</link><description>Recent content in tcsh
on A Scripter's Notes</description><language>en-us</language><managingEditor>kaushal.modi@gmail.com (Kaushal Modi)</managingEditor><webMaster>kaushal.modi@gmail.com (Kaushal Modi)</webMaster><lastBuildDate>Wed, 22 Apr 2026 08:24:58 -0400</lastBuildDate><generator>Hugo -- gohugo.io</generator><docs>https://validator.w3.org/feed/docs/rss2.html</docs><atom:link href="https://scripter.co/tags/tcsh/index.xml" rel="self" type="application/rss+xml"/><item><title>Count Down Timer in Shell</title><link>https://scripter.co/count-down-timer-in-shell/</link><description>&lt;blockquote>&lt;p>I was working on a &lt;code>tcsh&lt;/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.&lt;/p>
&lt;p>While I could simply echo a warning statement and put a &lt;code>sleep 10&lt;/code>, I
wanted the wait time to be shown &lt;strong>live&lt;/strong>.&lt;/p>
&lt;/blockquote>&lt;div class="ox-hugo-toc toc">
&lt;div class="heading">Table of Contents&lt;/div>
&lt;ul>
&lt;li>&lt;a href="#explanation">Explanation&lt;/a>&lt;/li>
&lt;li>&lt;a href="#result">Result&lt;/a>&lt;/li>
&lt;li>&lt;a href="#bash-implementation">Bash implementation&lt;/a>&lt;/li>
&lt;/ul>
&lt;/div>
&lt;!--endtoc-->
&lt;p>So here&amp;rsquo;s what worked pretty nicely &amp;mdash; The warning message is shown to
the user, and the actual wait time countdown is also displayed.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-tcsh" data-lang="tcsh">&lt;span class="line">&lt;span class="cl">&lt;span class="c">#!/usr/bin/env tcsh&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="nb">set &lt;/span>&lt;span class="nv">wait_time&lt;/span> &lt;span class="o">=&lt;/span> 10 &lt;span class="c"># seconds&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="nb">echo&lt;/span> &lt;span class="s2">&amp;#34;Are you sure you meant to run this script?&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="nb">echo&lt;/span> &lt;span class="s2">&amp;#34;This script does something drastic that you would severely regret if you happened to run this script by mistake!&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="nb">echo&lt;/span> &lt;span class="s2">&amp;#34;&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="nb">set &lt;/span>&lt;span class="nv">temp_cnt&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="k">${&lt;/span>&lt;span class="nv">wait_time&lt;/span>&lt;span class="k">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c"># https://www.cyberciti.biz/faq/csh-shell-scripting-loop-example/&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">while&lt;/span> &lt;span class="o">(&lt;/span> &lt;span class="k">${&lt;/span>&lt;span class="nv">temp_cnt&lt;/span>&lt;span class="k">}&lt;/span> &amp;gt;&lt;span class="o">=&lt;/span> 1 &lt;span class="o">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> printf &lt;span class="s2">&amp;#34;\rYou have %2d second(s) remaining to hit Ctrl+C to cancel that operation!&amp;#34;&lt;/span> &lt;span class="k">${&lt;/span>&lt;span class="nv">temp_cnt&lt;/span>&lt;span class="k">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> sleep 1
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> @ temp_cnt--
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">end
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">&lt;/span>&lt;span class="nb">echo&lt;/span> &lt;span class="s2">&amp;#34;&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>
&lt;h2 id="explanation">Explanation&amp;nbsp;&lt;a class="headline-hash no-text-decoration" href="#explanation">#&lt;/a>&lt;/h2>
&lt;ul>
&lt;li>The &lt;code>while&lt;/code> loop runs for &lt;code>$wait_time&lt;/code> times; each time waiting for
a second (&lt;code>sleep 1&lt;/code>) and then decrementing the temporary counter
&lt;code>$temp_cnt&lt;/code>.&lt;/li>
&lt;li>&lt;code>printf&lt;/code> is chosen instead of &lt;code>echo -n&lt;/code> because I wanted to have the
seconds number always hold 2 character places (&lt;code>%2d&lt;/code>).&lt;/li>
&lt;li>The &lt;code>\r&lt;/code> character in &lt;code>printf&lt;/code> makes the magic here. It represents
&lt;em>carriage return&lt;/em> i.e. The cursor will return to the beginning of
the line, and then print the following string, &lt;strong>overwriting&lt;/strong>
whatever there was on that line earlier.
&lt;ul>
&lt;li>&lt;code>printf&lt;/code> acts like &lt;code>echo -n&lt;/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 &lt;code>printf&lt;/code>, you need to do so explicitly by
adding a &lt;code>\n&lt;/code> character.&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;h2 id="result">Result&amp;nbsp;&lt;a class="headline-hash no-text-decoration" href="#result">#&lt;/a>&lt;/h2>
&lt;p>&lt;em>&lt;a href="https://asciinema.org/a/4vk5dayfbj4k19ghra6k67mmw">Click here&lt;/a> to see the animation on asciinema.org.&lt;/em>&lt;/p>
&lt;h2 id="bash-implementation">Bash implementation&amp;nbsp;&lt;a class="headline-hash no-text-decoration" href="#bash-implementation">#&lt;/a>&lt;/h2>
&lt;p>Below is a re-implementation of the above in bash.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="cp">#!/usr/bin/env bash
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="cp">&lt;/span>&lt;span class="nv">wait_time&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="m">10&lt;/span> &lt;span class="c1"># seconds&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="nb">echo&lt;/span> &lt;span class="s2">&amp;#34;Are you sure you meant to run this script?&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="nb">echo&lt;/span> &lt;span class="s2">&amp;#34;This script does something drastic that you would severely regret if you happened to run this script by mistake!&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="nb">echo&lt;/span> &lt;span class="s2">&amp;#34;&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="nv">temp_cnt&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="si">${&lt;/span>&lt;span class="nv">wait_time&lt;/span>&lt;span class="si">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">while&lt;/span> &lt;span class="o">[[&lt;/span> &lt;span class="si">${&lt;/span>&lt;span class="nv">temp_cnt&lt;/span>&lt;span class="si">}&lt;/span> -gt &lt;span class="m">0&lt;/span> &lt;span class="o">]]&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">do&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nb">printf&lt;/span> &lt;span class="s2">&amp;#34;\rYou have %2d second(s) remaining to hit Ctrl+C to cancel that operation!&amp;#34;&lt;/span> &lt;span class="si">${&lt;/span>&lt;span class="nv">temp_cnt&lt;/span>&lt;span class="si">}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> sleep &lt;span class="m">1&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="o">((&lt;/span>temp_cnt--&lt;span class="o">))&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">done&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="nb">echo&lt;/span> &lt;span class="s2">&amp;#34;&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div></description><author>Kaushal.Modi@fakeEmailToMakeValidatorHappy.com (Kaushal Modi)</author><category domain="https://scripter.co/categories/unix">unix</category><category domain="https://scripter.co/categories/shell">shell</category><category domain="https://scripter.co/tags/bash">bash</category><category domain="https://scripter.co/tags/tcsh">tcsh</category><category domain="https://scripter.co/tags/countdown">countdown</category><category domain="https://scripter.co/tags/timer">timer</category><guid>https://scripter.co/count-down-timer-in-shell/</guid><pubDate>Mon, 09 Jan 2017 08:02:25 -0500</pubDate></item><item><title>Check If a Command/Executable Exists from Shell Script</title><link>https://scripter.co/check-if-a-command-exists-from-shell-script/</link><description>&lt;blockquote>Shell script snippets to check if you have an executable or binary
installed in &lt;code>PATH&lt;/code>.&lt;/blockquote>&lt;div class="ox-hugo-toc toc">
&lt;div class="heading">Table of Contents&lt;/div>
&lt;ul>
&lt;li>&lt;a href="#bash-shell">Bash Shell&lt;/a>&lt;/li>
&lt;li>&lt;a href="#tcsh-shell">Tcsh Shell&lt;/a>&lt;/li>
&lt;/ul>
&lt;/div>
&lt;!--endtoc-->
&lt;p>I often need to check if a particular executable is present in the
&lt;code>PATH&lt;/code> before I can proceed with what I am doing in a shell
script. Also, I need to work with both &lt;code>tcsh&lt;/code> and &lt;code>bash&lt;/code>
scripts. Below presents the solution that has worked for these shell
scripts for me.&lt;/p>
&lt;h2 id="bash-shell">Bash Shell&amp;nbsp;&lt;a class="headline-hash no-text-decoration" href="#bash-shell">#&lt;/a>&lt;/h2>
&lt;p>The below solution using &lt;code>hash&lt;/code> was with the help of &lt;a href="https://stackoverflow.com/a/677212/1219634">this SO solution&lt;/a>.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-sh" data-lang="sh">&lt;span class="line">&lt;span class="cl">&lt;span class="k">if&lt;/span> ! &lt;span class="nb">hash&lt;/span> some_exec 2&amp;gt;/dev/null
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">then&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nb">echo&lt;/span> &lt;span class="s2">&amp;#34;&amp;#39;some_exec&amp;#39; was not found in PATH&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">fi&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Here is the &lt;em>tl;dr&lt;/em> from the above SO solution:&lt;/p>
&lt;blockquote>
&lt;p>Where bash is your shell/hashbang, consistently use &lt;code>hash&lt;/code> (for
commands) or &lt;code>type&lt;/code> (to consider built-ins &amp;amp; keywords). When writing a
POSIX script, use &lt;code>command -v&lt;/code>.&lt;/p>
&lt;/blockquote>
&lt;h2 id="tcsh-shell">Tcsh Shell&amp;nbsp;&lt;a class="headline-hash no-text-decoration" href="#tcsh-shell">#&lt;/a>&lt;/h2>
&lt;p>As it turns out, the &lt;code>tcsh&lt;/code> shell does not have the same &lt;code>hash&lt;/code>
command as the &lt;code>bash&lt;/code> shell.&lt;/p>
&lt;p>But the below solution using &lt;code>where&lt;/code> which I found with the help of
&lt;a href="https://stackoverflow.com/a/22058620/1219634">this SO solution&lt;/a> works fine.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-tcsh" data-lang="tcsh">&lt;span class="line">&lt;span class="cl">&lt;span class="k">if&lt;/span> &lt;span class="o">(&lt;/span> &lt;span class="sb">`&lt;/span>&lt;span class="nb">where &lt;/span>some_exec&lt;span class="sb">`&lt;/span> &lt;span class="o">==&lt;/span> &lt;span class="s2">&amp;#34;&amp;#34;&lt;/span> &lt;span class="o">)&lt;/span> &lt;span class="k">then
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k"> &lt;/span>&lt;span class="nb">echo&lt;/span> &lt;span class="s2">&amp;#34;&amp;#39;some_exec&amp;#39; was not found in PATH&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">endif&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div></description><author>Kaushal.Modi@fakeEmailToMakeValidatorHappy.com (Kaushal Modi)</author><category domain="https://scripter.co/categories/unix">unix</category><category domain="https://scripter.co/categories/shell">shell</category><category domain="https://scripter.co/tags/bash">bash</category><category domain="https://scripter.co/tags/tcsh">tcsh</category><category domain="https://scripter.co/tags/executable">executable</category><category domain="https://scripter.co/tags/exists">exists</category><category domain="https://scripter.co/tags/binary">binary</category><guid>https://scripter.co/check-if-a-command-exists-from-shell-script/</guid><pubDate>Wed, 23 Nov 2016 17:07:26 -0500</pubDate></item></channel></rss>