Advanced Bash Scripting

Special Shell Variables

Underscore

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

Note

$_ 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

$ ls -l file.conf
total 16
-rw-r--r-- 1 root root 896 Jun 18 2020 file.conf
$ cp $_ /tmp

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

Chaining Commands

$ ls -l file.conf; echo "Done"
total 16
-rw-r--r-- 1 root root 896 Jan 18 2020 file.conf
Done
$ echo $_
Done

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


Using $_ in Scripts

#!/usr/bin/env bash

# Create a new directory
mkdir project_build

# Change into the new directory using $_
cd $_

# Show current path
pwd

Here, $_ saves you from typing project_build again.


Common Use Cases

ScenarioLast Command$_ Value
Copying a filecp large_archive.tar.gz /backup/backup
Editing a filevim /etc/nginx/nginx.conf/etc/nginx/nginx.conf
Moving a directorymv logs_old logs_archivelogs_archive
Pipelining commandsgrep ERROR logfile.loglogfile.log

Advanced Examples

# Remove a file, then verify its removal
rm temp_data.csv
echo "Removed" $_

# Using in a pipeline
find . -name '*.log' | xargs gzip
echo "Compressed" $_

Further Reading

Warning

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

Watch Video

Watch video content

Previous
Zero
Next
Dash