Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the use of `!#` in bash?

Tags:

bash

shell

I'm not talking about the UNIX shebang meant to give the path of an interpreter. In fact, the shebang has made it quite hard to google this question successfully...

I'm talking about bash expansion:

  • !! retrieves the immediately preceding command (akin to the up arrow, but more usable)
  • !$ retrieves the last word of the last command
  • !# appears to retrieve the current line

But what're actually useful uses of !#?

  • echo !# expands to echo echo
  • echo echo !# expands to echo echo echo echo
  • ls !# expands to ls ls.

All of which seem overwhelmingly pointless. All I know about the !# command is what I've learned from using it – I'm not certain that I know its true functionality.

like image 788
Ari Sweedler Avatar asked Aug 16 '17 18:08

Ari Sweedler


People also ask

What is the use of idiom?

Definition of what's the use —used to say that it would not be useful to do something "You should talk to her." "What's the use?

What's the use of examples?

Examples help you clarify complex concepts, even in regulations. They are an ideal way to help your readers. In spoken English, when you ask for clarification of something, people often respond by giving you an example. Good examples can substitute for long explanations.

What's the use of doing?

what's the ˈuse (of doing something)? (also what ˈuse is there (in doing something)?) used for emphasizing that you think an action, etc. will not achieve anything: What's the use of worrying about the weather?

Whats the use of of?

Of is a preposition that indicates relationships between other words, such as belonging, things made of other things, things that contain other things, or a point of reckoning.


2 Answers

As mentioned comments on the question and other answer(s), you can easily find the section of man bash:

!#     The entire command line typed so far.

What isn't made explicit by the man pages or other documentation and perhaps leading you your befuddlement to a useful use for !# is that bash event designators are meant to be used in combination with the word designators and modifiers (found just below event designators on the man page). The use cases also make more sense when chaining commands together with pipes, ; or &/&&

For example:

I could use the substitution modifier to run a script foo.sh against two inputs bar and baz:

$ ./foo.sh bar; !#:s/bar/baz/

expands to form:

./foo.sh bar; ./foo.sh baz;

Or an example from the Docs (R.I.P. Docs) reproduced below shows how to use the word selector :N to select the first argument to the command as typed so far and neatly cd into the just created directory:

$ mkdir backup_download_directory && cd !#:1
mkdir backup_download_directory && cd backup_download_directory

These examples are pretty trivial, but I hope they show how !# could really save somebody some serious redundant typing while crafting powerful one-liners.

like image 119
Will Barnwell Avatar answered Oct 25 '22 19:10

Will Barnwell


!# is The entire command line typed so far. From man bash:

Event Designators
   An event designator is a reference to a command line entry in the history list.

   !      Start a history substitution, except when followed by a blank, newline, carriage return,  =  or  (
          (when the extglob shell option is enabled using the shopt builtin).
   !n     Refer to command line n.
   !-n    Refer to the current command line minus n.
   !!     Refer to the previous command.  This is a synonym for `!-1'.
   !string
          Refer to the most recent command starting with string.
   !?string[?]
          Refer  to  the  most recent command containing string.  The trailing ? may be omitted if string is
          followed immediately by a newline.
   ^string1^string2^
          Quick substitution.  Repeat the last command,  replacing  string1  with  string2.   Equivalent  to
          ``!!:s/string1/string2/'' (see Modifiers below).
   !#     The entire command line typed so far.

One example of usage:

$ mkdir foo && cd !#:1

Makes the directory foo and changes to it.

like image 41
ryanpcmcquen Avatar answered Oct 25 '22 20:10

ryanpcmcquen