These 7 Bash tricks will change how you use the terminal


Do you spend most of your time in the terminal, or are you trying to learn? Bash is very mature, and it packs a lot of hidden features. Today, I have seven command-line tricks that will save you time typing and editing.

Most of these tricks leverage Bash’s history expansion. You’re probably familiar with its most basic syntax—!!—which re-executes the previous command, but there’s more to the story here, and history expansion goes a bit deeper. We can use expansions to select a command, modify it, and then re-execute.

Bash’s history expansions comprise three segments, and what you saw (“!!”) constitutes the first segment, called the event designator. There are two more segments called the “word designator” and (optional) “modifier.” We put them together like this:

event-designator[:word-designator[:modifiers]]

A real-world example is easier to read:

!!:2:p  # You can use multiple modifiers, like :p:h.
  1. Event designator (e.g., the second “!” in “!!”): Defines which command in your history to target.
  2. Word designator (e.g., “2”): Defines which words (command arguments) to target. Words are generally separated by spaces or surrounded by quotes. Word zero is the command name.
  3. Modifier (e.g., “p”): Optionally defines what to do with the chosen word.

Broadly speaking, select a command, specify a word, and then optionally modify it. “!!:2:p” selects the previous command (“!!”), the second argument (“2”), and prints it (“p”.)

The first “!” activates Bash’s history expansion, and the expression that follows (e.g., another “!”) is the event designator. This value dictates which command you select. The only exception to a “!” prefix is “^,” which activates a string replacement (covered later.)

Dell XPS 13 Plus 2023

Operating System

Ubuntu Linux 22.04 LTS

CPU

13th Gen Intel Core i7-1360P

GPU

Intel Iris Xe Graphics

RAM

16GB DDR5


The official documentation provides lists that describe all the possible combinations. It’s challenging to see the value to begin with, but once you find a few useful tricks and understand how to form them, you’ll intuitively use them without thinking. Below, I have a few tricks I’ve found useful over the years.

Reuse all arguments

Different commands can sometimes use the same arguments

Linux mascot wearing glasses and suspenders, using a laptop beneath pixelated 'GEEK' text. Credit: Lucas Gouveia/How-To Geek

There are scenarios where you need to apply identical arguments to multiple commands. Take the following example:

touch foo bar
chmod 644 foo bar

It would be nice if we could reuse the arguments—”foo” and “bar”—and we can:

touch foo bar
chmod 644 !!:*

It says, “Select the previous command (“!!”) and reuse all its arguments (“*”). It’s the same as executing chmod 644 foo bar.

However, Bash allows us to shorten it even further by omitting a “!:” for symbol-based word designators (“*”, “^”, “$”, and “%”). For example:

touch foo bar
chmod 644 !*

“!*” is the same as “!!:*.”

Reuse the nth argument

Some arguments are too difficult to retype

A man sitting comfortably on an armchair using a laptop, with the Linux mascot Tux standing beside him and floating icons representing media, gaming, and music. Credit: Lucas Gouveia/How-To Geek | New Africa/Shutterstock

Similar to the previous expansion, we can select specific arguments from a command:

touch foo bar
chmod 644 !!:2  # Select the second word of the previous command.

It’s the same as:

chmod 644 bar

To elaborate on event designators a little, let’s choose a different command:

The event designator (e.g., the second “!” in “!!”) is how we choose a command.

touch foo bar
ls
chmod 644 !-2:2  # Select the command two places behind ("-2"), and choose the second ("2") word ("bar").

“!” activates history expansion, and “-2” chooses the command two places behind in your history. In comparison, “!!” is shorthand for “!-1” (previous command).

Event designators are flexible, and you’ve probably used them before when re-executing a command from your history, e.g., !100.

Reuse the command name

It’s useful once committed to the brainstem

Linux mascot sitting in a seashell using a laptop, with PowerShell, Nushell, and Murex logos around. Credit: Lucas Gouveia/How-To Geek

Typing out the command name is easy, so you may wonder if this trick is worth the effort. Well, typing out “!:0” is even easier, and once you’ve built a habit of doing so, you do it without thinking (like !!):

touch foo bar
!:0 baz

This is the same as touch baz.

  • “!”: Activates history expansion.
  • “0”: Select only the command name.

Excluding the event designator (the character after the first “!”) implies the previous command.

You can select any command by specifying a number as the event designator:

!100:0 baz

However, in that case, it’s probably easier to type the command instead.

Fix a typo

Because manually replacing mistakes is a pain

Illustration of the Linux Tux mascot looking worried beside a terminal window, with a warning icon and skull symbols. Credit: 

It would be silly of me to ask if you’ve ever mistyped a command—of course you have. However, you’ve probably strained your fingers and mental energy fixing them—struggling to move the cursor to the correct place and amending the commands. There is an easier solution: a string replacement.

ehco foo bar
^ehco^echo

It’s a simple string replacement, and you can change any part of the command:

echo "Any part of the cmd."
^cmd^command
A terminal window shows an echo command being re-run with one word replaced by another using a string replacement expansion.

If you’re familiar with sed, the previous expansion is the same as the following:

echo "Any part of the cmd."
!!:s/cmd/command/

Reuse the first or last argument

It’s more convenient than specifying argument numbers

A man using a laptop next to Tux, the Linux mascot wearing sunglasses. Credit: Lucas Gouveia/How-To Geek

We’ve seen how to reuse all arguments from a history entry (e.g., “!*”), but we can specifically pick the first or last argument.

Pick the first argument:

touch foo bar
chmod 644 !^

That’s the same as executing chmod 644 foo, and “!^” is equivalent to !!:1 (previous command; first argument.)

Pick the last argument:

touch foo bar
chmod 644 !$

That’s the same as chmod 644 bar.

“$” is particularly useful if you don’t know the number of arguments.

If you know regex, you’ll be familiar with “^” and “$” already, which mean the start and end of a line. That’s a handy way to memorize them.

Expand before submission

Check the command before executing it

The Linux mascot, Tux, looking thoughtful while surrounded by signs saying "Do" and "Don't." Credit: Lucas Gouveia/How-To Geek

All of these expansions can be a little confusing unless you ground them by viewing real (expanded) commands—that way, you can see what they actually do.

To do that, type out your expansion and hit Ctrl+Alt+E:

touch foo bar
chmod 644 !$ <Ctrl+Alt+E>

That will expand the expression into literal values without executing the command. Now you can see what it does before executing it.

Move to the start or end of the command line

Moving around is difficult, and shortcuts make it easy

Linux mascot wearing sunglasses and using a laptop, surrounded by floating windows with the i3 Window Manager logo in the background. Credit: Lucas Gouveia/How-To Geek

Moving around the command line isn’t obvious to beginners, but Bash provides several shortcuts to assist you. Two of them will move you to the start or end of the line, with Ctrl+A or Ctrl+E, respectively.

Alternatively, if you know Vim, you can enable vi mode by putting the following in your .bashrc file:

set -o vi
Server data on a monitor with Database MySQL.


5 Great Linux Utilities to Monitor Your System Resources in the Terminal

Because the core utilities don’t do it all.


That’s all I’ve got today. Remember to check out the official documentation for history expansion, because it has lists that describe each segment, and you can easily squeeze a lot more out of these tips.

Tux, the Linux mascot, wearing sunglasses and pointing at large 3D terminal symbol.-1


These fzf tricks will transform how you use the Linux terminal

I can’t live without fzf, and you’re missing out big time if you’re not using it.



Source link

Leave a Reply

Subscribe to Our Newsletter

Get our latest articles delivered straight to your inbox. No spam, we promise.

Recent Reviews


Pool maintenance has long existed in a fragmented state, where different tools solve different problems but rarely work together in a meaningful way. Cleaning the floor, clearing the surface, and maintaining water quality have traditionally required separate interventions, often at different times. What has been missing is a system that not only automates these tasks but also connects them through intelligence.

The Aiper Experts Duo introduces that shift by combining two purpose-built robots, the Scuba V3 and the EcoSurfer S2, into a single, coordinated ecosystem. Instead of operating in isolation, these devices function as a unified system that covers every layer of the pool, from the floor and walls to the waterline and surface.

At the center of this system is Cognitive AI

This moves beyond pre-programmed cleaning cycles and into continuous optimization. The technology works as an adaptive loop that enables the robots to interpret their surroundings, make decisions in real time, and refine their behavior based on past performance. By factoring in variables such as pool size, weather conditions, and cleaning history, the system evolves with use, delivering a level of precision that static automation cannot match. Within the Aiper Experts Duo, these AI-driven capabilities are associated with the Scuba V3, where features such as adaptive cleaning modes, real-time debris detection, and intelligent path planning support navigation and cleaning across the pool’s floor, walls, and waterline.

This intelligence becomes most apparent in how the system manages time and consistency. The EcoSurfer S2 operates using SolarSeeker™ technology, allowing it to maintain surface cleaning throughout the day while intelligently seeking sunlight to sustain its energy levels. At the same time, the Scuba V3 uses AI Navium™ Mode to generate weekly cleaning plans automatically, removing the need for manual scheduling and ensuring the pool remains consistently maintained.

Performance is not just about automation but about efficiency

The Scuba V3’s AI Patrol Cleaning identifies visible debris in real time and adjusts its route accordingly, delivering up to 10× faster cleaning compared to traditional cleaners that rely on standard S-shape floor patterns.  By responding dynamically to what it detects, the system ensures that cleaning is both targeted and time-efficient. This is supported by VisionPath™ technology, which integrates AI vision with advanced sensors to map efficient paths, reduce overlap, and navigate obstacles without unnecessary repetition.

This is supported by VisionPath, which combines an initial AI-led cleaning phase that focuses on visible debris with a structured grid-pattern cleaning of the entire pool floor. The result is a balanced approach that brings together speed and consistency, ensuring that immediate cleaning needs are addressed while still delivering complete and reliable coverage.

The system’s effectiveness also comes from its ability to deliver complete coverage without compromise. While the Scuba V3 handles deep cleaning across the pool’s structure, the EcoSurfer S2 maintains the surface and supports water quality through its adjustable chlorine tablet chamber. Together, they create a continuous maintenance cycle that addresses both visible debris and underlying water balance. Features such as MicroMesh™ filtration capture even ultra-fine particles, while DebrisGuard™ ensures that collected debris remains contained.

Reliability is built into the design through both engineering and architecture

By distributing tasks across two specialized devices, the system reduces wear and improves long-term durability. Combined with solar-assisted operation and energy-efficient path planning, this approach ensures consistent performance while significantly reducing the need for hands-on maintenance, including frequent charging or manual intervention.

For homeowners increasingly investing in connected, more carefree and reliable living environments, this represents a more complete approach to outdoor automation. The Aiper Experts Duo does not simply reduce the effort required to maintain a pool; it removes the need to think about it altogether, allowing maintenance to happen seamlessly in the background.

To explore the system further, visit the official product page:
https://aiper.store/us/products/aiper-experts-duo

As part of the ongoing spring promotion, customers can access savings of up to 25 percent,  available through April 10. In addition, an extra 5 percent discount is available at checkout using the code AiperExpertsDuoXDT, valid through April 25, making this a timely opportunity to transition to a more intelligent and fully integrated pool care system.



Source link