A View from Stanage Edge in the Peak District

Project - BASH Shortcuts (sc)

I’ve been using Linux since 1999 and would class myself as an intermediate-level BASH user. I write the occasional script in BASH, but tend to use PHP-CLI for my more complex scripting needs (after all, I am a certified PHP developer).

Anyway, I don’t pretend to know every BASH command out there, so it is possible that this functionality already exists and that I am trying to re-invent a well-established wheel. But, since I haven’t yet discovered the command that already does this, I’ve decided to write my own command to manage directory shortcuts from the shell command line.


Contents:


Features

This command allows you to associate an alias to a directory and ‘cd’ into that directory using just the alias.

Notable features:

  • Create, rename, delete and label shortcut aliases for directory paths
  • Partial shortcut alias name matching
  • Export shortcut paths into environment variables
  • Theme support (light and dark themes included)
  • Includes a test suite for testing compatibility with non-Debian distros

[Back to the Top]


Installation

Installation requires copying a single file and editing an existing one.

Step 1 - Download

Get the latest version of the software from sourceforge.

Step 2 - Copy

Copy the .bash_shortcuts file to your home directory ($HOME/.bash_shortcuts).

Step 3 - Enable

Open your .bashrc file in an editor. For example:

> nano $HOME/.bashrc

Add the following lines somewhere near the end of the file:

# Add BASH directory shortcuts
if [ -f ~/.bash_shortcuts ]; then
. ~/.bash_shortcuts
fi

Step 4 - Test

Open a new BASH session and test the command:

prompt > sc

BASH Directory Shortcuts by Richard Gunn

Version 1.0

Usage:

 sc NAME           : change ('cd') to the shortcut directory
 sc -a NAME        : add shortcut for current directory
 sc -a NAME TARGET : add shortcut for target directory
 sc -u NAME        : update shortcut for current directory
 sc -u NAME TARGET : update shortcut for target directory
 sc -l             : list all defined shortcuts
 sc -e             : export shortcuts to BASH variables
 sc -d NAME        : delete an existing shortcut
 sc -h             : this help text

Short and long options:
 -a --add,  -d --delete, -e --export, -h --help, -l --list, -u --update

~
prompt > _

[Back to the Top]


Usage Example

The best way to demonstrate the BASH shortcuts command is by example.

I keep all my ogg-encoded music files on a separately mounted hard-drive. I often have to ‘cd’ into that directory to do further encoding. Adding a directory shortcut speeds up that process.

Note: My terminal prompt occupies two lines – the top line is the current directory and bottom line is the command prompt (read my BASH Tips article for instructions on how to customize your own prompt). Also, I am using the shortened options with the ‘sc’ command.

Starting from my home directory I ‘cd’ into the music directory:

~
prompt > cd /mnt/xpstorage3/music

/mnt/xpstorage3/music
prompt > _

Now I create the shortcut with the ‘add’ command:

/mnt/xpstorage3/music
prompt > sc -a music
sc: Added shortcut 'music'

/mnt/xpstorage3/music
prompt > _

The ‘sc’ command confirms that the shortcut was added.

I can confirm that the shortcut was added by listing existing shortcuts:

/mnt/xpstorage3/music
prompt > sc -l
music -> /mnt/xpstorage3/music

/mnt/xpstorage3/music
prompt > _

Now I can go back to my home directory and then try to use the shortcut:

/mnt/xpstorage3/music
prompt > cd ~

~
prompt > sc music

/mnt/xpstorage3/music
prompt > _

Success!


[Back to the Top]


Exporting Shortcuts

I added this option as an afterthought. I don’t know how much use is will be.

All the shortcuts names will be converted to uppercase and prefixed with ‘SC_’, then exported as bash variables.

To export all shortcuts into BASH variables:

~
prompt > sc -e
SC_MUSIC=/mnt/xpstorage3/music

~
prompt > _

The ‘sc’ command echoes each variable as it’s exported.

Test that the shortcut was exported:

~
prompt > echo $SC_MUSIC
/mnt/xpstorage3/music

~
prompt > cd $SC_MUSIC

/mnt/xpstorage3/music
prompt > _

Success!

TIP: You could modify the ‘if’ statement you added during the installation to automatically export the shortcuts every time you open a BASH session.:

# Add BASH directory shortcuts and export
if [ -f ~/.bash_shortcuts ]; then
. ~/.bash_shortcuts
sc -e > /dev/null
fi

[Back to the Top]


Further Notes

The BASH directory shortcuts are really just symbolic links – they are stored in the $HOME/.sc directory. When the script is executed for the first time, this directory is automatically created along with an ‘sc’ shortcut pointing to it. If you prefer, you could manage the shortcuts by creating symbolic links directly in this directory.

You can shortcut to the shortcuts directory by issuing the command:

> sc sc

(This won’t work if you delete the ‘sc’ shortcut)

Transferring this script out of .bashrc and trying to run it as a standalone script will probably run into problems – when a script is executed from the command line, a new BASH environment is created for it to run in. If you try to ‘cd’ to a shortcut directory from inside a script, it will probably fail to produce the desired result as the directory will not be changed for the session that launched the script.

This issue is the reason that I decided to implement the command as a function defined within the BASH session – I haven’t tried using ‘sc’ within a script, so maybe my implementation will work within a launched script. I just don’t know.


[Back to the Top]


Page Generated: 2017-05-28