<?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>Ashish Agarwal &#187; Bash</title>
	<atom:link href="http://ashishagarwal.org/tag/bash/feed/" rel="self" type="application/rss+xml" />
	<link>http://ashishagarwal.org</link>
	<description></description>
	<lastBuildDate>Mon, 11 Mar 2019 21:28:46 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.1.42</generator>
	<item>
		<title>Platform independent .bashrc file</title>
		<link>http://ashishagarwal.org/2010/11/10/platform-independent-bashrc-file/</link>
		<comments>http://ashishagarwal.org/2010/11/10/platform-independent-bashrc-file/#comments</comments>
		<pubDate>Wed, 10 Nov 2010 18:55:12 +0000</pubDate>
		<dc:creator><![CDATA[ashish]]></dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Bash]]></category>

		<guid isPermaLink="false">http://ashishagarwal.org/?p=114</guid>
		<description><![CDATA[The number of computers I have accounts on has recently exploded, and manually editing my .bashrc file on each of these computers started getting tedious. I decided to resolve this by writing a single .bashrc file that can be copied &#8230; <a href="http://ashishagarwal.org/2010/11/10/platform-independent-bashrc-file/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>The number of computers I have accounts on has recently exploded, and manually editing my .bashrc file on each of these computers started getting tedious. I decided to resolve this by writing a single .bashrc file that can be copied over to all my accounts. This allows me to maintain a single master file, but requires me to set configurations conditional on the account the file is on. Here, I describe some of the things I did.</p>
<p>Firstly, I finally spent a few hours learning bash by readingÂ <a href="http://www.amazon.com/gp/product/0596009658?ie=UTF8&amp;tag=ashishagarwal-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0596009658">Learning the bash Shell</a><img style="border: none !important; margin: 0px !important;" src="http://www.assoc-amazon.com/e/ir?t=ashishagarwal-20&amp;l=as2&amp;o=1&amp;a=0596009658" border="0" alt="" width="1" height="1" />. I recommend you do this too. You&#8217;ll easily make up the hours in increased productivity.</p>
<p>My conditional settings are dependent on the kind of operating system I&#8217;m running and the particular machine I&#8217;m on. Let&#8217;s put this information in two variables:</p>
<div class="highlight" style="background: #f8f8f8;">
<pre style="line-height: 125%;"><span style="color: #19177c;">os</span><span style="color: #666666;">=</span><span style="color: #ba2121;">`</span>uname -s<span style="color: #ba2121;">`</span>
<span style="color: #19177c;">host</span><span style="color: #666666;">=</span><span style="color: #ba2121;">`</span>hostname | cut -d. -f1<span style="color: #ba2121;">`</span></pre>
</div>
<p>Passing the result of <em>hostname</em> to <em>cut</em> causes <em>host</em> to be set to just the first part of your hostname. For example, <em>foo</em> instead of <em>foo.bar.com</em>. This allows me to type less in later code.</p>
<p>Now, as an example, on one of my computer accounts I was using a software called Netkit. This required setting some environment variables and sourcing a file that provided bash completion features. Assuming the hostname was <em>net.cs.yale.edu</em>, I did this with:</p>
<div class="highlight" style="background: #f8f8f8;">
<pre style="line-height: 125%;"><span style="color: #008000; font-weight: bold;">if</span> <span style="color: #666666;">[</span> <span style="color: #19177c;">$host</span> <span style="color: #666666;">=</span> <span style="color: #ba2121;">"net"</span> <span style="color: #666666;">]</span>; <span style="color: #008000; font-weight: bold;">then</span>
<span style="color: #008000; font-weight: bold;">    </span><span style="color: #008000;">export </span><span style="color: #19177c;">NETKIT_HOME</span><span style="color: #666666;">=</span>/usr/local/netkit
    <span style="color: #19177c;">MANPATH</span><span style="color: #666666;">=</span><span style="color: #19177c;">$NETKIT_HOME</span>/man:<span style="color: #19177c;">$MANPATH</span>
    <span style="color: #19177c;">PATH</span><span style="color: #666666;">=</span><span style="color: #19177c;">$NETKIT_HOME</span>/bin:<span style="color: #19177c;">$PATH</span>
    . <span style="color: #19177c;">$NETKIT_HOME</span>/bin/netkit_bash_completion
<span style="color: #008000; font-weight: bold;">fi</span></pre>
</div>
<p>With this code, my environment variable namespace is not polluted on all my other accounts, and I won&#8217;t get any errors at login aboutÂ netkit_bash_completion not being found.</p>
<p>Some settings depend on the OS. For example, I like to colorize the output from ls, but the option for this differs on Mac and Linux systems. I can use a bash case construct to set the alias appropriately:</p>
<div class="highlight" style="background: #f8f8f8;">
<pre style="line-height: 125%;"><span style="color: #008000; font-weight: bold;">case</span> <span style="color: #19177c;">$os</span> in
    <span style="color: #ba2121;">"Darwin"</span> <span style="color: #666666;">)</span>
        <span style="color: #008000;">alias </span><span style="color: #19177c;">ls</span><span style="color: #666666;">=</span><span style="color: #ba2121;">'ls -G'</span>;;
    <span style="color: #ba2121;">"Linux"</span>  <span style="color: #666666;">)</span>
        <span style="color: #008000;">alias </span><span style="color: #19177c;">ls</span><span style="color: #666666;">=</span><span style="color: #ba2121;">'ls --color=auto'</span>;;
<span style="color: #008000; font-weight: bold;">esac</span></pre>
</div>
<p>As one more example, I define aliases to help me ssh between all my machines more easily. There&#8217;s no point in setting this up for the machine you&#8217;re on, so I do:</p>
<div class="highlight" style="background: #f8f8f8;">
<pre style="line-height: 125%;"><span style="color: #666666;">[</span> <span style="color: #19177c;">$host</span> !<span style="color: #666666;">=</span> <span style="color: #ba2121;">"net"</span> <span style="color: #666666;">]</span> <span style="color: #666666;">&amp;&amp;</span> <span style="color: #008000;">alias </span><span style="color: #19177c;">ssh_net</span><span style="color: #666666;">=</span><span style="color: #ba2121;">'ssh user_name@net.cs.yale.edu'</span></pre>
</div>
<p>Now all you have to do is create another bash script that rsync&#8217;s your .bashrc file to all your accounts, and run it every time you make a change.</p>
]]></content:encoded>
			<wfw:commentRss>http://ashishagarwal.org/2010/11/10/platform-independent-bashrc-file/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
