Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Bash and exclamation marks !!!!
darker
darker

Posted on • Edited on

     

Bash and exclamation marks !!!!

In case you didn't know, exclamation marks play an important role in the bash workflow, especially for your command history!

Example

ok... let me explain this weird thing, actually it's simple :

  • The first part of the command will take the first command item from the second most recent command you wrote, then concatenate to "add" and the second item of the most recent command,git add second_folder.

  • For the second part, it's a substitution command of "status" by "checkout" for the second command that i wrote, in reverse order on my bash history; it will take as arguments, those that i gave to my second command in the same order.
    That said, the output of this command will be:
    git checkout second_folder

Pretty NEAT right !?

So let's talk about it, the! invokes history expansion, a feature that originally appeared in the C shell, back in the days before you could count on terminals to have arrow keys. It's especially useful if you add the current command number to the prompt(PS1="\!$ ") so you can quickly look at your screen to get numbers for past commands.

So, am not going to go deep inside possibilities here but just give some cool cheat-sheets from exclamations marks when you're on your terminal !

This is a pretty short list of the cool ones :

  • !! run the latest command,

    # Ex :$apt-get update$sudo!!
  • !-3 run the third command in the reverse order from my bash history; this is also available for positive values such as the first command, from your~/.bash_history ->!1

  • !* get arguments passed to the most recent command ran,

    # Ex :$touchfilex, test4, example$chmod777!*
  • !ls to run a command from your history starting withls

With these 4 items, you can basically play/mix around with your bash history and get great stuffs like the one up there with a substitution string.

Thanks for reading, feel free to like and/or subscribe for more 🐼.

Top comments(2)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
t0ubs0n profile image
Axel Toubiou
Dev, Geek, Tech Enthusiast, 🏎 fan, 👨🏾‍💻 with 💙.I love beautiful things so I craft 👐🏾 beautiful things 😁
  • Location
    Nantes
  • Joined

Thanks for the tip. If I understood correctly "!" can be use like a cursor to navigate through previous commands used in the terminal?

CollapseExpand
 
sanixdarker profile image
darker
Backend engineer, Software developer, Electronic Geek, i like create simple tools doing amazing stuffs.

Exactly !

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Backend engineer, Software developer, Electronic Geek, i like create simple tools doing amazing stuffs.
  • Location
    NoWhere
  • Joined

More fromdarker

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp