2

What is the meaning of ! in a command? For example ls command, what is the output after put in from of it (!ls)?

| improve this question | | | | |
3

It means to run the last command in your history list that begins with ls as it was pointed out to me. If you last ran a command of lsb_release then that would be ran. If you ran just the command ls it would give you the directory listing. It runs the last or most recent command that starts with whatever you specify.

If you run history on your system you will see all the commands that you have ran. And if you wanted a specific command from your history it would be !# like !1990 would run my previous command of cd.

Example history output:

 1990  cd
 1991  sync
 1992  ssh pi@10.0.0.201
 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:~$

And if I wanted to run the last ssh command on that list I type it in as

!ssh

Example:

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:~$ 

Hope this helps!

| improve this answer | | | | |
  • 3
    No, it doesn’t run the last ls command, it runs the last command line beginning with “ls” – try for example lsb_release -d followed by !ls. – dessert Sep 26 '19 at 4:01
  • 1
    @dessert Fine..... You can have that one! =) – Terrance Sep 26 '19 at 4:03
  • I don't get the meaning of it. Can you explain more about it. What do you mean by it runs the last command line beginning with "ls"? The example that I get is, firstly it runs "ls" then it shows the stuffs in that directory, then it runs "!ls" then it shows more stuffs than preview. What actually happens? – Alex Phang Sep 26 '19 at 9:06
  • 1
    @AlexPhang That's mean. The last command from .bash_history. When i tried !ll command, it run the last command of history and it executed. If i tried !ls command, it appears event not found cause i never use ls command. As Terrance's answer, you can check the command list from history to see what the last command of pointer your ran. – abu-ahmed al-khatiri Sep 26 '19 at 10:17
0

Read man bash. It says, in part:

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.
| improve this answer | | | | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.