Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unfamiliar shell syntax in ack-grep install script

From the ack installation page (http://betterthangrep.com/install/) there is a one-liner installation with curl:

curl http://betterthangrep.com/ack-standalone > ~/bin/ack && chmod 0755 !#:3

I understand that it's getting the file from the website and saving it to ~/bin/ack, then setting permissions, but what does that last part ( !#:3 ) do ? (I do not recognize the syntax and Googling didn't yield any helpful results)

like image 913
sampson-chen Avatar asked Oct 14 '12 18:10

sampson-chen


People also ask

What is the difference between ACK and grep in Linux?

In Linux, you can easily use the GREP command on the terminal shell. You might be surprised to know that many power Linux users have already been using the Ack command to replace the GREP command. However, both GREP and Ack command function to output the same type of results, but they have their own individual pros and cons.

How do I use ACK-grep from the command line?

The package is called ack-grep: Since the executable is also installed as ack-grep, we can tell our system to shorten this to ack with for our command line use of the tool by typing this command: Now, the tool will respond to the name ack instead of ack-grep. If you are planning on using ack on other systems, the installation method may vary.

What is grep in Linux shell script?

About grep in Linux shell script Often, programmers need to find a file that contains a specific line or a specific word in that line. On Linux, this is accessible with one exact, simple but powerful grep command - grep stands for "global regular expression print".

How to use Ack Command in Linux?

Using the Ack command on a Linux system is pretty easy and hassle-free. After installing it, you can execute Ack commands on the shell. By default, this command is not installed on the system. In Linux, the Ack command can find data from variables, files, strings, and repositories.


2 Answers

See the section called HISTORY EXPANSION in man bash, particularly the Word Designators subsection. !#:3 refers to the third word of the pipe, which is (in your example) ~/bin/ack. In order, the words of the command are curl, 0; http://betterthangrep.com/ack-standalone, 1; >, 2; ~/bin/ack, 3; &&, 4; chmod, 5; 0755, 6; !#:3, 7. That is, !#:3 is a way to repeat the filename without using a separate variable or literal text.

Regarding the question about > and whitespace, note that > is a metacharacter, which man bash defines as a “character that, when unquoted, separates words. One of the following: | & ; ( ) < > space tab”. So whitespace does not affect whether > counts as a token. But note that in the following example, the first 3 is quoted so that bash doesn't interpret it as part of a 3> redirection. When the line was entered, bash echoed the expanded line and then executed it.

$ seq '3'>bbb;cat !#:3 !#:2 ccc; head !#:3 !#:8
seq '3'>bbb;cat bbb > ccc; head bbb ccc
==> bbb <==
1
2
3

==> ccc <==
1
2
3
like image 196
James Waldby - jwpat7 Avatar answered Oct 09 '22 21:10

James Waldby - jwpat7


!# means to execute the command typed so far, but you can specify a parameter with :n. :0 would be the first word (curl), :1 the second one (http...) and so on.

like image 32
Birei Avatar answered Oct 09 '22 22:10

Birei