title: bash: Run another program in the current directory while running from path
body: <p>I have a java CLI script that converts rgb names to hexcodes (e.g. 144 132 146 becomes #908492). However, I want to be able to run it from any terminal. I put a bash script in the same folder so it could run the file:</p>
  
  <p>The bash script is simple enough, just:</p>
  
  <pre><code>#!/bin/bash
  java rgb2hexConv $1 $2 $3
  </code></pre>
  
  <p>However, when I run the code through the PATH, I get errors related to the file rgb2hexConv not being found.</p>
  
  <p>Diagram:</p>
  
  <pre><code>/
    /home/
      /home/me/
        /home/me/someRandomDir/ (running from here does not work)
        /home/me/utils/ (in path) (running from here works)
          - rgb2hex (bash script)
          - rgb2hexConv.class (java program)
  </code></pre>
  
  <p>My guess is that it's looking for rgb2hexConv in /home/me/someRandomDir/ as opposed to /home/me/utils/. Is there anyway to get the directory of the bash script?</p>
  
  <p>EDIT: Changing the script to use ./rgb2hexConv gives the following:</p>
  
  <pre><code>Exception in thread "main" java.lang.NoClassDefFoundError: //rgb2hexConv
  Caused by: java.lang.ClassNotFoundException: ..rgb2hexConv
  // long stack trace removed
  Could not find the main class: ./rgb2hexConv.  Program will exit.
  </code></pre>
  
  <p>(The bit at the end of the first line is not a comment, but actual output)</p>
  
  <p>EDIT 2: After an attempt at using $0 the following output was recieved</p>
  
  <p>Exception in thread "main" java.lang.NoClassDefFoundError: /home/me/utils/rgb2hex/rgb2hexConv
  Caused by: java.lang.ClassNotFoundException: .home.me.utils.rgb2hex.rgb2hexConv
      // Long Stack Trace
  Could not find the main class: /home/me/utils/rgb2hex/rgb2hexConv.  Program will exit.</p>
  
  <p>Two things about this:</p>
  
  <ol>
  <li>$0 contains the file name as well as a directory</li>
  <li>The <code>java</code> command seems to be replacing "/" with ".".</li>
  </ol>