3

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?

5 Answers 5

4

$_ might do the trick:

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

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"

1

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.

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

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.

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

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.

3
  • Does the - tell the program its input is coming in through the pipe?
    – Hayek
    Jan 8, 2010 at 10:45
  • Through stdout, yep.
    – user1931
    Jan 8, 2010 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.) Jan 8, 2010 at 12:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .