<?xml version="1.0" encoding="UTF-8" ?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc=" http://purl.org/dc/elements/1.1/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>briancarper.net (λ) (Tag: Bash)</title><link>http://briancarper.net/tag/163/bash</link><description>Some guy's blog about programming and Linux and cows.</description><item><title>Windows Powershell: Can you handle the power?</title><link>http://briancarper.net/blog/windows-powershell-can-you-handle-the-power</link><guid>http://briancarper.net/blog/windows-powershell-can-you-handle-the-power</guid><pubDate>Sat, 15 Aug 2009 14:50:38 -0700</pubDate><description>&lt;p&gt;MS Powershell is Microsoft's ripoff of Bash.  I don't think this is a bad thing necessarily.  Bash is a good tool and it's open source.  If Windows bundled a sensible, full-fledged Bash and got rid of CMD.EXE I would dance for joy.&lt;/p&gt;

&lt;p&gt;So Powershell lets you refer to your home directory as &lt;code&gt;~&lt;/code&gt; and a bunch of commands have *nix aliases like &lt;code&gt;ls&lt;/code&gt; and &lt;code&gt;cat&lt;/code&gt;.  This is nice for those who have *nix commands firmly internalized.  You have to use &lt;code&gt;.\foo.bat&lt;/code&gt; to run things in the current directory instead of just &lt;code&gt;foo.bat&lt;/code&gt;, which I thought was cool.&lt;/p&gt;

&lt;p&gt;But Powershell is not without its problems.  For one thing I see this a lot:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;The redirection operator '&amp;lt;' is not supported yet.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;How hard is it to implement input redirection, really?  For another thing, tab completion continues to be broken.  When you hit &lt;code&gt;Tab&lt;/code&gt; it still doesn't put a slash at the end of the text it inserts.  You have to type a manual &lt;code&gt;\&lt;/code&gt; every time you hit &lt;code&gt;Tab&lt;/code&gt;, to continue tabbing your way through directories.  Thus doubling the number of keystrokes you're forced to type.  This continues to drive me crazy.  There's a &lt;a href=&quot;http://www.eggheadcafe.com/conversation.aspx?messageid=29874884&amp;amp;threadid=29852783&quot;&gt;small amount of evidence&lt;/a&gt; that someday it'll be fixed, but I'm not holding my breath.&lt;/p&gt;

&lt;p&gt;You also usually can't bundle flags together.  e.g. &lt;code&gt;rm -rf&lt;/code&gt; would have to be &lt;code&gt;rm -r -f&lt;/code&gt; in Powershell.  This is just annoying enough to bother me, but I can look past it.&lt;/p&gt;

&lt;p&gt;Sadly, Powershell also runs slower than a geriatric sea turtle.  I don't understand what it's doing that takes 10-20 seconds to startup.  Or why tab completion often lags for 5+ seconds itself.  &lt;/p&gt;

&lt;p&gt;My happiest surprise was when I tried to uninstall Powershell (so I could try version 2) and got this:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/random/powershell.png&quot; alt=&quot;/random/powershell.png&quot; title=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;This dialog listed every program installed on my computer (in random order) including every Windows Update I'd ever installed.  The worst part is that I couldn't even dismiss this dialog as an error.  For all I know, uninstalling Powershell &lt;em&gt;could&lt;/em&gt; cause every program on my computer to stop working.  I've seen stranger things happen in Windows.&lt;/p&gt;

&lt;p&gt;A person walking past my office when I saw this would have heard the crazed, maniacal, tortured laughter that only the experience of being forced to use Windows can elicit.&lt;/p&gt;</description></item><item><title>Keeping bash history in sync on disk and between multiple terminals</title><link>http://briancarper.net/blog/keeping-bash-history-in-sync-on-disk-and-between-multiple-terminals</link><guid>http://briancarper.net/blog/keeping-bash-history-in-sync-on-disk-and-between-multiple-terminals</guid><pubDate>Fri, 14 Sep 2007 14:55:13 -0700</pubDate><description>&lt;p&gt;I rely on my &lt;code&gt;~/.bash_history&lt;/code&gt; extensively to avoid retyping things, both short-term and long term (when I want to look up something I typed yesterday or last week).  &lt;/p&gt;

&lt;p&gt;First, an obvious necessity is&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;shopt -s histappend
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;in &lt;code&gt;~/.bashrc&lt;/code&gt;, so that bash just keeps on adding to the end of the ~/.bash_history file rather than obliterate the file at regular intervals.&lt;/p&gt;

&lt;p&gt;By default, bash reads from your ~/.bash_history once when you log in to a terminal, and it updates ~/.bash_history once when you log out.  I dislike both of these default behaviors.  &lt;/p&gt;

&lt;p&gt;Writing out to ~/.bash_history only when you log out is no good, because what if your terminal dies unexpectedly?  You'll lose the history of any commands you typed there.  This can happen if you &lt;code&gt;kill&lt;/code&gt; a bash process, or if the machine powers off unexpectedly, or who knows what.  So, to manually update your ~/.bash_history immediatety, at any time you can use this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;history -a
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;which tells bash to append to ~/.bash_history any commands in the current terminal that aren't already in there.&lt;/p&gt;

&lt;p&gt;The second behavior of bash I dislike is that it reads from the history file only at login, mostly because I always have many terminals open at once.  I run a command from one terminal, then run a different command from another, switch back and forth etc.  These terminals may be appending all kinds of nice new lines to the end of my ~/.bash_history, but because bash never reads the history file except at login, they're never accessible except in the terminal where the command was originally run. Luckily you can force bash to re-read your history file via:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;history -n
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;which tells bash to read any new lines that have appeared in ~/.bash_history since the last time it looked.&lt;/p&gt;

&lt;p&gt;Now, say I type a really long command, e.g. a huge long Perl one-liner, in one terminal, and I want to then access and run that command in another terminal via bash history.  With bash's default behavior, I can't do it; each terminal has its own history.  What I could do is type &lt;code&gt;history -a&lt;/code&gt; in the first terminal, and &lt;code&gt;history -n&lt;/code&gt; in the second terminal; then it'd work wonderfully.&lt;/p&gt;

&lt;p&gt;I find the thought of having to do that all the time tedious, though.  Better would be for bash to do it for me every time I run a command.  You can accomplish this by putting something like this in &lt;code&gt;~/.bashrc&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;export PROMPT_COMMAND=&quot;history -n; history -a&quot;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;PROMPT_COMMAND&lt;/code&gt; lets you specify a command that bash will run every time it shows you a fresh command prompt, i.e. every time you run a command and the command finishes.  So the above tells bash to read any new lines that have appeared in ~/.bash_history since the last time it read it, and then append the last-run command from this terminal to ~/.bash_history, every time you run a command.&lt;/p&gt;

&lt;p&gt;So now, if you type a command in one terminal, and want to access it via the history of another terminal, run a command in the other terminal (or just hit Enter) to trigger PROMPT_COMMAND, and then your history will be nicely up-to-date and synchronized with any other terminals you have open. Almost certainly, you'll never notice the tiny bit of overhead caused by bash constantly reading and writing to ~/.bash_history.&lt;/p&gt;

&lt;p&gt;See &lt;code&gt;man bash&lt;/code&gt; for more info on the &lt;code&gt;history&lt;/code&gt; builtin.&lt;/p&gt;</description></item><item><title>eselect</title><link>http://briancarper.net/blog/eselect</link><guid>http://briancarper.net/blog/eselect</guid><pubDate>Wed, 01 Aug 2007 21:36:30 -0700</pubDate><description>&lt;p&gt;Works:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;eselect bashcomp enable --global ri
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Silently fails:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;eselect bashcomp enable ri --global
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Reason: (&lt;code&gt;/usr/share/eselect/modules/bashcomp.eselect&lt;/code&gt;)&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;do_enable() {
    #...snip...
    if [[ ${1} == &quot;--global&quot; ]] ; then
        bcdir=&quot;${ROOT}/etc/bash_completion.d&quot;
        shift
    fi

    #...snip...
    # ignore any unrecognized options
    [[ ${bc} == --* ]] &amp;amp;&amp;amp; continue
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Always looks for &lt;code&gt;--global&lt;/code&gt; as the first parameter, apparently.  Bug?  Not sure.  This would be the second eselect-related bug I've noticed in the past two days, if so.&lt;/p&gt;</description></item><item><title>HISTIGNORE</title><link>http://briancarper.net/blog/histignore</link><guid>http://briancarper.net/blog/histignore</guid><pubDate>Sun, 01 Jul 2007 12:05:56 -0700</pubDate><description>&lt;p&gt;&lt;code&gt;HISTIGNORE&lt;/code&gt; is a handy bash option.  You can specify certain strings that you want bash to ignore when storing command line history in &lt;code&gt;~/.bash_history&lt;/code&gt;.  For those using &lt;code&gt;sudo&lt;/code&gt;, you could automatically prevent bash from saving any of your run-as-root commands in your history.  Or you can prevent it from storing &lt;code&gt;exit&lt;/code&gt; or &lt;code&gt;fg&lt;/code&gt; or sundry &lt;code&gt;ls&lt;/code&gt; any other such commands that aren't worth remembering.&lt;/p&gt;

&lt;p&gt;See also &lt;code&gt;man bash&lt;/code&gt;.&lt;/p&gt;</description></item><item><title>Darn you, failglob</title><link>http://briancarper.net/blog/darn-you-failglob</link><guid>http://briancarper.net/blog/darn-you-failglob</guid><pubDate>Sat, 05 May 2007 20:14:34 -0700</pubDate><description>&lt;p&gt;I turned on &lt;code&gt;bash_completition&lt;/code&gt; today.  Couldn't quite remember why I had it turned off.  I quickly realized though.  bash would fail on any filename with a square-bracket in it.  It looks something like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;~ $ touch 'test [1]'
~ $ ls&amp;lt;I HIT TAB HERE&amp;gt; bash: no match: test [1]
~ $
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In other words, hitting TAB to tab-complete filenames would dump me back to a command prompt giving me that unhelpful error message, and I'd have to start the whole command over.&lt;/p&gt;

&lt;p&gt;The problem was that I had this in my &lt;code&gt;~/.bashrc&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;shopt -s failglob
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I can't for the life of me remember why that was in my &lt;code&gt;.bashrc&lt;/code&gt;.  Clearly it wasn't doing anything I never had any use for, because this is the first time I got this &quot;bash: no match&quot; error.  Removing that line makes &lt;code&gt;bash_completion&lt;/code&gt; work again though, so all is well.&lt;/p&gt;

&lt;p&gt;This was a nightmare to debug too.  Google was totally useless.  I tracked it down to the &lt;code&gt;_filedir&lt;/code&gt; function in &lt;code&gt;/etc/bash_completition&lt;/code&gt;, and then it was trial-and-error.  Next time I'm going to start off loading a clean login environment before I bother checking the system scripts for things.  I did also learn that &lt;code&gt;set -v&lt;/code&gt; in a bash script makes it dump every line it executes to STDIN as it's executing them.&lt;/p&gt;</description></item></channel></rss>

