<?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">exists on A Scripter's Notes</title><subtitle type="html">Emacs, scripting and anything text oriented.</subtitle><link href="https://scripter.co/tags/exists/" rel="alternate" type="text/html" title="HTML"/><link href="https://scripter.co/tags/exists/index.xml" rel="alternate" type="application/rss+xml" title="RSS"/><link href="https://scripter.co/tags/exists/atom.xml" rel="self" type="application/atom+xml" title="Atom"/><link href="https://scripter.co/tags/exists/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/exists/</id><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></feed>