Skip to main content
The special shell variable $_ holds the last argument of the previous command. It’s especially handy in interactive Bash sessions and scripts when you want to avoid retyping long or dynamic arguments.

Why Use $_?

  • Boosts productivity by reducing repetitive typing
  • Seamlessly reuses file names, directory paths, or any last argument
  • Works in interactive shells and within scripts
$_ refers strictly to the last argument of the previous command. If that command had no arguments, $_ will be empty.

Interactive Shell Examples

Listing and Copying a File

Here, $_ expands to file.conf, so you don’t have to type it twice.

Chaining Commands

Since the last command was echo "Done", $_ now contains Done.

Using $_ in Scripts

Here, $_ saves you from typing project_build again.

Common Use Cases


Advanced Examples


Further Reading

If you chain multiple commands with ; or &&, $_ always reflects the very last argument of the last executed command.

Watch Video