What is bang dollar (!$) in Bash?

后端 未结 3 595
离开以前
离开以前 2020-12-14 00:37

Bang dollar seems to refer to the last part of the last command line.

E.g.

$ ls -l
 .... something
$ !$
-l
bash: -l command not found
3条回答
  •  天命终不由人
    2020-12-14 01:16

    !$ can do what $_ does, except the fact that $_ does not store the value it returns (as its substitution) to history.

    Here is an example.

    With !$

    za:tmep za$ ls -lad 
    drwxr-xr-x  4 za  staff  136 Apr  6  2016 .
    za:tmep za$ !$
    -lad
    -bash: -lad: command not found
    za:tmep za$ history | tail -n 3
      660  ls -lad 
      661  -lad                     <<== history shows !$ substitution.  
      662  history | tail -n 3
    

    With $_

    za:tmep za$ ls -lad
    drwxr-xr-x  4 za  staff  136 Apr  6  2016 .
    za:tmep za$ $_
    -bash: -lad: command not found
    za:tmep za$ history | tail -n 3
      663  ls -lad
      664  $_         <<== history shows $_ and not its substitution. 
      665  history | tail -n 3
    za:tmep za$ 
    

    More options:

    !^      first argument
    !:2     second argument
    !:2-$   second to last arguments
    !:2*    second to last arguments
    !:2-    second to next to last arguments
    !:2-3   second to third arguments
    !$      last argument
    !*      all arguments
    

提交回复
热议问题