Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
1×
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.

Autoplay

    Rerun Bash Commands with History Expansions

    Cameron NokesCameron Nokes
    bashBash

    History expansions let you interact with bash's history. For example, if you forgot to run a command with sudo, you can sudo !! to rerun the last command with it. !$ can be used to access the last argument of the last command.

    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Transcript

    Transcript

    Instructor: History expansions always begin with an exclamation mark. For example, to run the last command that we ran again, we can use double exclamation marks. I run the date commands.

    Then if I just do two exclamation marks and hit enter, you'll see it'll run it again. This is particularly useful if you're doing something that requires superuser permission but forgot to put sudo at the beginning of the command.

    For example, you can use Bash to turn your wireless on and off, use ifconfig, pass the name of the wireless interface, and say, "Down." Doing this requires that you use sudo. If I do sudo two exclamation marks, you can see it runs it again. Now it works.

    Another handy history expansion is exclamation mark and dollar sign, which refers to the last argument of the previous commands. For example, if I create a script and I want to give it execute permissions now, I can just do that. I gave that script file execute permissions. It's pretty handy. It saves some typing.

    Discuss

    Discuss