<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Bash: Get current script name and absolute paths</title>
	<atom:link href="http://www.jtanium.com/2007/12/21/bash-get-current-script-name-and-relative-and-absolute-paths/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jtanium.com/2007/12/21/bash-get-current-script-name-and-relative-and-absolute-paths/</link>
	<description>I jot things down, in hopes of finding them later...</description>
	<lastBuildDate>Tue, 31 Jan 2012 15:07:04 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
	<item>
		<title>By: ishi</title>
		<link>http://www.jtanium.com/2007/12/21/bash-get-current-script-name-and-relative-and-absolute-paths/comment-page-1/#comment-410</link>
		<dc:creator>ishi</dc:creator>
		<pubDate>Wed, 16 Sep 2009 09:17:41 +0000</pubDate>
		<guid isPermaLink="false">http://www.jtanium.com/blog/?p=57#comment-410</guid>
		<description>Speaking of bash, you could do it like this:

pushd $(dirnamr $0) &gt; /dev/null
my_path=$(pwd)
popd &gt; /dev/null &gt; /dev/null

You could do much the same with cd (and cd -), but I don&#039;t remember if &#039;cd -&#039; is any more portable than pushd.  Much the same with just remembering the &#039;old pwd&#039;.

It has a very nice feature of removing junk from the file name, like when you call your script with something like 

../../one/dir/../../..///another/dir/../../../../usr/bin/script.sh

which might result from strange path concatenation (it *does* happen).

Before you &#039;scream&#039;, yes, I know, this method assumes, that the executor has +x for the directory containing the script.  Oh well..  Show me any sane person, who removes x from directories, but permits execution scripts therein ;)</description>
		<content:encoded><![CDATA[<p>Speaking of bash, you could do it like this:</p>
<p>pushd $(dirnamr $0) &gt; /dev/null<br />
my_path=$(pwd)<br />
popd &gt; /dev/null &gt; /dev/null</p>
<p>You could do much the same with cd (and cd -), but I don&#8217;t remember if &#8216;cd -&#8217; is any more portable than pushd.  Much the same with just remembering the &#8216;old pwd&#8217;.</p>
<p>It has a very nice feature of removing junk from the file name, like when you call your script with something like </p>
<p>../../one/dir/../../..///another/dir/../../../../usr/bin/script.sh</p>
<p>which might result from strange path concatenation (it *does* happen).</p>
<p>Before you &#8216;scream&#8217;, yes, I know, this method assumes, that the executor has +x for the directory containing the script.  Oh well..  Show me any sane person, who removes x from directories, but permits execution scripts therein <img src='https://www.jtanium.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jtanium</title>
		<link>http://www.jtanium.com/2007/12/21/bash-get-current-script-name-and-relative-and-absolute-paths/comment-page-1/#comment-402</link>
		<dc:creator>jtanium</dc:creator>
		<pubDate>Wed, 12 Aug 2009 01:35:09 +0000</pubDate>
		<guid isPermaLink="false">http://www.jtanium.com/blog/?p=57#comment-402</guid>
		<description>Bear in mind, that is only going to work if the script is in your path. Me, I&#039;m not going to trust that.</description>
		<content:encoded><![CDATA[<p>Bear in mind, that is only going to work if the script is in your path. Me, I&#8217;m not going to trust that.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mindya Bizness</title>
		<link>http://www.jtanium.com/2007/12/21/bash-get-current-script-name-and-relative-and-absolute-paths/comment-page-1/#comment-401</link>
		<dc:creator>Mindya Bizness</dc:creator>
		<pubDate>Tue, 11 Aug 2009 21:06:10 +0000</pubDate>
		<guid isPermaLink="false">http://www.jtanium.com/blog/?p=57#comment-401</guid>
		<description>in bash you can also use the following to get the path of the script.

BaseDir=$(dirname $(which ${0}))</description>
		<content:encoded><![CDATA[<p>in bash you can also use the following to get the path of the script.</p>
<p>BaseDir=$(dirname $(which ${0}))</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jtanium</title>
		<link>http://www.jtanium.com/2007/12/21/bash-get-current-script-name-and-relative-and-absolute-paths/comment-page-1/#comment-394</link>
		<dc:creator>jtanium</dc:creator>
		<pubDate>Tue, 26 May 2009 13:28:55 +0000</pubDate>
		<guid isPermaLink="false">http://www.jtanium.com/blog/?p=57#comment-394</guid>
		<description>@nicholas,

That&#039;s pretty sweet.  I&#039;ve never seen that before, and I&#039;ll definitely use it.

Thanks.</description>
		<content:encoded><![CDATA[<p>@nicholas,</p>
<p>That&#8217;s pretty sweet.  I&#8217;ve never seen that before, and I&#8217;ll definitely use it.</p>
<p>Thanks.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: nicholas tung</title>
		<link>http://www.jtanium.com/2007/12/21/bash-get-current-script-name-and-relative-and-absolute-paths/comment-page-1/#comment-393</link>
		<dc:creator>nicholas tung</dc:creator>
		<pubDate>Tue, 26 May 2009 06:55:47 +0000</pubDate>
		<guid isPermaLink="false">http://www.jtanium.com/blog/?p=57#comment-393</guid>
		<description>&quot;$0&quot; is the name of the script file. It&#039;s not by default an absolute path.

With respect, this is not a clean solution; readlink as in http://fritzthomas.com/open-source/linux/384-how-to-get-the-absolute-path-within-the-running-bash-script/ is much better. The style is also not very good and breaks with spaces (the link above does too; it&#039;s not my example). Try (with outer quotes)
&quot;$(readlink -f &quot;$0&quot;)&quot;
&quot;$(dirname &quot;$(readlink -f &quot;$0&quot;)&quot;)&quot;

Despite what it looks, the inside of $() seems to start some sort of new environment, so the quotes around the &quot;$0&quot; behave like you&#039;d expect them to.

regards,
Nicholas</description>
		<content:encoded><![CDATA[<p>&#8220;$0&#8243; is the name of the script file. It&#8217;s not by default an absolute path.</p>
<p>With respect, this is not a clean solution; readlink as in <a href="http://fritzthomas.com/open-source/linux/384-how-to-get-the-absolute-path-within-the-running-bash-script/" rel="nofollow">http://fritzthomas.com/open-source/linux/384-how-to-get-the-absolute-path-within-the-running-bash-script/</a> is much better. The style is also not very good and breaks with spaces (the link above does too; it&#8217;s not my example). Try (with outer quotes)<br />
&#8220;$(readlink -f &#8220;$0&#8243;)&#8221;<br />
&#8220;$(dirname &#8220;$(readlink -f &#8220;$0&#8243;)&#8221;)&#8221;</p>
<p>Despite what it looks, the inside of $() seems to start some sort of new environment, so the quotes around the &#8220;$0&#8243; behave like you&#8217;d expect them to.</p>
<p>regards,<br />
Nicholas</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jtanium</title>
		<link>http://www.jtanium.com/2007/12/21/bash-get-current-script-name-and-relative-and-absolute-paths/comment-page-1/#comment-387</link>
		<dc:creator>jtanium</dc:creator>
		<pubDate>Fri, 24 Apr 2009 16:03:05 +0000</pubDate>
		<guid isPermaLink="false">http://www.jtanium.com/blog/?p=57#comment-387</guid>
		<description>Yeah, just to make sure:
&lt;pre&gt;
[jtanium@flobot ~]$ cat script_name.sh 
#!/usr/bin/env bash

echo &quot;           \\$0: $0&quot;

_my_name=`basename $0`
if [ &quot;`echo $0 &#124; cut -c1`&quot; = &quot;/&quot; ]; then
  _my_path=`dirname $0`
else
  _my_path=`pwd`/`echo $0 &#124; sed -e s/$_my_name//`
fi

echo &quot;     Filename: $_my_name&quot;
echo &quot;Absolute path: $_my_path&quot;
[jtanium@flobot ~]$ ./script_name.sh 
           \./script_name.sh: ./script_name.sh
     Filename: script_name.sh
Absolute path: /home/jtanium/./
[jtanium@flobot ~]$ 
&lt;/pre&gt;
It does make me wonder what the deal is the the first echo $0 thing is...</description>
		<content:encoded><![CDATA[<p>Yeah, just to make sure:</p>
<pre>
[jtanium@flobot ~]$ cat script_name.sh
#!/usr/bin/env bash

echo "           \\$0: $0"

_my_name=`basename $0`
if [ "`echo $0 | cut -c1`" = "/" ]; then
  _my_path=`dirname $0`
else
  _my_path=`pwd`/`echo $0 | sed -e s/$_my_name//`
fi

echo "     Filename: $_my_name"
echo "Absolute path: $_my_path"
[jtanium@flobot ~]$ ./script_name.sh
           \./script_name.sh: ./script_name.sh
     Filename: script_name.sh
Absolute path: /home/jtanium/./
[jtanium@flobot ~]$
</pre>
<p>It does make me wonder what the deal is the the first echo $0 thing is&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: iongion</title>
		<link>http://www.jtanium.com/2007/12/21/bash-get-current-script-name-and-relative-and-absolute-paths/comment-page-1/#comment-386</link>
		<dc:creator>iongion</dc:creator>
		<pubDate>Tue, 21 Apr 2009 13:21:32 +0000</pubDate>
		<guid isPermaLink="false">http://www.jtanium.com/blog/?p=57#comment-386</guid>
		<description>Are you sure this works, using this exactly like in your sample of centos 5.3</description>
		<content:encoded><![CDATA[<p>Are you sure this works, using this exactly like in your sample of centos 5.3</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jtanium</title>
		<link>http://www.jtanium.com/2007/12/21/bash-get-current-script-name-and-relative-and-absolute-paths/comment-page-1/#comment-370</link>
		<dc:creator>jtanium</dc:creator>
		<pubDate>Thu, 04 Dec 2008 17:23:28 +0000</pubDate>
		<guid isPermaLink="false">http://www.jtanium.com/blog/?p=57#comment-370</guid>
		<description>That&#039;s what I&#039;m doing to get the file name, here:

_my_name=`basename $0`

The real point is to get the absolute path of the directory containing the script.

If you are familiar with Ruby, you can accomplish the same thing with File.dirname(__FILE__)</description>
		<content:encoded><![CDATA[<p>That&#8217;s what I&#8217;m doing to get the file name, here:</p>
<p>_my_name=`basename $0`</p>
<p>The real point is to get the absolute path of the directory containing the script.</p>
<p>If you are familiar with Ruby, you can accomplish the same thing with File.dirname(__FILE__)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: nanocell</title>
		<link>http://www.jtanium.com/2007/12/21/bash-get-current-script-name-and-relative-and-absolute-paths/comment-page-1/#comment-353</link>
		<dc:creator>nanocell</dc:creator>
		<pubDate>Mon, 24 Nov 2008 15:54:41 +0000</pubDate>
		<guid isPermaLink="false">http://www.jtanium.com/blog/?p=57#comment-353</guid>
		<description>you could just use the &quot;basename&quot; utility to get the filename, e..g
basename /foo/bar/test.txt
returns: 
test.txt</description>
		<content:encoded><![CDATA[<p>you could just use the &#8220;basename&#8221; utility to get the filename, e..g<br />
basename /foo/bar/test.txt<br />
returns:<br />
test.txt</p>
]]></content:encoded>
	</item>
</channel>
</rss>

