$_ 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
$_ expands to file.conf, so you don’t have to type it twice.
Chaining Commands
echo "Done", $_ now contains Done.
Using $_ in Scripts
$_ saves you from typing project_build again.
Common Use Cases
| Scenario | Last Command | $_ Value |
|---|---|---|
| Copying a file | cp large_archive.tar.gz /backup | /backup |
| Editing a file | vim /etc/nginx/nginx.conf | /etc/nginx/nginx.conf |
| Moving a directory | mv logs_old logs_archive | logs_archive |
| Pipelining commands | grep ERROR logfile.log | logfile.log |
Advanced Examples
Further Reading
If you chain multiple commands with
; or &&, $_ always reflects the very last argument of the last executed command.