As a full-stack developer, I spend a considerable amount of time working in Bash. Mastering shortcuts and customizations to make Bash work the way I want has been hugely valuable.
The built-in shopt
command unlocks powerful ways to tweak Bash to boost productivity for programmers and sysadmins alike.
In this comprehensive 3k word guide for developers, I‘ll share my top tips for customizing Bash using shopt gained from 15+ years experience coding.
Why Customize Your Shell?
Let‘s first discuss why you should care about customizing Bash beyond the defaults:
1. Match your mental model
We all have slightly different ways we prefer to interact with the command line. Customizing Bash closes the gap from the defaults.
2. Accelerate repetitive tasks
The shell underpins everything you do – small efficiencies compound.
3. Reduce typos/mistakes
A minor typo can cause major issues. Tweaks like cdspell
prevent silly mistakes.
4. Improve information visibility
More control over output formats reduces parsing effort.
5. Adopt power user features
Uncover advanced functionality without changing tools.
6. Make Bash enjoyment 📈 rather than 📉
A customized tailored experience encourages usage over barebones defaults.
Simply put: you use Bash a lot – why settle for default UX?
Now let‘s see how to customize with shopt
.
An Introduction to the shopt Command
The shopt
builtin allows changing shell options dynamically during a session. It exposes settings controlling key aspects of Bash behavior.
Basic syntax:
shopt [-s|-u] [options]
Let‘s breakdown the flags:
-s
: Enables option-u
: Disables option- No flag: Checks if option is set
For example:
# Enable option
shopt -s autocd
# Disable option
shopt -u autocd
# Check status
shopt autocd
Now let‘s explore impactful customizations.
Top 10 shopt Options for Developers
After years using Bash across projects ranging from containerized cloud services to embedded systems, these are my "desert island" picks.
1. cdspell
Use Case: Fixes minor mistakes in cd commands.
Ever run cd dktop
and gotten an error that the directory doesn‘t exist? This option attempts to autocorrect close typos by "fuzzily" matching destinations:
shopt -s cdspell
cd dktop
# Actually changes dir to Desktop
This seemingly simple fix prevents many annoying failed cd‘s.
2. cmdhist
Use Case: Recall previous commands for easy re-running.
Access history substitutions with !num
:
shopt -s cmdhist
# Run command #8 again
!8
# Repeat last command
!!
No more repetitive typing or scrolling through hundreds of lines of history.
3. lithist
Use Case: Prevent loss of command history between sessions
Normally Bash history gets overwritten each session. This appends instead:
shopt -s lithist
# Now history accumulates across sessions
history
Stop losing valuable context, especially across ssh sessions.
4. histverify
Use Case: Avoid accidentally re-running dangerous/destructive commands
Displays the substitution before executing from history:
shopt -s histverify
# Verifies command before running
!872
Avoids catastrophically running rm -rf /
again.
5. dirspell
Use Case: Auto-correct mistakes when specifying directories
This is like cdspell
but for operations inside dirs:
shopt -s dirspell
ls /hoem/dekstop/
# Actually runs on /home/desktop/
Now tab-completion isn‘t even necessary to accurately reference common directories.
6. globasciiranges
Use Case: Support extended ASCII characters in globs
Enables using extended ASCII in globs:
shopt -s globasciiranges
mv file[128-255].txt processed/
Useful when handling international character sets.
7. globstar
Use Case: Match multiple directory levels with **
The **
glob matches 0+ directories:
shopt -s globstar
# Move all txt files in tree
mv **/*.txt archived/
Be careful with recursiveDELETEs! But helpful for bulk operations.
8. extglob
Use Case: Use advanced pattern matching expressions
Enables constructs like @(foo|bar)
regex-like blobs:
shopt -s extglob
rm @(tmp_*|.*)
Unlocks expressive power similar to Perl without changing tools.
9. failglob
Use Case: Prevent partial matches on globs from passing silently
Returns error if a glob only expands to some matches:
shopt -s failglob
mv foo.txt no_match*
# Fails due to unmatched wildcard
Eliminates confusing logic errors from missing files.
10. nocaseglob
Use Case: Case-insensitive globs
Match filenames regardless of casing:
shopt -s nocaseglob
mv *JPG images/
# Moves .jpg, .JPG, .jpG, etc.
Avoid annoying filename case mistakes.
These are my everyday go-to options for supercharging development workflow.
Now for some honorary mentions:
dotglob: Also matches hidden dotfiles with globs like *
.
nullglob: Returns empty string on no matches rather than literals.
expand_aliases: Enables nested aliases.
Check the full list with shopt
on your system and enable additional ones tailored to your needs.
Alternatives to shopt Customization
While shopt
is great for toggling options dynamically, other approaches like aliases and scripts can also customize Bash:
Aliases map shortcuts to commands:
alias ll=‘ls -alhF‘
ll # Runs ls with preferred flags
However these don‘t allow the advanced functionality of shopt extensions.
Scripts automate workflows in Bash:
#!/bin/bash
# myscript
echo "Running complex task..."
# Do something
But require separate execution instead of enhancing existing commands.
In summary:
- shopt: Built-in way to tweak shell behavior
- Aliases: Shortcuts for commands
- Scripts: Automate sequences of tasks
Shopt fits changing preferences without external code, though for more advanced programming integrating a tool like Perl or Python may be preferable.
Now let‘s make shopt
changes permanent.
Making shopt Persistent
By default shopt
customizations only apply to the current shell session.
To make options persist across logins, add to your Bash profile:
1. Open .bashrc config file:
nano ~/.bashrc
2. Add shopt commands:
# Enable cdspell globbing
shopt -s cdspell
shopt -s globstar
3. Save and close file
Now options apply automatically whenever you open Bash!
Conclusion
Customizing CLI environments to match personal preferences helps increase engagement, enjoyment, and ultimately, productivity and performance.
After many years using Bash across projects, these are the shopt
settings I find provide the greatest improvements:
- cdspell
- cmdhist
- lithist
- histverify
- globstar
- globasciiranges
I encourage all developers to review the available options and tweak Bash to their needs. The effort invested pays continuous dividends each time Bash is used.
Start customizing your shell with shopt
today to enhance your overall developer experience!