Arun Killu Arun Killu - 1 month ago 5
Bash Question

What does !!:2 mean in Mac or Linux?

Today I saw a command in Mac:

touch !!:2/{f1.txt, f2.txt}


I know the use of touch command but what does !!:2 does in this command. I don't have Mac and tried in Linux It is giving some weird output. If anyone could explain more expression like this it would be great.

Answer

touch updates file timestamp (to current time, given no arguments)

!! is history expansion operation, retrieving previous command in this form (two exclamation dots), alias for '!-1'

:2 is word specifier, retrieving 2nd command argument. E.g. if previous history command was ls -l /tmp, !!:2 will render to '/tmp'

{f1.txt, f2.txt} is called 'Brace expansion'. For example, foo{bar,baz} will be expanded to 'foobar foobaz'

So, let's assume we run bash command

ls -l /tmp

Now, touch !!:2/{f1.txt,f2.txt} will produce

touch /tmp/f1.txt /tmp/f2.txt