<?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/tag/bash/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>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 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>How to correctly use LD_LIBRARY_PATH</title>
		<link>http://www.wiredrevolution.com/system-administration/how-to-correctly-use-ld_library_path?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=how-to-correctly-use-ld_library_path</link>
		<comments>http://www.wiredrevolution.com/system-administration/how-to-correctly-use-ld_library_path#comments</comments>
		<pubDate>Wed, 22 Oct 2008 12:32:13 +0000</pubDate>
		<dc:creator>Ryan</dc:creator>
				<category><![CDATA[system administration]]></category>
		<category><![CDATA[BASH]]></category>
		<category><![CDATA[bashrc]]></category>
		<category><![CDATA[dynamic library]]></category>
		<category><![CDATA[ELF]]></category>
		<category><![CDATA[environment variable]]></category>
		<category><![CDATA[ldd]]></category>
		<category><![CDATA[LD_LIBRARY_PATH]]></category>

		<guid isPermaLink="false">http://www.wiredrevolution.com/?p=453</guid>
		<description><![CDATA[<img src="http://www.wiredrevolution.com/wp-content/uploads/sysadmin_icon.png" width="80" height="94" alt="" title="system administration" /><br/>The LD_LIBRARY_PATH environment variable contains a colon separated list of paths that the linker uses to resolve library dependencies of ELF executables at run-time. These paths will be given priority over the standard library paths /lib and /usr/lib. The standard paths will still be searched, but only after the list of paths in LD_LIBRARY_PATH has [...]


Related posts<ol><li><a href='http://www.wiredrevolution.com/commands/view-dynamic-library-dependencies-with-ldd' rel='bookmark' title='Permanent Link: View dynamic library dependencies with ldd'>View dynamic library dependencies with ldd</a></li>
<li><a href='http://www.wiredrevolution.com/system-administration/select-threading-implementation-using-ld_assume_kernel' rel='bookmark' title='Permanent Link: Select threading implementation using LD_ASSUME_KERNEL'>Select threading implementation using LD_ASSUME_KERNEL</a></li>
<li><a href='http://www.wiredrevolution.com/guides/how-to-get-boxee-to-correctly-identify-local-media-files' rel='bookmark' title='Permanent Link: How to get Boxee to correctly identify local media files'>How to get Boxee to correctly identify local media files</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<img src="http://www.wiredrevolution.com/wp-content/uploads/sysadmin_icon.png" width="80" height="94" alt="" title="system administration" /><br/><p>The LD_LIBRARY_PATH environment variable contains a colon separated list of paths that the linker uses to resolve library dependencies of ELF executables at run-time. These paths will be given priority over the standard library paths <strong>/lib</strong> and <strong>/usr/lib</strong>.  The standard paths will still be searched, but only after the list of paths in LD_LIBRARY_PATH has been exhausted.</p>
<p>The best way to use LD_LIBRARY_PATH is to set it on the command line or script immediately before executing the program. This way you can keep the new LD_LIBRARY_PATH isolated from the rest of your system.</p>
<pre>
$ export LD_LIBRARY_PATH="/list/of/library/paths:/another/path"
$ ./program
</pre>
<p>In general it is not a good practice to have LD_LIBRARY_PATH permanently set in your environment. This could lead to unintended side effects as programs can link to unintended libraries producing strange results or unexpectedly crashing. There is also the possibility introducing potential security threats. </p>
<p>All those warnings aside, if you are using BASH you can set it permanently by placing a line similar to this in your <strong>.bashrc</strong> in your home directory.</p>
<pre>
export LIBRARY_PATH="/list/of/library/paths:/another/path"
</pre>
<p>A common case for setting LD_LIBRARY_PATH is when you have an application that requires dynamic libraries which were not installed in the standard library locations.</p>
<p>You can check if the linker can locate all the required libraries by <a href="http://www.wiredrevolution.com/commands/view-dynamic-library-dependencies-with-ldd">running the ldd command</a>.</p>
<pre>
$ ldd ~/myprogram
</pre>
<pre>
	librt.so.1 => /lib/librt.so.1 (0x00002b4eca08e000)
	libc.so.6 => /lib/libc.so.6 (0x00002b4eca49f000)
	libpthread.so.0 => /lib/libpthread.so.0 (0x00002b4eca7df000)
	/lib64/ld-linux-x86-64.so.2 (0x00002b4ec9e72000)
	libmylib.so.1 => not found
</pre>
<p>The linker cannot find libmylib.so.1. </p>
<p>Let&#8217;s assume this library exists here &#8220;~/myprogdir/lib/libmylib.so.1&#8243;. We have to set LD_LIBRARY_PATH to include this path for the application to successfully run.</p>
<pre>
$ export LD_LIBRARY_PATH="~/myprogdir/lib/:$LD_LIBRARY_PATH"
$ ldd ~/myprogram
</pre>
<pre>
	librt.so.1 => /lib/librt.so.1 (0x00002b4eca08e000)
	libc.so.6 => /lib/libc.so.6 (0x00002b4eca49f000)
	libpthread.so.0 => /lib/libpthread.so.0 (0x00002b4eca7df000)
	/lib64/ld-linux-x86-64.so.2 (0x00002b4ec9e72000)
	libmylib.so.1 => ~/myprogdir/lib/libmylib.so.1 (0x00002b4eca9fa000)
</pre>
<p>The linker has now found all the required libraries.</p>


<p>Related posts<ol><li><a href='http://www.wiredrevolution.com/commands/view-dynamic-library-dependencies-with-ldd' rel='bookmark' title='Permanent Link: View dynamic library dependencies with ldd'>View dynamic library dependencies with ldd</a></li>
<li><a href='http://www.wiredrevolution.com/system-administration/select-threading-implementation-using-ld_assume_kernel' rel='bookmark' title='Permanent Link: Select threading implementation using LD_ASSUME_KERNEL'>Select threading implementation using LD_ASSUME_KERNEL</a></li>
<li><a href='http://www.wiredrevolution.com/guides/how-to-get-boxee-to-correctly-identify-local-media-files' rel='bookmark' title='Permanent Link: How to get Boxee to correctly identify local media files'>How to get Boxee to correctly identify local media files</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.wiredrevolution.com/system-administration/how-to-correctly-use-ld_library_path/feed</wfw:commentRss>
		<slash:comments>5</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>

