Per-directory environment variables, now safer and better

| No Comments | No TrackBacks
As I wrote about previously, using bash_varstash and bash_smartcd you can set up your bash shell to set important environment variables without intervention when changing to particular directories, and have it automatically restore them to their previous state when leaving.  The library is on github.

While this library has served me very well, I've now released a new version with several significant and interesting changes.

autostash

First is the addition of a new command, called autostash. It will perform the unstash for you, if run from smartcd, when the directory is left, without explicit configuration.  This significantly lowers the already fairly low bar for using this feature, and adds a bit more DTRT.

An example might now look like this:

autostash PATH
export PATH=/foo:$PATH

and that's all you need!  Maybe in a later release, these can be combined into a single command, but not today.

New script layout

Second is a complete change in structure in how the scripts themselves are located and executed.  Previously, files would be co-located with the directory they correspond to.  For example, the directory /foo/bar would contain the file /foo/bar/.bash_enter which would be run upon 'cd'ing into it.  Now, however, the files are located in a hierarchy under your home directory, primarily for security reasons.

To make the transition easier, if a legacy script is found, smartcd will notify you about it and tell you how to migrate it to the proper location.  In addition, if you manually run stash, unstash, or autostash, they too will tell you how to create a proper script to automate the action you just ran.

Here are some examples of this in action:

$ cd my-project/
# NOTICE!  Deprecated .bash_enter found, please migrate this file to the new
# format using the following (or similar) commands:
mkdir -p "/home/username/.smartcd/home/username/my-project"
cat "/home/username/my-project/.bash_enter" >> "/home/username/.smartcd/home/username/my-project/bash_enter"
rm "/home/username/my-project/.bash_enter"

$ autostash PATH
# You are manually autostashing a variable.  To automatically perform this
# whenever you enter this directory, paste the following command(s):
mkdir -p "/home/username/.smartcd/home/username/my-project"
echo autostash PATH >> "/home/username/.smartcd/home/username/my-project/bash_enter"

To turn off these help messages, you can set the variable VARSTASH_QUIET

Interactive and automatic configuration

To additionally simplify configuring things, some additional methods for editing the bash_enter and bash_leave scripts are provided.

If you just want the recommended actions listed in the previous section to be performed for you with no questions asked, you can set the variable VARSTASH_AUTOCONFIG

$ VARSTASH_AUTOCONFIG=1
$ autostash foo
Automatically running echo autostash foo >> "/home/username/.smartcd/home/username/bash_enter"

If you simply want to open the file in your preferred editor, you can run varstash_edit

$ varstash_edit
Usage: varstash_edit ( bash_enter | bash_leave )
$ varstash_edit bash_enter
[ $EDITOR invoked on the correct bash_enter file ]

This can also happen automatically when you type stash, unstash, or autostash if you set the variable VARSTASH_AUTOEDIT.  This will first add the commands, as with VARSTASH_AUTOCONFIG, then launch your editor

$ VARSTASH_AUTOEDIT=1
Automatically running echo autostash foo >> "/home/username/.smartcd/home/username/bash_enter"
[ $EDITOR invoked on the correct bash_enter file ]

No TrackBacks

TrackBack URL: http://cxreg.com/blag/mt/mt-tb.cgi/6

Leave a comment

About this Entry

This page contains a single entry by cxreg published on May 30, 2011 1:03 AM.

Bash arrays, variable stashing, and cd aliasing = magic goodness was the previous entry in this blog.

Find recent content on the main index or look in the archives to find all content.