gpollice

Sometimes I feel like an antique: tcsh -> bash

In Software, Tools on November 12, 2011 at 5:34 pm

The other day I was doing something in the Linux shell and one of my students expressed surprise that I was using tcsh. Today, everyone seems to be using the more modern bash, or Bourne Again Shell. I got to thinking why I use tcsh and realized that it’s just so comfortable. As things change so rapidly and there’s so much to learn I tend not to relearn something when I don’t have to.

But then I thought, “am I missing out on something?” Sometimes I think it’s good to just learn a different way of doing things for the fun (or frustration of it). So today I decided to become a little bit better at bash. Now, like any of the shells, the man page for bash is pretty daunting. But all I wanted to do was set up an account on a clean Ubuntu Linux system that I’m preparing for use in a testing course next term. I’m creating a VirtualBox machine image so that all of the students will have the same environment for doing their work. If you haven’t tried VirtualBox, I highly recommend it as a choice for virtualization. I’ve used it hosted on Mac, Windows, and Linux and it works just fine.

So, after I created the VM for Ubuntu I wanted to have some of the things that are familiar to me when I use tcsh as my shell available in bash. Mainly I wanted a prompt that shows something like “gpollice [n]: “, where n is simply the current command in the command history. I also wanted to have some simple shortcuts that I alias, such as ls as a rename for ls -CF and ll for ls -l.

In tcsh, these are simple aliases that look like this in my .tcshrc file:

alias ll ls -l
alias ls ls -CF

The conversion to bash is simple. bash assumes that you want to put your aliases together and the default .bashrc file includes a file called .bash_aliases where you can put all of your aliases. The two aliases above convert directly with the addition of an equal sign separating the alias name and its contents. The aliases I have in .bash_aliases are:

alias g='grep -i'
alias ls='ls -CF'
alias ll='ls -l'
alias la='ls -A'
alias up='cd ..;pwd'

I was pretty confident that the conversion would be pretty easy. I next wanted to get the prompt configured and finding the place in .bashrc was pretty easy. But, I wasn’t sure how I was going to get the history count. In tcsh, I do it this way:

set prompt = "`whoami` [\!]: "

In my .bashrc file I simply inserted the line:

PS1 = "`whoami` [\!]: "

It was just a matter of finding out what I had to change, and it was PS1, instead of prompt.

Now for the hard part. There is a set of aliases that I’ve used since 1986 that I got from Bill McKeeman at the Wang Institute of Graduate Studies. It’s a neat way of bookmarking directories and getting back to them quickly. In tcsh, it’s a set of aliases that look like this:

alias listwork	'ls -al ~/.wrk* | fgrep '.wrk' | sed "s/^.*.\.wrk\.//"'
alias rmwork	'rm -f ~/.wrk."\!*"'
alias setwork	'rm -f ~/.wrk."\!*"; ln -s "`pwd -P`" ~/.wrk."\!*"'
alias work	'cd ~/.wrk."\!*";echo "pwd: `pwd -P`";cd `pwd -P`;ls;'

This simply creates soft links to the directories in my home directory that are prefixed with “.wrk.” It’s an elegant way to do quick navigation. I would be lost without this. Since everything came along pretty easily thus far I was hopeful that I’d have my bash environment set up pretty quickly. WRONG! I spent the afternoon tracking down how to actually get it done. In the process, I learned about shell functions in bash and found that they really are nice and simplify a lot of things. I suspect I’ll now go crazy and put a ton of these into my environment;mdash&but that’s for another time.

Without going into details, the solution to this was to add these lines to my .bash_aliases file.

setwork () { rm -f ~/.wrk.$1; ln -s "`pwd -P`" ~/.wrk.$1; }
listwork () { ls -al ~/.wrk* | fgrep '.wrk' | sed "s/^.*.\.wrk\.//"; }
rmwork () { rm -f ~/.wrk.$1; }
work () { cd ~/.wrk.$1; cd `pwd -P`; echo "pwd: `pwd -P`"; ls; }

The thing to remember is that when you want arguments to shell commands, you can’t use an alias. You need to use a function. But functions are not hard to learn. Once I found the right information it took just a few minutes.

I might become a bash user after all. At least this old duck can still learn new tricks.

Advertisements
  1. I was surprised to see my name pop up here. I hardly remember using my old catalog of csh hacks, much less what they were. Anyway, I am glad they are still useful after 25 years or so. I comfort myself by believing forgetting the old makes way for the new. Goodbye to FAP and PLI/F and lio-dac-dpy-dac; hello to the cloud, photoshop and erlang. We often debated what would be useful to our students for 50 years. Perhaps the courage to forget should have headed the list. /s/ Bill

    • Hi Bill. It’s amazing how some things just work and we depend upon them. I’ve gotten so used to the “work” aliases. They’ve become indispensable. I’m not sure whether it really deterred me from learning bash, or whether I was just too lazy. But it was fun porting them to bash.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: