Linux命令符號!ls的意思

Linux命令符號!ls的意思

是什麼意思 !在命令中?例如ls指令,輸入from(!ls)後的輸出是什麼?

答案1

這意味著運行歷史列表中以ls向我指出的開頭的最後一個命令。如果您上次執行了某個命令,lsb_release那麼該命令就會被執行。如果您只運行該命令,ls它會給您目錄列表。它運行以您指定的內容開頭的最後一個或最近的命令。

如果您history在系統上運行,您將看到您運行過的所有命令。如果您想要歷史記錄中的特定命令,那麼就像!#運行!1990我之前的cd.

歷史輸出範例:

 1990  cd
 1991  sync
 1992  ssh [email protected]
 1993  sync
 1994  sync
 1995  sudo apt autoremove
 1996  reboot
 1997  sudo apt remove nvidia-driver-430
 1998  sudo apt install nvidia-driver-435
 1999  sudo apt autoremove
 2000  reboot
 2001  ./intrepid_check.bsh 
 2002  ssh intrepid
 2003  ls -al
 2004  ls
 2005  ls -al
 2006  history
terrance@terrance-ubuntu:~$

如果我想運行ssh該列表中的最後一個命令,我將其輸入為

!ssh

例子:

terrance@terrance-ubuntu:~$ !ssh
ssh intrepid
Welcome to Ubuntu 18.04.3 LTS (GNU/Linux 5.0.0-29-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

 * Congrats to the Kubernetes community on 1.16 beta 1! Now available
   in MicroK8s for evaluation and testing, with upgrades to RC and GA

     snap info microk8s

 * Canonical Livepatch is enabled.
   - All available patches applied.

0 packages can be updated.
0 updates are security updates.

Your Hardware Enablement Stack (HWE) is supported until April 2023.
You have mail.
Last login: Wed Sep 25 21:38:55 2019 from 10.0.0.100
-------------------------------------------------------------- 09/25/19 21:43:36
terrance@Intrepid:~$ 

希望這可以幫助!

答案2

man bash。它部分地說:

Event Designators
   An  event  designator  is a reference to a command line entry in the history list.  Unless
   the reference is absolute, events are relative to the  current  position  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 minus n.
   !!     Refer to the previous command.  This is a synonym for `!-1'.
   !string
          Refer to the most recent command preceding the current position in the history list
          starting with string.
   !?string[?]
          Refer to the most recent command preceding the current position in the history list
          containing string.  The trailing ? may be omitted if string is followed immediately
          by a newline.
   ^string1^string2^
          Quick  substitution.   Repeat the previous command, replacing string1 with string2.
          Equivalent to ``!!:s/string1/string2/'' (see Modifiers below).
   !#     The entire command line typed so far.

相關內容