DEV Community

Jared Caraway
Jared Caraway

Posted on

TIL: shopt lets you cd without typing 'cd'

I've been using Linux for about 15 years now. I started out with an Ubuntu LTS distro loaded onto a friend's old, donated Windows box and learned all the basics I needed to get started. It's been downhill from there.

One of the foundational commands for getting around in Linux by way of the terminal is cd. I've known about it since before I knew about Linux by way of Windows: this command is so ubiquitous that it made its way over to the Windows shell, too.

cd means "change directory". You typically type something like cd ~/Videos to change the terminal's current working directory to, in this case, Videos.

I'm currently setting up our first household Raspberry Pi which brought me back into Linux land (which, as a Mac power user, I never really left). In the process of tinkering with a fresh Linux distro, I figured I'd ask my old friend Claude what those with more knowledge of the Linuxverse would do to trick out their stock distros.

Introducing: shopt

Claude opened my eyes to the magic of shopt (which stands for "shell options). The Bash shell is POSIX-compliant by default, so additional options have been tucked away behind this command.

I'll skip straight to the good stuff. Here's how you can upgrade your terminal so you can change directory simply by typing the target directory, sans cd.

  1. Open Bash and edit the .bashrc file with your preferred editor (I'm a basic Nano guy still, don't @ me). Like so:

    nano ~/.bashrc

  2. Add the following lines somewhere - I usually just jump to the bottom:

    # cd into a directory by just typing its name (no cd needed)
    shopt -s autocd

  3. Save, close, and then refresh your .bashrc file:

    source ~/.bashrc

What It Looks Like in Action

That's it!

Simple as Pi, eh? Thanks for reading - I hope you learned something today! Let me know if there's anything else you'd like me to cover in future articles.

Top comments (0)