Is it possible to run consecutive commands, with those that follow referencing the parameter(s) passed to the very first?

For example, download, untar, and cd:

wget superuser.tar.gz
tar -xzf superuser.tar.gz
cd ./superuser

Instead do:

wget superuser.tar.gz && tar -xzf $1 && cd $1
// with $1 being superuser.tar.gz

The only way I can think of accomplishing this is to reference it as a variable. Is there another solution?


$_ might do the trick:

koke@escher:~$ mkdir test && cd $_ && echo $_

So to expand on Jorge Bernal's answer, you can use parameter expansion, too:

wget superuser.tar.gz && tar -xzf $_ && cd ${_%.tar.gz}

which will strip off the ".tar.gz"


Not what you asked for, but maybe it's close enough for you. In bash, Meta+_ (Meta + underscore) will give you the last parameter of the previous command. So, you can do:

$ wget superuser.tar.gz
$ tar -xzf <Meta>-_
# In the above, I typed Meta-_, got superuser.tar.gz
$ cd ./ <Meta>-_<Meta>-2<Meta>-<backspace><backspace>
# In the above, I type Meta-_ to get superuser.tar.gz, then
# Meta-2 followed by Meta-backspace to delete
# two words, then backspace to delete the period.

This is easier done than explained, unfortunately.

I have my Alt key mapped to Meta (default on most terminals in linux), so it's very easy to type.

Finally, Meta+_ takes a numeric argument, so you can type Meta+2Meta+_ to get the second parameter of the previous command at the current cursor position.

  • How do you do this on OSX - seems that cmd isn't the same as meta. – Rich Bradshaw Jan 8 '10 at 14:54
  • You can choose "Use option as meta key" in terminal's preferences, and then use the option key. – Alok Jan 20 '10 at 5:27

Incidentally, you're better off using dtrx for extracting archives in one go. That way messy archives won't clutter your directories. Other alternatives: atool, 7z, unp, and e. Most of these via reddit.

  • Great suggestion, I added it to my must-have list. – Hayek Jan 23 '10 at 23:58

The last part is tricky because the folder inside the archive is probably not named the same as the archive (superuser.tar.gz), so unless the folder inside is also named superuser.tar.gz, you'll need to use basename or similar to remove the .tar.gz extension. If you know the directory name however, you could just supply it on the command line.

The rest is easy. Send Wget's output to stdout, then pipe it to tar:

wget -O - superuser.tar.gz 2> /dev/null | tar xzvf -

If you know the name of the directory inside the archive:

wget -O - superuser.tar.gz 2> /dev/null | tar xzvf - && cd superuser

This is not universal across commands obviously, but most of what you want to achieve through "referencing" can be done through piping instead.

  • Does the - tell the program its input is coming in through the pipe? – Hayek Jan 8 '10 at 10:45
  • Through stdout, yep. – John T Jan 8 '10 at 10:57
  • GNU tar reads from stdin by default, so tar xzv is enough... (This doesn't apply to BSD tar though, which makes things annoying.) – user1686 Jan 8 '10 at 12:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.