<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>WiredRevolution.com &#187; bash</title>
	<atom:link href="http://www.wiredrevolution.com/category/bash-programming/feed" rel="self" type="application/rss+xml" />
	<link>http://www.wiredrevolution.com</link>
	<description>A Bit of Linux Wisdom</description>
	<lastBuildDate>Wed, 18 Jan 2012 22:45:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Get a Listing of Directories in a Subdirectory</title>
		<link>http://www.wiredrevolution.com/bash-programming/get-a-listing-of-directories-in-a-subdirectory?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=get-a-listing-of-directories-in-a-subdirectory</link>
		<comments>http://www.wiredrevolution.com/bash-programming/get-a-listing-of-directories-in-a-subdirectory#comments</comments>
		<pubDate>Fri, 18 Feb 2011 19:13:42 +0000</pubDate>
		<dc:creator>Ryan</dc:creator>
				<category><![CDATA[bash]]></category>
		<category><![CDATA[command]]></category>
		<category><![CDATA[directory]]></category>
		<category><![CDATA[filter]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[list]]></category>
		<category><![CDATA[ls]]></category>
		<category><![CDATA[only]]></category>
		<category><![CDATA[output]]></category>
		<category><![CDATA[subdirectories]]></category>

		<guid isPermaLink="false">http://www.wiredrevolution.com/?p=1556</guid>
		<description><![CDATA[<img src="http://www.wiredrevolution.com/wp-content/uploads/bash_icon.png" width="80" height="34" alt="" title="bash" /><br/>Here is a great little command line trick to list only the subdirectories in your current working directory. $ ls -d */ Related postsSearch for files with the find command Command substitution in BASH


Related posts<ol><li><a href='http://www.wiredrevolution.com/commands/search-for-files-with-the-find-command' rel='bookmark' title='Permanent Link: Search for files with the find command'>Search for files with the find command</a></li>
<li><a href='http://www.wiredrevolution.com/bash-programming/command-substitution-in-bash' rel='bookmark' title='Permanent Link: Command substitution in BASH'>Command substitution in BASH</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<img src="http://www.wiredrevolution.com/wp-content/uploads/bash_icon.png" width="80" height="34" alt="" title="bash" /><br/><p>Here is a great little command line trick to list only the subdirectories in your current working directory.</p>
<p><code>$ ls -d */</code></p>


<p>Related posts<ol><li><a href='http://www.wiredrevolution.com/commands/search-for-files-with-the-find-command' rel='bookmark' title='Permanent Link: Search for files with the find command'>Search for files with the find command</a></li>
<li><a href='http://www.wiredrevolution.com/bash-programming/command-substitution-in-bash' rel='bookmark' title='Permanent Link: Command substitution in BASH'>Command substitution in BASH</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.wiredrevolution.com/bash-programming/get-a-listing-of-directories-in-a-subdirectory/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Echo text without a trailing newline</title>
		<link>http://www.wiredrevolution.com/bash-programming/echo-text-without-a-trailing-newline?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=echo-text-without-a-trailing-newline</link>
		<comments>http://www.wiredrevolution.com/bash-programming/echo-text-without-a-trailing-newline#comments</comments>
		<pubDate>Fri, 18 Feb 2011 18:28:20 +0000</pubDate>
		<dc:creator>Ryan</dc:creator>
				<category><![CDATA[bash]]></category>
		<category><![CDATA[carrage return]]></category>
		<category><![CDATA[command]]></category>
		<category><![CDATA[continuation]]></category>
		<category><![CDATA[echo]]></category>
		<category><![CDATA[escape]]></category>
		<category><![CDATA[format]]></category>
		<category><![CDATA[line break]]></category>
		<category><![CDATA[newline]]></category>
		<category><![CDATA[output]]></category>
		<category><![CDATA[stop]]></category>
		<category><![CDATA[supress]]></category>
		<category><![CDATA[terminal]]></category>
		<category><![CDATA[text]]></category>
		<category><![CDATA[trailing]]></category>

		<guid isPermaLink="false">http://www.wiredrevolution.com/?p=1552</guid>
		<description><![CDATA[<img src="http://www.wiredrevolution.com/wp-content/uploads/bash_icon.png" width="80" height="34" alt="" title="bash" /><br/>By default the echo command will include a trailing newline at the end of a string. There are many times when you may want to suppress this newline for example when you want to format text in a certain way.There are a couple easy ways to do this. First you can use the &#8216;-n&#8217; option. [...]


Related posts<ol><li><a href='http://www.wiredrevolution.com/bash-programming/customize-the-bash-ps1-command-prompt' rel='bookmark' title='Permanent Link: Customize the BASH PS1 command prompt'>Customize the BASH PS1 command prompt</a></li>
<li><a href='http://www.wiredrevolution.com/bash-programming/command-substitution-in-bash' rel='bookmark' title='Permanent Link: Command substitution in BASH'>Command substitution in BASH</a></li>
<li><a href='http://www.wiredrevolution.com/bash-programming/find-the-exit-status-of-a-previous-command-in-bash' rel='bookmark' title='Permanent Link: Find the exit status of a previous command in Bash'>Find the exit status of a previous command in Bash</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<img src="http://www.wiredrevolution.com/wp-content/uploads/bash_icon.png" width="80" height="34" alt="" title="bash" /><br/><p>By default the echo command will include a trailing newline at the end of a string. There are many times when you may want to suppress this newline for example when you want to format text in a certain way.There are a couple easy ways to do this.</p>
<p>First you can use the &#8216;-n&#8217; option.<br />
<code>$ echo -n "hello"</code></p>
<p>You can also use the &#8216;-e&#8217; option which enables interpretation of backslash escapes. Followed by a &#8216;\c&#8217; escape character to signal continuation (no automatic line break).<br />
<code>$ echo -e "hello\c"</code></p>


<p>Related posts<ol><li><a href='http://www.wiredrevolution.com/bash-programming/customize-the-bash-ps1-command-prompt' rel='bookmark' title='Permanent Link: Customize the BASH PS1 command prompt'>Customize the BASH PS1 command prompt</a></li>
<li><a href='http://www.wiredrevolution.com/bash-programming/command-substitution-in-bash' rel='bookmark' title='Permanent Link: Command substitution in BASH'>Command substitution in BASH</a></li>
<li><a href='http://www.wiredrevolution.com/bash-programming/find-the-exit-status-of-a-previous-command-in-bash' rel='bookmark' title='Permanent Link: Find the exit status of a previous command in Bash'>Find the exit status of a previous command in Bash</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.wiredrevolution.com/bash-programming/echo-text-without-a-trailing-newline/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Find the exit status of a previous command in Bash</title>
		<link>http://www.wiredrevolution.com/bash-programming/find-the-exit-status-of-a-previous-command-in-bash?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=find-the-exit-status-of-a-previous-command-in-bash</link>
		<comments>http://www.wiredrevolution.com/bash-programming/find-the-exit-status-of-a-previous-command-in-bash#comments</comments>
		<pubDate>Fri, 14 Jan 2011 17:30:01 +0000</pubDate>
		<dc:creator>Ryan</dc:creator>
				<category><![CDATA[bash]]></category>
		<category><![CDATA[$?]]></category>
		<category><![CDATA[access]]></category>
		<category><![CDATA[command]]></category>
		<category><![CDATA[find]]></category>
		<category><![CDATA[print]]></category>
		<category><![CDATA[return]]></category>
		<category><![CDATA[return code]]></category>
		<category><![CDATA[return statis]]></category>
		<category><![CDATA[return value]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[shell]]></category>
		<category><![CDATA[statis]]></category>
		<category><![CDATA[variable]]></category>

		<guid isPermaLink="false">http://www.wiredrevolution.com/?p=1464</guid>
		<description><![CDATA[<img src="http://www.wiredrevolution.com/wp-content/uploads/bash_icon.png" width="80" height="34" alt="" title="bash" /><br/>Most programs will return an exit status of 0 if the program was successful, while a non-zero exit status usually indicates an error. You can find the exit status or exit code of the previously executed command by accessing the &#8220;$?&#8221; shell variable. $ COMMAND $ echo $? Typically when a command terminates on a [...]


Related posts<ol><li><a href='http://www.wiredrevolution.com/bash-programming/find-the-pid-of-a-background-child-process-in-bash' rel='bookmark' title='Permanent Link: Find the PID of a background child process in Bash'>Find the PID of a background child process in Bash</a></li>
<li><a href='http://www.wiredrevolution.com/bash-programming/find-the-pid-of-the-current-bash-script' rel='bookmark' title='Permanent Link: Find the PID of the current Bash script'>Find the PID of the current Bash script</a></li>
<li><a href='http://www.wiredrevolution.com/bash-programming/find-the-parent-pid-of-a-bash-script' rel='bookmark' title='Permanent Link: Find the parent PID of a Bash Script'>Find the parent PID of a Bash Script</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<img src="http://www.wiredrevolution.com/wp-content/uploads/bash_icon.png" width="80" height="34" alt="" title="bash" /><br/><p>Most programs will return an exit status of 0 if the program was successful, while a non-zero exit status usually indicates an error. You can find the exit status or exit code of the previously executed command by accessing the &#8220;<strong>$?</strong>&#8221; shell variable.</p>
<p><code>$ COMMAND<br />
$ echo $?</code></p>
<p>Typically when a command terminates on a fatal signal whose number is N, Bash uses the value 128+N as the exit status. If a command is not found, the child process created to execute it returns a status of 127. If a command is found but is not executable, the return status is 126.</p>
<p>You can also use the exit status in a conditional.</p>
<p><code>#!/bin/bash<br />
COMMAND<br />
STATUS=$?<br />
if [ $STATUS -eq 0 ]; then<br />
   echo "Command Successful"<br />
else<br />
   echo "Command Failed"<br />
fi</code></p>


<p>Related posts<ol><li><a href='http://www.wiredrevolution.com/bash-programming/find-the-pid-of-a-background-child-process-in-bash' rel='bookmark' title='Permanent Link: Find the PID of a background child process in Bash'>Find the PID of a background child process in Bash</a></li>
<li><a href='http://www.wiredrevolution.com/bash-programming/find-the-pid-of-the-current-bash-script' rel='bookmark' title='Permanent Link: Find the PID of the current Bash script'>Find the PID of the current Bash script</a></li>
<li><a href='http://www.wiredrevolution.com/bash-programming/find-the-parent-pid-of-a-bash-script' rel='bookmark' title='Permanent Link: Find the parent PID of a Bash Script'>Find the parent PID of a Bash Script</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.wiredrevolution.com/bash-programming/find-the-exit-status-of-a-previous-command-in-bash/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Find the parent PID of a Bash Script</title>
		<link>http://www.wiredrevolution.com/bash-programming/find-the-parent-pid-of-a-bash-script?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=find-the-parent-pid-of-a-bash-script</link>
		<comments>http://www.wiredrevolution.com/bash-programming/find-the-parent-pid-of-a-bash-script#comments</comments>
		<pubDate>Sun, 09 Jan 2011 17:53:03 +0000</pubDate>
		<dc:creator>Ryan</dc:creator>
				<category><![CDATA[bash]]></category>
		<category><![CDATA[$PPID]]></category>
		<category><![CDATA[access]]></category>
		<category><![CDATA[child]]></category>
		<category><![CDATA[find]]></category>
		<category><![CDATA[parent]]></category>
		<category><![CDATA[PID]]></category>
		<category><![CDATA[print]]></category>
		<category><![CDATA[process]]></category>
		<category><![CDATA[process ID]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[shell]]></category>
		<category><![CDATA[variable]]></category>

		<guid isPermaLink="false">http://www.wiredrevolution.com/?p=1449</guid>
		<description><![CDATA[<img src="http://www.wiredrevolution.com/wp-content/uploads/bash_icon.png" width="80" height="34" alt="" title="bash" /><br/>For any number of reasons you may want to know the parent process ID of the current Bash script. You can find the parent process of the current Bash script or shell by printing the &#8216;$PPID&#8216; shell variable. $ echo $PPID20341 Related postsFind the PID of the current Bash script Find the PID of a [...]


Related posts<ol><li><a href='http://www.wiredrevolution.com/bash-programming/find-the-pid-of-the-current-bash-script' rel='bookmark' title='Permanent Link: Find the PID of the current Bash script'>Find the PID of the current Bash script</a></li>
<li><a href='http://www.wiredrevolution.com/bash-programming/find-the-pid-of-a-background-child-process-in-bash' rel='bookmark' title='Permanent Link: Find the PID of a background child process in Bash'>Find the PID of a background child process in Bash</a></li>
<li><a href='http://www.wiredrevolution.com/bash-programming/find-the-exit-status-of-a-previous-command-in-bash' rel='bookmark' title='Permanent Link: Find the exit status of a previous command in Bash'>Find the exit status of a previous command in Bash</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<img src="http://www.wiredrevolution.com/wp-content/uploads/bash_icon.png" width="80" height="34" alt="" title="bash" /><br/><p>For any number of reasons you may want to know the parent process ID of the current Bash script. You can find the parent process of the current Bash script or shell by printing the &#8216;<strong>$PPID</strong>&#8216; shell variable.</p>
<p><code>$ echo $PPID</code><code>20341</code></p>


<p>Related posts<ol><li><a href='http://www.wiredrevolution.com/bash-programming/find-the-pid-of-the-current-bash-script' rel='bookmark' title='Permanent Link: Find the PID of the current Bash script'>Find the PID of the current Bash script</a></li>
<li><a href='http://www.wiredrevolution.com/bash-programming/find-the-pid-of-a-background-child-process-in-bash' rel='bookmark' title='Permanent Link: Find the PID of a background child process in Bash'>Find the PID of a background child process in Bash</a></li>
<li><a href='http://www.wiredrevolution.com/bash-programming/find-the-exit-status-of-a-previous-command-in-bash' rel='bookmark' title='Permanent Link: Find the exit status of a previous command in Bash'>Find the exit status of a previous command in Bash</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.wiredrevolution.com/bash-programming/find-the-parent-pid-of-a-bash-script/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Find the PID of a background child process in Bash</title>
		<link>http://www.wiredrevolution.com/bash-programming/find-the-pid-of-a-background-child-process-in-bash?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=find-the-pid-of-a-background-child-process-in-bash</link>
		<comments>http://www.wiredrevolution.com/bash-programming/find-the-pid-of-a-background-child-process-in-bash#comments</comments>
		<pubDate>Sun, 09 Jan 2011 17:47:57 +0000</pubDate>
		<dc:creator>Ryan</dc:creator>
				<category><![CDATA[bash]]></category>
		<category><![CDATA[$!]]></category>
		<category><![CDATA[access]]></category>
		<category><![CDATA[background]]></category>
		<category><![CDATA[child]]></category>
		<category><![CDATA[PID]]></category>
		<category><![CDATA[print]]></category>
		<category><![CDATA[process]]></category>
		<category><![CDATA[process ID]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[shell]]></category>
		<category><![CDATA[variable]]></category>
		<category><![CDATA[view]]></category>

		<guid isPermaLink="false">http://www.wiredrevolution.com/?p=1441</guid>
		<description><![CDATA[<img src="http://www.wiredrevolution.com/wp-content/uploads/bash_icon.png" width="80" height="34" alt="" title="bash" /><br/>In order to control or monitor background child processes from a shell script you will need to know the PID of the child. Bash stores the PID of the last process executed in the &#8220;$!&#8221; shell variable. If you start a background process in an interactive shell it will output the PID of the child. [...]


Related posts<ol><li><a href='http://www.wiredrevolution.com/bash-programming/find-the-pid-of-the-current-bash-script' rel='bookmark' title='Permanent Link: Find the PID of the current Bash script'>Find the PID of the current Bash script</a></li>
<li><a href='http://www.wiredrevolution.com/bash-programming/find-the-parent-pid-of-a-bash-script' rel='bookmark' title='Permanent Link: Find the parent PID of a Bash Script'>Find the parent PID of a Bash Script</a></li>
<li><a href='http://www.wiredrevolution.com/bash-programming/find-the-exit-status-of-a-previous-command-in-bash' rel='bookmark' title='Permanent Link: Find the exit status of a previous command in Bash'>Find the exit status of a previous command in Bash</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<img src="http://www.wiredrevolution.com/wp-content/uploads/bash_icon.png" width="80" height="34" alt="" title="bash" /><br/><p>In order to control or monitor background child processes from a shell script you will need to know the PID of the child. Bash stores the PID of the last process executed in the &#8220;$!&#8221; shell variable.</p>
<p>If you start a background process in an interactive shell it will output the PID of the child.<br />
<code>$ sleep 1 &#038;</code><code>[1]20450</code></p>
<p>Since thee PID of the last command set to run in the background by the current shell or script is stored in &#8216;<strong>$!</strong>&#8216; variable, you can simply echo this variable to print the PID of the previous sleep command.<br />
<code>$ echo $!</code><code>20450</code></p>


<p>Related posts<ol><li><a href='http://www.wiredrevolution.com/bash-programming/find-the-pid-of-the-current-bash-script' rel='bookmark' title='Permanent Link: Find the PID of the current Bash script'>Find the PID of the current Bash script</a></li>
<li><a href='http://www.wiredrevolution.com/bash-programming/find-the-parent-pid-of-a-bash-script' rel='bookmark' title='Permanent Link: Find the parent PID of a Bash Script'>Find the parent PID of a Bash Script</a></li>
<li><a href='http://www.wiredrevolution.com/bash-programming/find-the-exit-status-of-a-previous-command-in-bash' rel='bookmark' title='Permanent Link: Find the exit status of a previous command in Bash'>Find the exit status of a previous command in Bash</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.wiredrevolution.com/bash-programming/find-the-pid-of-a-background-child-process-in-bash/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Find the PID of the current Bash script</title>
		<link>http://www.wiredrevolution.com/bash-programming/find-the-pid-of-the-current-bash-script?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=find-the-pid-of-the-current-bash-script</link>
		<comments>http://www.wiredrevolution.com/bash-programming/find-the-pid-of-the-current-bash-script#comments</comments>
		<pubDate>Sun, 09 Jan 2011 17:18:51 +0000</pubDate>
		<dc:creator>Ryan</dc:creator>
				<category><![CDATA[bash]]></category>
		<category><![CDATA[$$]]></category>
		<category><![CDATA[access]]></category>
		<category><![CDATA[current]]></category>
		<category><![CDATA[environment variable]]></category>
		<category><![CDATA[find]]></category>
		<category><![CDATA[PID]]></category>
		<category><![CDATA[print]]></category>
		<category><![CDATA[process ID]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[shell]]></category>
		<category><![CDATA[variable]]></category>

		<guid isPermaLink="false">http://www.wiredrevolution.com/?p=1438</guid>
		<description><![CDATA[<img src="http://www.wiredrevolution.com/wp-content/uploads/bash_icon.png" width="80" height="34" alt="" title="bash" /><br/>In many cases you will need to determine the PID of a current Bash script or shell. Bash stores a specific variable that allows you to view the process ID of the current shell &#8220;$$&#8216;. You can echo the $$ to print the current PID. $ echo $$23490 Related postsFind the parent PID of a [...]


Related posts<ol><li><a href='http://www.wiredrevolution.com/bash-programming/find-the-parent-pid-of-a-bash-script' rel='bookmark' title='Permanent Link: Find the parent PID of a Bash Script'>Find the parent PID of a Bash Script</a></li>
<li><a href='http://www.wiredrevolution.com/bash-programming/find-the-pid-of-a-background-child-process-in-bash' rel='bookmark' title='Permanent Link: Find the PID of a background child process in Bash'>Find the PID of a background child process in Bash</a></li>
<li><a href='http://www.wiredrevolution.com/bash-programming/find-the-exit-status-of-a-previous-command-in-bash' rel='bookmark' title='Permanent Link: Find the exit status of a previous command in Bash'>Find the exit status of a previous command in Bash</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<img src="http://www.wiredrevolution.com/wp-content/uploads/bash_icon.png" width="80" height="34" alt="" title="bash" /><br/><p>In many cases you will need to determine the PID of a current Bash script or shell. Bash stores a specific variable that allows you to view the process ID of the current shell &#8220;<strong>$$</strong>&#8216;.</p>
<p>You can echo the $$ to print the current PID.<br />
<code>$ echo $$</code><code>23490</code></p>


<p>Related posts<ol><li><a href='http://www.wiredrevolution.com/bash-programming/find-the-parent-pid-of-a-bash-script' rel='bookmark' title='Permanent Link: Find the parent PID of a Bash Script'>Find the parent PID of a Bash Script</a></li>
<li><a href='http://www.wiredrevolution.com/bash-programming/find-the-pid-of-a-background-child-process-in-bash' rel='bookmark' title='Permanent Link: Find the PID of a background child process in Bash'>Find the PID of a background child process in Bash</a></li>
<li><a href='http://www.wiredrevolution.com/bash-programming/find-the-exit-status-of-a-previous-command-in-bash' rel='bookmark' title='Permanent Link: Find the exit status of a previous command in Bash'>Find the exit status of a previous command in Bash</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.wiredrevolution.com/bash-programming/find-the-pid-of-the-current-bash-script/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Have a Bash script determine it&#8217;s own location</title>
		<link>http://www.wiredrevolution.com/bash-programming/have-a-bash-script-determine-its-own-location?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=have-a-bash-script-determine-its-own-location</link>
		<comments>http://www.wiredrevolution.com/bash-programming/have-a-bash-script-determine-its-own-location#comments</comments>
		<pubDate>Wed, 15 Dec 2010 18:49:37 +0000</pubDate>
		<dc:creator>Ryan</dc:creator>
				<category><![CDATA[bash]]></category>
		<category><![CDATA[absolute]]></category>
		<category><![CDATA[BASH]]></category>
		<category><![CDATA[canonicalized]]></category>
		<category><![CDATA[determine]]></category>
		<category><![CDATA[directory]]></category>
		<category><![CDATA[dirname]]></category>
		<category><![CDATA[find]]></category>
		<category><![CDATA[locate]]></category>
		<category><![CDATA[location]]></category>
		<category><![CDATA[path]]></category>
		<category><![CDATA[pathname]]></category>
		<category><![CDATA[pwd]]></category>
		<category><![CDATA[readlink]]></category>
		<category><![CDATA[relative]]></category>
		<category><![CDATA[script]]></category>

		<guid isPermaLink="false">http://www.wiredrevolution.com/?p=1333</guid>
		<description><![CDATA[<img src="http://www.wiredrevolution.com/wp-content/uploads/bash_icon.png" width="80" height="34" alt="" title="bash" /><br/>At some point it may be helpful to have a BASH script dynamically determine the location of itself when executed from anywhere on the system. The following code will produce the canonicalized absolute pathname of the script, as well as the directory that it resides in. The script first determines if the the first argument [...]


Related posts<ol><li><a href='http://www.wiredrevolution.com/bash-programming/convert-a-relative-path-to-absolute-path-in-bash' rel='bookmark' title='Permanent Link: Convert a relative path to absolute path in BASH'>Convert a relative path to absolute path in BASH</a></li>
<li><a href='http://www.wiredrevolution.com/bash-programming/find-the-pid-of-the-current-bash-script' rel='bookmark' title='Permanent Link: Find the PID of the current Bash script'>Find the PID of the current Bash script</a></li>
<li><a href='http://www.wiredrevolution.com/bash-programming/find-the-parent-pid-of-a-bash-script' rel='bookmark' title='Permanent Link: Find the parent PID of a Bash Script'>Find the parent PID of a Bash Script</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<img src="http://www.wiredrevolution.com/wp-content/uploads/bash_icon.png" width="80" height="34" alt="" title="bash" /><br/><p>At some point it may be helpful to have a BASH script dynamically determine the location of itself when executed from anywhere on the system. The following code will produce the canonicalized absolute pathname of the script, as well as the directory that it resides in.</p>
<p>The script first determines if the the first argument which is a path to the script is a relative or absolute path.  If it is a relative path it will prepend the current working directory. Finally the entire path is canonicalized which resolves all symlinks and removes the extra &#8220;./&#8221;, &#8220;../&#8221; and &#8216;/&#8217; characters.</p>
<p>Here is the script:</p>
<p><code>if [[ "$0" =~ ^/ ]]<br />
then<br />
SCRIPT_LOCATION=$(readlink -f "$0")<br />
else<br />
SCRIPT_LOCATION=$(readlink -f "$(pwd)/""$0")<br />
fi<br />
SCRIPT_DIR=$(dirname $SCRIPT_LOCATION)<br />
echo $SCRIPT_LOCATION<br />
echo $SCRIPT_DIR</code></p>
<p>Now for a quick test. Lets assume our script is located here:<br />
<strong>/home/ryan/scripts/mylocation.sh</strong></p>
<p>Executing the script in the same directory:<br />
<code>$ ./mylocation</code><code>/home/ryan/scripts/mylocation.sh<br />
/home/ryan/scripts</code></p>
<p>Executing in another directory with the absolute path produces the same results:<br />
<code>$ /home/ryan//////scripts/../scripts/mylocation.sh</code><code>/home/ryan/scripts/mylocation.sh<br />
/home/ryan/scripts</code></p>


<p>Related posts<ol><li><a href='http://www.wiredrevolution.com/bash-programming/convert-a-relative-path-to-absolute-path-in-bash' rel='bookmark' title='Permanent Link: Convert a relative path to absolute path in BASH'>Convert a relative path to absolute path in BASH</a></li>
<li><a href='http://www.wiredrevolution.com/bash-programming/find-the-pid-of-the-current-bash-script' rel='bookmark' title='Permanent Link: Find the PID of the current Bash script'>Find the PID of the current Bash script</a></li>
<li><a href='http://www.wiredrevolution.com/bash-programming/find-the-parent-pid-of-a-bash-script' rel='bookmark' title='Permanent Link: Find the parent PID of a Bash Script'>Find the parent PID of a Bash Script</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.wiredrevolution.com/bash-programming/have-a-bash-script-determine-its-own-location/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Convert a relative path to absolute path in BASH</title>
		<link>http://www.wiredrevolution.com/bash-programming/convert-a-relative-path-to-absolute-path-in-bash?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=convert-a-relative-path-to-absolute-path-in-bash</link>
		<comments>http://www.wiredrevolution.com/bash-programming/convert-a-relative-path-to-absolute-path-in-bash#comments</comments>
		<pubDate>Fri, 01 Oct 2010 15:11:41 +0000</pubDate>
		<dc:creator>Ryan</dc:creator>
				<category><![CDATA[bash]]></category>
		<category><![CDATA[absolute]]></category>
		<category><![CDATA[canonicalize]]></category>
		<category><![CDATA[convert]]></category>
		<category><![CDATA[debian]]></category>
		<category><![CDATA[path]]></category>
		<category><![CDATA[pathname]]></category>
		<category><![CDATA[readlink]]></category>
		<category><![CDATA[realpath]]></category>
		<category><![CDATA[relative]]></category>
		<category><![CDATA[shell]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://www.wiredrevolution.com/?p=1294</guid>
		<description><![CDATA[<img src="http://www.wiredrevolution.com/wp-content/uploads/bash_icon.png" width="80" height="34" alt="" title="bash" /><br/>There are a few ways to take a relative path and convert it to an absolute path which can be accessed regardless of the current working directory. The easiest method is to use the readlink utility which comes bundled with about every distribution. Here my working directory is my home directory. It is printed as [...]


Related posts<ol><li><a href='http://www.wiredrevolution.com/bash-programming/have-a-bash-script-determine-its-own-location' rel='bookmark' title='Permanent Link: Have a Bash script determine it&#8217;s own location'>Have a Bash script determine it&#8217;s own location</a></li>
<li><a href='http://www.wiredrevolution.com/bash-programming/convert-text-files-within-a-directory-from-windows-to-unix-format' rel='bookmark' title='Permanent Link: Convert text files within a directory from Windows to Unix format'>Convert text files within a directory from Windows to Unix format</a></li>
<li><a href='http://www.wiredrevolution.com/commands/using-tar-to-archive-files' rel='bookmark' title='Permanent Link: Using tar to archive files'>Using tar to archive files</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<img src="http://www.wiredrevolution.com/wp-content/uploads/bash_icon.png" width="80" height="34" alt="" title="bash" /><br/><p>There are a few ways to take a relative path and convert it to an absolute path which can be accessed regardless of the current working directory. The easiest method is to use the <strong>readlink</strong> utility which comes bundled with about every distribution.</p>
<p>Here my working directory is my home directory. It is printed as an absolute path.<br />
<code>$ pwd</code><code>/home/ryan</code></p>
<p>Using readlink we can convert this same directory as a relative path to an absolute path.<br />
<code>$ readlink -f ./../ryan/Desktop/../../../home/ryan</code><code>/home/ryan</code></p>
<p>If you are using a Debian based distribution like Ubuntu you can use the <strong>realpath</strong> utility instead.<br />
<code>$ realpath ./../ryan/Desktop/../../../home/ryan</code><code>/home/ryan</code></p>


<p>Related posts<ol><li><a href='http://www.wiredrevolution.com/bash-programming/have-a-bash-script-determine-its-own-location' rel='bookmark' title='Permanent Link: Have a Bash script determine it&#8217;s own location'>Have a Bash script determine it&#8217;s own location</a></li>
<li><a href='http://www.wiredrevolution.com/bash-programming/convert-text-files-within-a-directory-from-windows-to-unix-format' rel='bookmark' title='Permanent Link: Convert text files within a directory from Windows to Unix format'>Convert text files within a directory from Windows to Unix format</a></li>
<li><a href='http://www.wiredrevolution.com/commands/using-tar-to-archive-files' rel='bookmark' title='Permanent Link: Using tar to archive files'>Using tar to archive files</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.wiredrevolution.com/bash-programming/convert-a-relative-path-to-absolute-path-in-bash/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Multiple line comments in BASH</title>
		<link>http://www.wiredrevolution.com/bash-programming/multiple-line-comments-in-bash?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=multiple-line-comments-in-bash</link>
		<comments>http://www.wiredrevolution.com/bash-programming/multiple-line-comments-in-bash#comments</comments>
		<pubDate>Wed, 29 Sep 2010 18:53:03 +0000</pubDate>
		<dc:creator>Ryan</dc:creator>
				<category><![CDATA[bash]]></category>
		<category><![CDATA[block]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[comment]]></category>
		<category><![CDATA[comments]]></category>
		<category><![CDATA[here document]]></category>
		<category><![CDATA[line]]></category>
		<category><![CDATA[multi]]></category>
		<category><![CDATA[multi-line]]></category>
		<category><![CDATA[multiple]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://www.wiredrevolution.com/?p=1291</guid>
		<description><![CDATA[<img src="http://www.wiredrevolution.com/wp-content/uploads/bash_icon.png" width="80" height="34" alt="" title="bash" /><br/>Comments are done in BASH and most other shells by placing a &#8216;#&#8217; mark at the beginning of a line. To create a multi-line comment, or to comment out an entire block of code, you can use the following HERE DOCUMENT feature. :


Related posts<ol><li><a href='http://www.wiredrevolution.com/bash-programming/find-the-pid-of-the-current-bash-script' rel='bookmark' title='Permanent Link: Find the PID of the current Bash script'>Find the PID of the current Bash script</a></li>
<li><a href='http://www.wiredrevolution.com/bash-programming/find-the-parent-pid-of-a-bash-script' rel='bookmark' title='Permanent Link: Find the parent PID of a Bash Script'>Find the parent PID of a Bash Script</a></li>
<li><a href='http://www.wiredrevolution.com/bash-programming/find-the-exit-status-of-a-previous-command-in-bash' rel='bookmark' title='Permanent Link: Find the exit status of a previous command in Bash'>Find the exit status of a previous command in Bash</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<img src="http://www.wiredrevolution.com/wp-content/uploads/bash_icon.png" width="80" height="34" alt="" title="bash" /><br/><p>Comments are done in BASH and most other shells by placing a &#8216;#&#8217; mark at the beginning of a line. To create a multi-line comment, or to comment out an entire block of code, you can use the following HERE DOCUMENT feature.</p>
<p><code>: << '--COMMENT--'<br />
comment line 1<br />
comment line 2<br />
comment line n<br />
--COMMENT--</code></p>


<p>Related posts<ol><li><a href='http://www.wiredrevolution.com/bash-programming/find-the-pid-of-the-current-bash-script' rel='bookmark' title='Permanent Link: Find the PID of the current Bash script'>Find the PID of the current Bash script</a></li>
<li><a href='http://www.wiredrevolution.com/bash-programming/find-the-parent-pid-of-a-bash-script' rel='bookmark' title='Permanent Link: Find the parent PID of a Bash Script'>Find the parent PID of a Bash Script</a></li>
<li><a href='http://www.wiredrevolution.com/bash-programming/find-the-exit-status-of-a-previous-command-in-bash' rel='bookmark' title='Permanent Link: Find the exit status of a previous command in Bash'>Find the exit status of a previous command in Bash</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.wiredrevolution.com/bash-programming/multiple-line-comments-in-bash/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Convert text files within a directory from Windows to Unix format</title>
		<link>http://www.wiredrevolution.com/bash-programming/convert-text-files-within-a-directory-from-windows-to-unix-format?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=convert-text-files-within-a-directory-from-windows-to-unix-format</link>
		<comments>http://www.wiredrevolution.com/bash-programming/convert-text-files-within-a-directory-from-windows-to-unix-format#comments</comments>
		<pubDate>Wed, 07 Jan 2009 14:14:33 +0000</pubDate>
		<dc:creator>Ryan</dc:creator>
				<category><![CDATA[bash]]></category>
		<category><![CDATA[BASH]]></category>
		<category><![CDATA[carriage return]]></category>
		<category><![CDATA[convert]]></category>
		<category><![CDATA[CRLF]]></category>
		<category><![CDATA[remove]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[text]]></category>
		<category><![CDATA[tr]]></category>
		<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://www.wiredrevolution.com/?p=901</guid>
		<description><![CDATA[<img src="http://www.wiredrevolution.com/wp-content/uploads/bash_icon.png" width="80" height="34" alt="" title="bash" /><br/>When a file is saved in Windows and then moved to a Linux system the formatting differences can cause a variety of problems. To effectively use these files you will need to change the format from Windows/DOS to Unix. This conversion occurs by simply removing the Windows carriage return characters. I have explained how to [...]


Related posts<ol><li><a href='http://www.wiredrevolution.com/system-administration/remove-windows-carriage-returns-with-tr' rel='bookmark' title='Permanent Link: Remove Windows carriage returns with tr'>Remove Windows carriage returns with tr</a></li>
<li><a href='http://www.wiredrevolution.com/commands/convert-pdf-file-to-text-with-pdftotext' rel='bookmark' title='Permanent Link: Convert PDF file to text with pdftotext'>Convert PDF file to text with pdftotext</a></li>
<li><a href='http://www.wiredrevolution.com/bash-programming/convert-a-relative-path-to-absolute-path-in-bash' rel='bookmark' title='Permanent Link: Convert a relative path to absolute path in BASH'>Convert a relative path to absolute path in BASH</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<img src="http://www.wiredrevolution.com/wp-content/uploads/bash_icon.png" width="80" height="34" alt="" title="bash" /><br/><p>When a file is saved in Windows and then moved to a Linux system the formatting differences can cause a variety of problems. To effectively use these files you will need to change the format from Windows/DOS to Unix. This conversion occurs by simply removing the Windows carriage return characters.</p>
<p>I have explained how to <a href="http://www.wiredrevolution.com/system-administration/remove-windows-carriage-returns-with-tr">use the tr command remove these windows carriage returns</a>, but when you have a large amount of files to convert this can become tedious. As a solution to this I have written a BASH script to convert all text files within a directory.</p>
<pre>
#!/bin/bash

for file in /directory/to/convert/*
do
  if [[ -f $file &#038;&#038; `file $file | grep text` ]]
  then
    tr -d '\r' < $file > "$file"_clear
    mv "$file"_clear $file
  fi
done
</pre>


<p>Related posts<ol><li><a href='http://www.wiredrevolution.com/system-administration/remove-windows-carriage-returns-with-tr' rel='bookmark' title='Permanent Link: Remove Windows carriage returns with tr'>Remove Windows carriage returns with tr</a></li>
<li><a href='http://www.wiredrevolution.com/commands/convert-pdf-file-to-text-with-pdftotext' rel='bookmark' title='Permanent Link: Convert PDF file to text with pdftotext'>Convert PDF file to text with pdftotext</a></li>
<li><a href='http://www.wiredrevolution.com/bash-programming/convert-a-relative-path-to-absolute-path-in-bash' rel='bookmark' title='Permanent Link: Convert a relative path to absolute path in BASH'>Convert a relative path to absolute path in BASH</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.wiredrevolution.com/bash-programming/convert-text-files-within-a-directory-from-windows-to-unix-format/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Command substitution in BASH</title>
		<link>http://www.wiredrevolution.com/bash-programming/command-substitution-in-bash?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=command-substitution-in-bash</link>
		<comments>http://www.wiredrevolution.com/bash-programming/command-substitution-in-bash#comments</comments>
		<pubDate>Fri, 07 Nov 2008 13:42:59 +0000</pubDate>
		<dc:creator>Ryan</dc:creator>
				<category><![CDATA[bash]]></category>
		<category><![CDATA[backquote]]></category>
		<category><![CDATA[backtick]]></category>
		<category><![CDATA[BASH]]></category>
		<category><![CDATA[command]]></category>
		<category><![CDATA[environment variable]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[substitution]]></category>

		<guid isPermaLink="false">http://www.wiredrevolution.com/?p=628</guid>
		<description><![CDATA[<img src="http://www.wiredrevolution.com/wp-content/uploads/bash_icon.png" width="80" height="34" alt="" title="bash" /><br/>BASH has the ability to execute a command string and replace that string with the output of the command. Or to say it another way, the output of a command will replace the command itself. There are 2 ways to accomplish this. The first is to surround the command with backticks or blockquotes. `command` You [...]


Related posts<ol><li><a href='http://www.wiredrevolution.com/bash-programming/customize-the-bash-ps1-command-prompt' rel='bookmark' title='Permanent Link: Customize the BASH PS1 command prompt'>Customize the BASH PS1 command prompt</a></li>
<li><a href='http://www.wiredrevolution.com/bash-programming/single-versus-double-quotes-in-bash' rel='bookmark' title='Permanent Link: Single versus double quotes in BASH'>Single versus double quotes in BASH</a></li>
<li><a href='http://www.wiredrevolution.com/commands/search-for-files-with-the-find-command' rel='bookmark' title='Permanent Link: Search for files with the find command'>Search for files with the find command</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<img src="http://www.wiredrevolution.com/wp-content/uploads/bash_icon.png" width="80" height="34" alt="" title="bash" /><br/><p>BASH has the ability to execute a command string and replace that string with the output of the command. Or to say it another way, the output of a command will replace the command itself.</p>
<p>There are 2 ways to accomplish this.</p>
<p>The first is to surround the command with backticks or blockquotes.<br />
<strong>`command`</strong></p>
<p>You can also substitute with dollar symbol and parenthesis.<br />
<strong>$(command)</strong></p>
<p>The following will do a direct substitution of the current username on the command line.</p>
<pre>
$ echo /home/$(whoami)
</pre>
<pre>
/home/ryan
</pre>
<p>Another great use is the ability to assign a command to an environment variable.</p>
<pre>
$ MYDATE=`date`
</pre>
<p>or using the other format</p>
<pre>
$ MYDATE=$(date)
</pre>


<p>Related posts<ol><li><a href='http://www.wiredrevolution.com/bash-programming/customize-the-bash-ps1-command-prompt' rel='bookmark' title='Permanent Link: Customize the BASH PS1 command prompt'>Customize the BASH PS1 command prompt</a></li>
<li><a href='http://www.wiredrevolution.com/bash-programming/single-versus-double-quotes-in-bash' rel='bookmark' title='Permanent Link: Single versus double quotes in BASH'>Single versus double quotes in BASH</a></li>
<li><a href='http://www.wiredrevolution.com/commands/search-for-files-with-the-find-command' rel='bookmark' title='Permanent Link: Search for files with the find command'>Search for files with the find command</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.wiredrevolution.com/bash-programming/command-substitution-in-bash/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Delete a specific line from a text file with sed</title>
		<link>http://www.wiredrevolution.com/bash-programming/delete-a-specific-line-from-a-text-file-with-sed?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=delete-a-specific-line-from-a-text-file-with-sed</link>
		<comments>http://www.wiredrevolution.com/bash-programming/delete-a-specific-line-from-a-text-file-with-sed#comments</comments>
		<pubDate>Sun, 26 Oct 2008 09:05:21 +0000</pubDate>
		<dc:creator>Ryan</dc:creator>
				<category><![CDATA[bash]]></category>
		<category><![CDATA[BASH]]></category>
		<category><![CDATA[delete]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[regular expression]]></category>
		<category><![CDATA[sed]]></category>

		<guid isPermaLink="false">http://www.wiredrevolution.com/?p=513</guid>
		<description><![CDATA[<img src="http://www.wiredrevolution.com/wp-content/uploads/bash_icon.png" width="80" height="34" alt="" title="bash" /><br/>At some point you may have the need to remove all lines within a text file that match a certain pattern. Accomplishing this is easy with the sed command. Here is the command format. $ sed -i '/PATTERN/ d' file.txt The &#8216;-i&#8216; option allows you to edit the specified file in place. PATTERN is a [...]


Related posts<ol><li><a href='http://www.wiredrevolution.com/bash-programming/single-versus-double-quotes-in-bash' rel='bookmark' title='Permanent Link: Single versus double quotes in BASH'>Single versus double quotes in BASH</a></li>
<li><a href='http://www.wiredrevolution.com/bash-programming/convert-text-files-within-a-directory-from-windows-to-unix-format' rel='bookmark' title='Permanent Link: Convert text files within a directory from Windows to Unix format'>Convert text files within a directory from Windows to Unix format</a></li>
<li><a href='http://www.wiredrevolution.com/bash-programming/command-substitution-in-bash' rel='bookmark' title='Permanent Link: Command substitution in BASH'>Command substitution in BASH</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<img src="http://www.wiredrevolution.com/wp-content/uploads/bash_icon.png" width="80" height="34" alt="" title="bash" /><br/><p>At some point you may have the need to remove all lines within a text file that match a certain pattern. Accomplishing this is easy with the <strong>sed</strong> command.</p>
<p>Here is the command format. </p>
<pre>
$ sed -i '/PATTERN/ d' file.txt
</pre>
<ul>
<li>The &#8216;<strong>-i</strong>&#8216; option allows you to edit the specified file in place. </li>
<li><strong>PATTERN</strong> is a regular expression</li>
<li><strong>d</strong> is a command which instructs sed to delete the line if it matches the PATTERN</li>
</ul>
<p>Here is an example. </p>
<pre>
$ cat file.txt
</pre>
<pre>
one
two
three
one hundred
</pre>
<p>We want to remove the lines containing the word &#8220;one&#8221;</p>
<pre>
$ sed -i '/one/ d' file.txt
</pre>
<pre>
$ cat file.txt
</pre>
<pre>
two
three
</pre>
<p>If your pattern contains an environment variable, you can surround it with double quotes instead to force the shell to expand it.</p>
<pre>
$ sed -i "/$MYVAR/ d" file.txt
</pre>


<p>Related posts<ol><li><a href='http://www.wiredrevolution.com/bash-programming/single-versus-double-quotes-in-bash' rel='bookmark' title='Permanent Link: Single versus double quotes in BASH'>Single versus double quotes in BASH</a></li>
<li><a href='http://www.wiredrevolution.com/bash-programming/convert-text-files-within-a-directory-from-windows-to-unix-format' rel='bookmark' title='Permanent Link: Convert text files within a directory from Windows to Unix format'>Convert text files within a directory from Windows to Unix format</a></li>
<li><a href='http://www.wiredrevolution.com/bash-programming/command-substitution-in-bash' rel='bookmark' title='Permanent Link: Command substitution in BASH'>Command substitution in BASH</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.wiredrevolution.com/bash-programming/delete-a-specific-line-from-a-text-file-with-sed/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Customize the BASH PS1 command prompt</title>
		<link>http://www.wiredrevolution.com/bash-programming/customize-the-bash-ps1-command-prompt?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=customize-the-bash-ps1-command-prompt</link>
		<comments>http://www.wiredrevolution.com/bash-programming/customize-the-bash-ps1-command-prompt#comments</comments>
		<pubDate>Sun, 19 Oct 2008 14:45:22 +0000</pubDate>
		<dc:creator>Ryan</dc:creator>
				<category><![CDATA[bash]]></category>
		<category><![CDATA[BASH]]></category>
		<category><![CDATA[bashrc]]></category>
		<category><![CDATA[codes]]></category>
		<category><![CDATA[color]]></category>
		<category><![CDATA[command prompt]]></category>
		<category><![CDATA[environment variable]]></category>
		<category><![CDATA[format]]></category>
		<category><![CDATA[PS1]]></category>

		<guid isPermaLink="false">http://www.wiredrevolution.com/?p=403</guid>
		<description><![CDATA[<img src="http://www.wiredrevolution.com/wp-content/uploads/bash_icon.png" width="80" height="34" alt="" title="bash" /><br/>The PS1 environment controls the appearance of the BASH command line prompt. There are a variety of default prompts but they usually include username, hostname, and working directory. You can easily customize your prompt to display information important to you as well as add color and style formatting. Here we can see the default prompt [...]


Related posts<ol><li><a href='http://www.wiredrevolution.com/bash-programming/command-substitution-in-bash' rel='bookmark' title='Permanent Link: Command substitution in BASH'>Command substitution in BASH</a></li>
<li><a href='http://www.wiredrevolution.com/bash-programming/have-a-bash-script-determine-its-own-location' rel='bookmark' title='Permanent Link: Have a Bash script determine it&#8217;s own location'>Have a Bash script determine it&#8217;s own location</a></li>
<li><a href='http://www.wiredrevolution.com/bash-programming/find-the-pid-of-a-background-child-process-in-bash' rel='bookmark' title='Permanent Link: Find the PID of a background child process in Bash'>Find the PID of a background child process in Bash</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<img src="http://www.wiredrevolution.com/wp-content/uploads/bash_icon.png" width="80" height="34" alt="" title="bash" /><br/><p>The <strong>PS1</strong> environment controls the appearance of the BASH command line prompt. There are a variety of default prompts but they usually include username, hostname, and working directory. You can easily customize your prompt to display information important to you as well as add color and style formatting.</p>
<p>Here we can see the default prompt is &#8220;username@hostname working_directory $&#8221;. </p>
<pre>
ryan@workstation ~ $
</pre>
<p>You can echo the PS1 environment variable and see the character codes which are used to create the output.</p>
<pre>
ryan@workstation ~ $ echo $PS1
</pre>
<pre>
\u@\h \w \$
</pre>
<p>Here is a list of all the special character codes you can use to build your PS1 command prompt.</p>
<h3>Special Character Codes</h3>
<p><strong>\a</strong> &#8211; an ASCII bell character (07)<br />
<strong>\d</strong> &#8211; the date in &#8220;Weekday Month Date&#8221; format (e.g., &#8220;Tue May 26&#8243;)<br />
<strong>\D{format}</strong> &#8211; the format is passed to strftime(3) and the result is inserted into the prompt string; an empty format results in a locale-specific time representation. The braces are required<br />
<strong>\e</strong> &#8211; an ASCII escape character (033)<br />
<strong>\h</strong> &#8211; the hostname up to the first `.&#8217;<br />
<strong>\H</strong> &#8211; the hostname<br />
<strong>\j</strong> &#8211; the number of jobs currently managed by the shell<br />
<strong>\l</strong> &#8211; the basename of the shell&#8217;s terminal device name<br />
<strong>\n</strong> &#8211; newline<br />
<strong>\r</strong> &#8211; carriage return<br />
<strong>\s</strong> &#8211; the name of the shell, the basename of $0 (the portion following the final slash)<br />
<strong>\t</strong> &#8211; the current time in 24-hour HH:MM:SS format<br />
<strong>\T</strong> &#8211; the current time in 12-hour HH:MM:SS format<br />
<strong>\@</strong> &#8211; the current time in 12-hour am/pm format<br />
<strong>\A</strong> &#8211; the current time in 24-hour HH:MM format<br />
<strong>\u</strong> &#8211; the username of the current user<br />
<strong>\v</strong> &#8211; the version of bash (e.g., 2.00)<br />
<strong>\V</strong> &#8211; the release of bash, version + patchelvel (e.g., 2.00.0)<br />
<strong>\w</strong> &#8211; the current working directory<br />
<strong>\W</strong> &#8211; the basename of the current working directory<br />
<strong>\!</strong> &#8211; the history number of this command<br />
<strong>\#</strong> &#8211; the command number of this command<br />
<strong>\$</strong> &#8211; if the effective UID is 0, a #, otherwise a $<br />
<strong>\nnn</strong> &#8211; the character corresponding to the octal number nnn<br />
<strong>\\</strong> &#8211; a backslash<br />
<strong>\[</strong> - begin a sequence of non-printing characters, which could be used to embed a terminal control sequence into the prompt<br />
<strong>\]</strong> &#8211; end a sequence of non-printing characters </p>
<p>Lets try a simple change. Make sure you use <a href="http://www.wiredrevolution.com/programming/bash-programming/single-versus-double-quotes-in-bash">double quotes</a> so the escape characters get passed on to the variable and not expanded in the shell.</p>
<pre>
ryan@workstation ~ $ PS1="My Prompt: $ "
</pre>
<pre>
My Prompt: $
</pre>
<p>As you can see the prompt will immediately change to reflect the changes.</p>
<p>We can add the date to the prompt.</p>
<pre>
ryan@workstation ~ $ PS1="\d \u@\h \w \$ "
</pre>
<pre>
Sun Oct 19 ryan@workstation ~ $
</pre>
<p>Now lets add some color to our original prompt.</p>
<pre>
ryan@workstation ~ $ PS1="\[\e[1;32m\]\u@\h\[\e[1;34m\] \w \$\[\e[0m\] "
</pre>
<p>Let go over this in detail.</p>
<p>A style block will display all text that follows with the style it defines. The block contains 3 elements.</p>
<p>Lets look at the first block.<br />
&#8220;\[\e[1;32m\]&#8221;</p>
<p>&#8220;<strong>\[</strong>" - begin a sequence of non-printing characters<br />
"<strong>\e[1;32m</strong>" - the semicolon separated list of style/color codes (in this case <strong>bold;green</strong>)<br />
"<strong>\]</strong>&#8221; &#8211; end a sequence of non-printing characters</p>
<p>The first block will display the following <em>username@hostname</em> text in BOLD green.<br />
&#8220;<strong>\[\e[1;32m\]</strong>\u@\h\[\e[1;34m\] \w \$\[\e[0m\] &#8221;</p>
<p>The second block will display the following <em>working_directory</em> text in BOLD blue.<br />
&#8220;\[\e[1;32m\]\u@\h<strong>\[\e[1;34m\]</strong> \w \$\[\e[0m\] &#8221;</p>
<p>The final block resets the colors.<br />
&#8220;\[\e[1;32m\]\u@\h\[\e[1;34m\] \w \$<strong>\[\e[0m\]</strong> &#8221;</p>
<p>Here is a rundown of all the color and style codes.</p>
<h3>Color and Style Codes</h3>
<p><strong>Style</strong><br />
0 &#8211; default<br />
1 &#8211; bold<br />
4 &#8211; underline<br />
7 &#8211; inverse<br />
9 &#8211; strikeout</p>
<p><strong>Foreground Colors</strong><br />
30 &#8211; foreground Black<br />
31 &#8211; foreground Red<br />
32 &#8211; foreground Green<br />
33 &#8211; foreground Yellow<br />
34 &#8211; foreground Blue<br />
35 &#8211; foreground Magenta<br />
36 &#8211; foreground Cyan<br />
37 &#8211; foreground White</p>
<p><strong>Background Colors</strong><br />
40 &#8211; background Black<br />
41 &#8211; background Red<br />
42 &#8211; background Green<br />
43 &#8211; background Yellow<br />
44 &#8211; background Blue<br />
45 &#8211; background Magenta<br />
46 &#8211; background Cyan<br />
47 &#8211; background White</p>
<p>You can combine multiple codes to create the exact style you want.<br />
&#8220;\[\e[1;4;36;47m\]&#8221;<br />
This style block defines bold and underlined cyan text with a white background. </p>
<p>To make these changes permanent place your new prompt string like the one below in your <strong>~/.bashrc</strong> file in your home directory. </p>
<pre>
PS1="\[\e[1;32m\]\u@\h\[\e[1;34m\] \w \$ \[\e[0m\]"
</pre>
<p>This file is sourced every time you start a new interactive shell.</p>


<p>Related posts<ol><li><a href='http://www.wiredrevolution.com/bash-programming/command-substitution-in-bash' rel='bookmark' title='Permanent Link: Command substitution in BASH'>Command substitution in BASH</a></li>
<li><a href='http://www.wiredrevolution.com/bash-programming/have-a-bash-script-determine-its-own-location' rel='bookmark' title='Permanent Link: Have a Bash script determine it&#8217;s own location'>Have a Bash script determine it&#8217;s own location</a></li>
<li><a href='http://www.wiredrevolution.com/bash-programming/find-the-pid-of-a-background-child-process-in-bash' rel='bookmark' title='Permanent Link: Find the PID of a background child process in Bash'>Find the PID of a background child process in Bash</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.wiredrevolution.com/bash-programming/customize-the-bash-ps1-command-prompt/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>I/O redirection in BASH</title>
		<link>http://www.wiredrevolution.com/bash-programming/io-redirection-in-bash?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=io-redirection-in-bash</link>
		<comments>http://www.wiredrevolution.com/bash-programming/io-redirection-in-bash#comments</comments>
		<pubDate>Thu, 16 Oct 2008 12:50:37 +0000</pubDate>
		<dc:creator>Ryan</dc:creator>
				<category><![CDATA[bash]]></category>
		<category><![CDATA[/dev/null]]></category>
		<category><![CDATA[BASH]]></category>
		<category><![CDATA[file descriptors]]></category>
		<category><![CDATA[input]]></category>
		<category><![CDATA[output]]></category>
		<category><![CDATA[pipe]]></category>
		<category><![CDATA[redirection]]></category>
		<category><![CDATA[stderr]]></category>
		<category><![CDATA[stdin]]></category>
		<category><![CDATA[stdout]]></category>

		<guid isPermaLink="false">http://www.wiredrevolution.com/?p=350</guid>
		<description><![CDATA[<img src="http://www.wiredrevolution.com/wp-content/uploads/bash_icon.png" width="80" height="34" alt="" title="bash" /><br/>One of the best features of the Linux command line is the ability to efficiently direct input and output to and from files and other programs. Every program begins with 3 open file streams. stdin (file descriptor 0) &#8211; input from the user, usually keyboard stdout (file descriptor 1) &#8211; standard output that is displayed [...]


Related posts<ol><li><a href='http://www.wiredrevolution.com/c/find-controlling-terminal-with-ttyname' rel='bookmark' title='Permanent Link: Find controlling terminal with ttyname'>Find controlling terminal with ttyname</a></li>
<li><a href='http://www.wiredrevolution.com/system-administration/remove-windows-carriage-returns-with-tr' rel='bookmark' title='Permanent Link: Remove Windows carriage returns with tr'>Remove Windows carriage returns with tr</a></li>
<li><a href='http://www.wiredrevolution.com/bash-programming/command-substitution-in-bash' rel='bookmark' title='Permanent Link: Command substitution in BASH'>Command substitution in BASH</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<img src="http://www.wiredrevolution.com/wp-content/uploads/bash_icon.png" width="80" height="34" alt="" title="bash" /><br/><p>One of the best features of the Linux command line is the ability to efficiently direct input and output to and from files and other programs.</p>
<p>Every program begins with 3 open file streams.</p>
<ul>
<li><strong>stdin</strong> (file descriptor 0) &#8211; input from the user, usually keyboard</li>
<li><strong>stdout</strong> (file descriptor 1) &#8211; standard output that is displayed on the screen</li>
<li><strong>stderr</strong> (file descriptor 2) &#8211; standard error output, also displayed on the screen</li>
</ul>
<p>We can redirect the stdout from a command to a file.  The file is created if it doesn&#8217;t exist and is truncated if it does.</p>
<pre>
$ <em>command</em> > outfile
</pre>
<p>Any error messages produced by the command will still be sent to the command line. To redirect both stdout and stderr you can do this.</p>
<pre>
$ <em>command</em> &#038;> outfile
</pre>
<p>Lets say you want to suppress the error messages from a command and view only the successful output on the command line.</p>
<pre>
$ <em>command</em> 2> /dev/null
</pre>
<p>The error messages in this case will be sent to &#8216;<strong>/dev/null</strong>&#8216; which is a special device that discards all data sent to it.</p>
<p>If you want to append stdout to a file. The file is still created if it doesn&#8217;t exists.</p>
<pre>
$ <em>command</em> >> outfile
</pre>
<p>To append stderr to a file.</p>
<pre>
$ <em>command</em> 2>> outfile
</pre>
<p>Its a little more complex to append both stdout and stderr to a file.</p>
<pre>
$ <em>command</em> >> outfile 2>&#038;1
</pre>
<p>Here stdout is appended to file and stderr is redirected to stdout.</p>
<p>If you want to redirect stdin from a file to a command.</p>
<pre>
$ <em>command</em> < inputfile
</pre>
<p>We can combine redirection of stdin from the inputfile and stdout to the outfile.</p>
<pre>
$ <em>command</em> < inputfile > outfile
</pre>
<p>A more complex example where all 3 streams are redirected.</p>
<pre>
$ <em>command</em> < inputfile > outfile 2>&#038;1
</pre>
<p>So far I have only showed how to direct file streams from program to a file and vice versa. You can also redirect the output from one command to the input of another. This is one of the most powerful features of Linux, and learning to do it well will make you a better Linux user.</p>
<p>Lets direct the stdout from one command to the stdin of another. Chaining commands togather is done with the pipe character '<strong>|</strong>'. </p>
<pre>
$ <em>command1</em> | <em>command2</em>
</pre>
<p>Here we "pipe" the output from command1 to command 2.</p>
<p>As you can imagine we can get really creative and combine all of this into a single command line statement.</p>
<pre>
$ <em>command1</em> 2>~/errorfile | <em>command2</em> > ~/resultfile 2>> ~/errorfile
</pre>
<p>Lets go over this.  </p>
<ol>
<li>command1 will truncate and write stderr to 'errorfile' in your home directory</li>
<li>stdout is piped to the stdin of command2</li>
<li>the final results from stdout of command2 are saved in 'resultfile'</li>
<li>command2 stderr will be appended to 'errorfile'</li>
</ol>


<p>Related posts<ol><li><a href='http://www.wiredrevolution.com/c/find-controlling-terminal-with-ttyname' rel='bookmark' title='Permanent Link: Find controlling terminal with ttyname'>Find controlling terminal with ttyname</a></li>
<li><a href='http://www.wiredrevolution.com/system-administration/remove-windows-carriage-returns-with-tr' rel='bookmark' title='Permanent Link: Remove Windows carriage returns with tr'>Remove Windows carriage returns with tr</a></li>
<li><a href='http://www.wiredrevolution.com/bash-programming/command-substitution-in-bash' rel='bookmark' title='Permanent Link: Command substitution in BASH'>Command substitution in BASH</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.wiredrevolution.com/bash-programming/io-redirection-in-bash/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Single versus double quotes in BASH</title>
		<link>http://www.wiredrevolution.com/bash-programming/single-versus-double-quotes-in-bash?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=single-versus-double-quotes-in-bash</link>
		<comments>http://www.wiredrevolution.com/bash-programming/single-versus-double-quotes-in-bash#comments</comments>
		<pubDate>Thu, 16 Oct 2008 03:50:30 +0000</pubDate>
		<dc:creator>Ryan</dc:creator>
				<category><![CDATA[bash]]></category>
		<category><![CDATA[BASH]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[quotes]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://www.wiredrevolution.com/?p=344</guid>
		<description><![CDATA[<img src="http://www.wiredrevolution.com/wp-content/uploads/bash_icon.png" width="80" height="34" alt="" title="bash" /><br/>Understanding the difference between double versus single quotes is important when using BASH. Many times you may have seen them being used interchangeably. The basic difference is that variable names will be expanded within double quotes but not within single ones. This example shows the normal output with no quotes. $ echo $USER ryan As [...]


Related posts<ol><li><a href='http://www.wiredrevolution.com/bash-programming/delete-a-specific-line-from-a-text-file-with-sed' rel='bookmark' title='Permanent Link: Delete a specific line from a text file with sed'>Delete a specific line from a text file with sed</a></li>
<li><a href='http://www.wiredrevolution.com/bash-programming/find-the-pid-of-a-background-child-process-in-bash' rel='bookmark' title='Permanent Link: Find the PID of a background child process in Bash'>Find the PID of a background child process in Bash</a></li>
<li><a href='http://www.wiredrevolution.com/bash-programming/customize-the-bash-ps1-command-prompt' rel='bookmark' title='Permanent Link: Customize the BASH PS1 command prompt'>Customize the BASH PS1 command prompt</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<img src="http://www.wiredrevolution.com/wp-content/uploads/bash_icon.png" width="80" height="34" alt="" title="bash" /><br/><p>Understanding the difference between double versus single quotes is important when using BASH.  Many times you may have seen them being used interchangeably. The basic difference is that variable names will be expanded within double quotes but not within single ones.</p>
<p>This example shows the normal output with no quotes.</p>
<pre>
$ echo $USER
</pre>
<pre>
ryan
</pre>
<p>As you can see the shell will expand the variable.</p>
<p>In this example we will surround the variable with double quotes.</p>
<pre>
$ echo "$USER"
</pre>
<pre>
ryan
</pre>
<p>We see the same result as before.</p>
<p>This time with single quotes.</p>
<pre>
$ echo '$USER'
</pre>
<pre>
$USER
</pre>
<p>The variable name is not expanded.</p>
<p>Here is another interesting case. The single quotes are inside the double quotes.</p>
<pre>
$ echo "'$USER'"
</pre>
<pre>
'ryan'
</pre>
<p>The variable is expanded and the single quotes are retained.</p>
<p>Here I flip the order with the single quotes on the outside.</p>
<pre>
$ echo '"$USER"'
</pre>
<pre>
"$USER"
</pre>
<p>The variable is not expanded but the double quotes are retained.</p>


<p>Related posts<ol><li><a href='http://www.wiredrevolution.com/bash-programming/delete-a-specific-line-from-a-text-file-with-sed' rel='bookmark' title='Permanent Link: Delete a specific line from a text file with sed'>Delete a specific line from a text file with sed</a></li>
<li><a href='http://www.wiredrevolution.com/bash-programming/find-the-pid-of-a-background-child-process-in-bash' rel='bookmark' title='Permanent Link: Find the PID of a background child process in Bash'>Find the PID of a background child process in Bash</a></li>
<li><a href='http://www.wiredrevolution.com/bash-programming/customize-the-bash-ps1-command-prompt' rel='bookmark' title='Permanent Link: Customize the BASH PS1 command prompt'>Customize the BASH PS1 command prompt</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.wiredrevolution.com/bash-programming/single-versus-double-quotes-in-bash/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

