You could assign the filename to a variable first:
f=test.tar.gz; tar xvf "$f"; rm "$f"
Or use the $_
special parameter, it contains the last word of the previous command, which is often (but of course not always) the filename you've been working with:
tar xvf test.tar.gz; rm "$_"
This works with multiple commands too, as long as the filename is always the last argument to the commands (e.g. echo foo; echo $_; echo $_
prints three times foo
.)
As an aside, you may want to consider using tar ... && rm ...
, i.e. with the &&
operator instead of a semicolon. That way, the rm
will not run if the first command fails.