1

I need to use plural commands sequence frequently, like:

xelatex a-tex-file.tex && xelatex a-tex-file.tex

to create a latex catalog. Or:

dot -Tpdf -o a-dot-file{.pdf,.dot} && evince a-dot-file.pdf

to view a pdf file after being compiled immediately to check if I correctly modify the graph file described by dot language. And so on.

To prevent clerical error I want to use repeated command line parts(such as filename) by any possible ways, a bit like:

xelatex a-tex-file.tex && xelatex $2

and maybe

dot -Tpdf -o a-dot-file.pdf ${3%.*}.dot && evince $3

Maybe set (positional) variables or make a script can get it but I hope it can change the environment as less as possible, and I don't think it is sensible to make scripts for every command permutation, or set/unset variable on every time.

So is there any way to use repeated command line parts in just current command line more "temporarily" in various shells?

rikuri
  • 80
  • 5

1 Answers1

1

For bash, !# is the history expansion for the current command line that you've typed so far.

$ echo foobar && !#:0 !#:1
echo foobar && echo foobar
foobar
foobar

The second one (with the brace expansion) will be tricky because the history expansion happens before the brace expansion, so you'd have to do something goofy like

$ dot -Tpdf a-dot-file{.dot,.pdf} && evince !#:2:s/{.dot,.pdf}/.pdf/

and it's absolutely not worth bothering. You'd use a function for that:

dot-evince() {
    local pdf=${1/%.dot/.pdf}
    dot -Tpdf "$1" "$pdf" && evince "$pdf"
}

dot-evince a-dot-file.dot
glenn jackman
  • 238,783
  • 38
  • 220
  • 352