PowerUp Your Shell With Your Profile

How about some neat-o shell tricks for everyone to play with? I figured out some neat things the other day while trying to add some functionality to my shell, specifically through scripts or the profile.  What I found was that the magic is in the profile!

Have you ever wanted to have commands that performed certain functions from quick keystrokes rather than long command lines? Of course you have, that’s why we all set aliases in our profiles like ‘ll’ instead of ‘ls -al’ and the like.  But what if you want or need more functionality than simple command shortening? That’s what I was after, more function in a smaller form.  Some way to pass variables to aliases to get more out of them. Ah, but you can’t pass variables to aliases, so we are stuck right? Wrong, that’s where the functions come in to play.

A neat thing I found out was that you can put functions right into your profile, and just like that you have loads more options for increasing functionality. Now, this works pretty much like any shell script (or at least it has for me so far), so there are tons of options for tweaking your command line goodness.

As an example, I am going to show you a simple case loop in a function that you can use to call just like a command, only with options that will affect your current environment, unlike actual shell scripts. With a shell script, one you set that environment variable or change directories, it all goes away once the script stops running. However, by doing this in the profile, you can alter your current shell environment. Sweet, huh?

OK, here is the case loop you can check out as an example. I named the function ‘t’, short for test, so you can call it by entering simply ‘t’ on the command line and passing a variable to it. Check it out:

t () {
  case "$@" in
  one)  echo "One"
        ;;
  two)  echo "Two"
        ;;
  *)    echo "NULL"
        ;;
  esac
}

There you go, it’s that simple. Using this example as a guide, you can setup this type of loop or any shell type script for that matter to do your masterful geeky bidding.

Enjoy!

Tell me what you are thinking?