Unix: Choosing the easy path

Wise men say that you should never choose the easy path but, instead, live life fully. But when it comes to moving around the Unix file system, easy is good. And bash's builtin shopt command can make maneuvering even the most complicated file system paths easier.

By  

Some of us have spent decades moving around our Unix file systems with the cd command and maybe even using file name completion so that we don't have to type very letter in every directory name. Even so, there may be some bash options that you might not be aware of and the shopt command has some options that might surprise you.

The shopt built-in provides numerous options for changing optional shell behavior. To view these options, just type shopt on the command line. Oh, and don't mentally parse that as "shop t", but "sh opt" as in "shell options". That should make it easier to remember.

Depending on the version of bash that you are using, you will see a variable list of options that you can set and unset with shopt -s (set) and shopt -u (unset) commands. Some of these can be used to change bash's behavior when you issue cd commands.

$ bash -version
GNU bash, version 3.2.25(1)-release (i386-redhat-linux-gnu)
Copyright (C) 2005 Free Software Foundation, Inc.

Typing shopt on a line by itself will list the current settings.

$ shopt
cdable_vars     off
cdspell         off
checkhash       off
checkwinsize    on
cmdhist         on
dotglob         off
execfail        off
expand_aliases  on
extdebug        off
extglob         off
extquote        on
failglob        off
force_fignore   on
gnu_errfmt      off
histappend      off
histreedit      off
histverify      off
hostcomplete    on
huponexit       off
interactive_comments    on
lithist         off
login_shell     on
mailwarn        off
no_empty_cmd_completion off
nocaseglob      off
nocasematch     off
nullglob        off
progcomp        on
promptvars      on
restricted_shell        off
shift_verbose   off
sourcepath      on
xpg_echo        off

Notice that this list also tells you if the option in question is enabled (on) or disabled (off).

If autocd is in your list (bash 4.1 and newer), you can use this option to make it possible to cd to a directory without typing the cd command. Instead, you just type a directory name. The shell then sees that the "command" you entered is not a command, but the name of a directory and moves you there.

$ Lab12
$ pwd
/home/unixdweeb/Lab12

A similar option, available in older (pre 4.1) versions of bash is cdable_vars. This is easier to parse than "shopt". Think of it as "cd-able variables". In other words, if a variable contains the name of a directory, you can move into the directory by using the variable with the cd command.

$ shopt -s cdable_vars
$ lab=Lab12
$ cd $lab
$ pwd
Lab12

By the way, this doesn't work if your variable is set to something like ~/Lab12 or ~jdoe/Lab12. The tilde isn't interpreted as referring to your home directory.

Photo Credit: 

flickr/ WilsonB

Join us:
Facebook

Twitter

Pinterest

Tumblr

LinkedIn

Google+

Operating SystemsWhite Papers & Webcasts

See more White Papers | Webcasts

Answers - Powered by ITworld

ITworld Answers helps you solve problems and share expertise. Ask a question or take a crack at answering the new questions below.

Join us:
Facebook

Twitter

Pinterest

Tumblr

LinkedIn

Google+

Ask a Question