<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>exists on
A Scripter's Notes</title><link>https://scripter.co/tags/exists/</link><description>Recent content in exists
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/exists/index.xml" rel="self" type="application/rss+xml"/><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>