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. But $HOME/Lab12 works as expected. The cdable_vars option essentially provides a way for you to set up shortcuts to directories without having to create symbolic links. Another useful option that helps with directory changes is cdspell. This one allows the shell to compensate for minor typos in the paths you type. Meant to go to /tmp, but you typed cd /tpm, no problem!
$ shopt -s cdspell $ cd /tpm /tmp
The force_fignore, set by default, uses whatever value you have assigned to $FIGNORE to prevent path completion using that name. If, for example, you have two directories -- ignore.yes and ignore.no -- and you set FIGNORE to "yes", you will be able to use path completion for ignore.no, but the shell will act as if ignore.yes doesn't exist -- for path completion anyway. You have to type each letter to cd into it.
$ FIGNORE="yes" $ mkdir ignore.yes $ mkdir ignore.no $ cd ignor[TAB] $ pwd ignore.no
The force_fignore variable can be used when there are particular directories that you don't want your uses to accidentally enter when using path name completion. The FIGNORE variable can be set to a complete directory name or, as in the example, to a file extension or right substring of the file name. Choose your own path, but let shopt make it easier.
Read more of Sandra Henry-Stocker's Unix as a Second Language blog and follow the latest IT news at ITworld, Twitter and Facebook.
This article is published as part of the IDG Contributor Network. Want to Join?