bash Functions

Posted in: Development, Technology

I set up a bunch of aliases in my ~/.profile a while back, stuff like this:

  • alias ex1="ssh ex1.example.com"
  • alias src="cd ~/Sites/src"

Since I use the command line interface to Subversion (I normally prefer a GUI, but for SVN I think the command line is actually faster), I’ve been navigating my source directories more and more often.

Tonight I learned about bash functions - which have now replaced almost all of my aliases.

  • ex () { ssh $1.example.com ; }
  • src () { cd ~/Sites/src/$1/trunk ; }

Now I can do stuff like this:

ex ex1

and it becomes:

ssh ex1.example.com

and:

src tasks

becomes:

cd ~/Sites/src/tasks/trunk

Probably 70% of you our there don’t care, and 28% already knew this, but for that remaining 2%… this should be gold. :)

Popularity: 6% [?]

Posted January 12th, 2006 @ 3:23 AM

7 Replies

  1. matt adds this Comment:

    Thanks for sharing, appreciate it. Guess I’m part of the 2% ;)

    January 12th, 2006 at 8:22 am

  2. Ian adds this Comment:

    Very gold, thanks!

    January 12th, 2006 at 8:40 am

  3. Edward O'Connor adds this Comment:

    For SSH, you can use your .ssh/config file to specify short names for hosts. Here’s mine as an example.

    For cd short-handing, I imagine bash has an equivalent for csh’s cdpath.

    January 12th, 2006 at 12:19 pm

  4. ConĂ¡nn adds this Comment:

    Nope! that is cool and I am now using it.

    January 14th, 2006 at 2:04 pm

  5. Don Mitchell adds this Comment:

    Bash does have a CDPATH env variable you can set to make switching directories easier. It’s a path style variable so you set it to a list of directories and then any cd command will check that list of PATHs to find the first match and put you into that directory. So given the above src() function, you could set CDPATH to something likie:

    export CDPATH=.:~/sro/trunk:

    January 14th, 2006 at 11:30 pm

  6. Maarten adds this Comment:

    Was just trying to figure this out the other day. Thanks!

    January 19th, 2006 at 12:47 pm

Add a Comment

Please note: Use of a non-personal web site or blog in the field below and/or comments that are off-topic, personal attacks, or support requests will likely be removed at my discretion.

Note: This post is over 2 years old. You may want to check later in this blog to see if there is new information relevant to your comment.

“Needs More Cowbell” » « Smart People

About This Site

This is the personal web site of Alex King, an independent developer based in Denver, Colorado USA. More...


Crowd Favorite

Crowd Favorite is my software and web development business.

We build web applications, design and develop custom WordPress themes and plugins, and build custom sites using WordPress as a CMS.


I also have a tumblog that aggregates my online content from other services (Twitter, Flickr, del.icio.us. etc.).

Ads