Customizing your environment -- Aliases, and Dot-Sourcing

Tue, Jun 14, 2005 3-minute read

Since it takes a little while to get approved by BetaPlace, I’m going to hold of on the answer to prompt customization until next time. However, we do have some more cool things that will help you customize your environment.

The MSH shell is designed to be both verbose, and pithy. (Jeffrey’s Channel9 interview quote springs to mind.) We enable this through the use of aliases, like many other shells.

Sure, it’s great that you have descriptive commands such as “get-location” and “new-hostbuffercellarray”. They’re great for reading, great for showing up as hits in help searches, and great when you need to understand a script you wrote 6 months ago. However, I didn’t learn the Dvorak keyboard layout to spend my day typing 20-character commands.

Writing an alias for a command takes the form:

MSH:33 C:\>set-alias cls clear-host

Now, typing ‘cls’ has the same effect as typing ‘clear-host.’ Of course, ‘set-alias’ is aliased to ‘alias’ to make your life easier, and sentences like this more difficult.

If you want to persist these changes, as you did with your prompt, store them in your profile.msh. You’ll notice that we define a great number of convenience commands this way. Prefer aliases to obtain terseness, rather than naming your scripts this way. Just as the verbose Monad cmdlet names help you find them during searches, so will the titles of your descriptively-named custom scripts.

Now that you’re getting a bit of customization done to your profile, it’s probably time to describe a healthy habit. That is, separating your profile into the part that the Monad team ships, and the part that you’ve customized. We’ll accomplish this by putting all of our custom profile modifications into a separate file, called profile-custom.msh.

  1. In your editor of choice, create a new file called profile-custom.msh. Save it to the same directory that contains your default profile.msh.
  2. Take all of the changes you made to the default profile, and put them instead into the profile-custom.msh.
  3. Add the following lines to the bottom of profile.msh:

# Load our profile customizations
. (combine-path $MyDocuments \msh\profile-custom.msh)

The last step is called “dot-sourcing.” Dot-sourcing works on both files and functions. When you dot-source either of these, Monad treats the commands they contain as though you had typed them inline. So, dot-sourcing your custom profile makes Monad treat your customizations as though they were actully in-line with the rest of the profile.

Here’s another example of dot-sourcing, this time using functions.

## Scratch.msh
##

function ExampleFunction
{
   $myVariable = 10
   "Hello World"
}

ExampleFunction
$myVariable

. ExampleFunction
$myVariable

Produces the result:

MSH:3 C:\temp>.\scratch.msh
Hello World
Hello World
10

Notice that the value of $myVariable is still available after dot-sourcing the function. The same does not hold true when you call the function directly.

So, here’s a question for those of you who have played with the shell so far: What idioms from other languages / shells do you find the hardest to un-train yourself from? For example, “cd..” is a valid command in DOS, but not MSH. What have you found the hardest to discover? What did you do to try and discover it?  I’m looking for things like,

“I couldn’t figure out how to use a hash table, so I typed get-command *hashtable*"

[Edit: Monad has now been renamed to Windows PowerShell. This script or discussion may require slight adjustments before it applies directly to newer builds.]