Reputation: 14233
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.
Upvotes: 2
Views: 216
Reputation: 1444
touch
updates file timestamp (to current time, given no arguments)
!!
is 'History expansion' operation, retrieving previous command from bash history log 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'. Brace expansion requires single word string without unescaped white spaces (it's definitely a typo in the question). 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
Upvotes: 3
Reputation: 54233
https://tiswww.case.edu/php/chet/bash/bashref.html
!! refers to the previous command. This is a synonym for ‘!-1’.
:2 refers to the second argument.
So for example :
echo "content" > foo
cp foo bar
cat !!:2
Displays the content of bar. !!:2 is the second argument of the previous command. Which one was it in your example?
Upvotes: 0