shopt

shell builtinLinux/Unix
The shopt command is one of the most frequently used commands in Linux/Unix-like operating systems. shopt Set and unset shell options

Quick Reference

Command Name:

shopt

Category:

shell builtin

Platform:

Linux/Unix

Basic Usage:

shopt [options] [arguments]

Common Use Cases

    Syntax

    shopt [-pqsu] [-o] [optname...]

    Options

    Option Description
    -s Set (enable) each specified option
    -u Unset (disable) each specified option
    -q Quiet mode; suppress output and return status indicates if option is set
    -p Display a list of all settable options with an indication of whether each is set
    -o Restrict the values of optname to those defined for the -o option to the set builtin
    Common Options Description
    cdspell Auto-correct minor errors in directory names for the cd command
    checkwinsize Update the values of LINES and COLUMNS after each command if necessary
    cmdhist Save multi-line commands in history as a single entry
    dotglob Include hidden files (starting with .) in pathname expansion
    extglob Enable extended pattern matching features
    failglob Cause patterns that match no files to produce an error
    globstar The ** pattern will match all files and zero or more directories and subdirectories
    histappend Append to history file rather than overwriting it
    nocaseglob Case-insensitive pathname expansion
    nullglob Patterns which match no files expand to a null string, rather than themselves

    Examples

    How to Use These Examples

    The examples below show common ways to use the shopt command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    # Basic Examples Basic
    shopt
    Display all shell options and their current values.
    shopt -s extglob
    Enable extended pattern matching operators.
    shopt -u dotglob
    Disable including hidden files in pathname expansion. # Advanced Examples Advanced # Check if a specific option is set if shopt -q extglob; then echo "Extended pattern matching is enabled" else echo "Extended pattern matching is disabled" fi # Set multiple options at once shopt -s extglob globstar # Reset multiple options at once shopt -u dotglob nullglob # Display current options with their settings in a reusable format shopt -p # Save options state to restore later saved_opts=$(shopt -p) shopt -s extglob # Do something that needs extglob $saved_opts # Restore previous options # Check for interactive shell if shopt -q login_shell; then echo "This is a login shell" else echo "This is not a login shell" fi # Enable useful options for scripting shopt -s nullglob failglob for file in *.txt; do echo "Processing $file" done # Ensure case insensitive matching for pathname expansion shopt -s nocaseglob for file in *.JPG; do echo "Found image: $file" done # Use cdspell to auto-correct cd typos shopt -s cdspell cd /ect # Will be corrected to /etc

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The `shopt` command is a built-in Bash shell utility that allows users to control various optional shell behaviors. It provides a way to set or unset (enable or disable) different shell options that affect how Bash behaves in various scenarios. The name `shopt` is short for "shell option". Unlike the `set` command, which primarily handles shell flags defined by the POSIX standard, `shopt` manages Bash-specific options that extend the shell's functionality. These options control features such as pathname expansion behavior, error handling, command history management, and more. Key features of the `shopt` command include: 1. Option Management: The primary purpose of `shopt` is to enable (`-s`) or disable (`-u`) specific shell behaviors. 2. Status Checking: With the `-q` flag, scripts can check if an option is enabled without producing output, making it useful for conditional logic. 3. Configuration Display: When used without arguments, `shopt` displays all available options and their current settings. The `-p` flag formats this output in a reusable format. 4. Persistent Settings: Many users add `shopt` commands to their `.bashrc` or `.bash_profile` files to customize their shell environment permanently. Some of the most useful options managed by `shopt` include: - `extglob`: Enables extended pattern matching operators like `?(pattern)`, `*(pattern)`, `+(pattern)`, etc. - `globstar`: Enables the `**` pattern for recursive directory matching. - `cdspell`: Automatically corrects minor typos in directory names when using the `cd` command. - `histappend`: Appends to the history file rather than overwriting it. - `checkwinsize`: Updates the LINES and COLUMNS variables after each command if the window size has changed. - `nocaseglob`: Makes pathname expansion case-insensitive. The `shopt` command is particularly valuable for: - Shell scripting, where it can enable powerful pattern matching and error handling features. - Customizing the interactive shell experience for enhanced productivity. - Troubleshooting shell behavior issues. - Creating portable scripts that explicitly set their required shell options. It's important to note that `shopt` is specific to Bash and is not available in other shells like `sh`, `zsh`, or `dash`. This means scripts that use `shopt` should explicitly specify Bash as the interpreter (e.g., with `#!/bin/bash`) to ensure proper execution.

    Related Commands

    These commands are frequently used alongside shopt or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the shopt command works in different scenarios.

    $ shopt
    View All Commands