Check out a free preview of the full Complete Intro to Linux and the Command-Line course

The "Bash History" Lesson is part of the full, Complete Intro to Linux and the Command-Line course featured in this preview video. Here's what you'd learn in this lesson:

Brian continues to explore the command line interface by demonstrating how to find the history of bash commands that have been used. Useful interactions like clearing, copying and pasting, and repeating the last command are also discussed in this section.

Preview
Close

Transcript from the "Bash History" Lesson

[00:00:00]
>> Talk about bash history for a second. So, we talked about the things that you type in go into your bash history, that's what up and down is using. That's what the reverse searches using. But just so, you know where your bash history is, if I hit tail and then tail -/ bash_history, like that.

[00:00:24]
This is gonna show me the last things that I ran. That's what tail does, tail just outputs the last ten lines from a file. Your bash_history will have everything you've ever run, but I just wanna let you know where that is. So, if you go in and you delete your bash_history, it'll delete all of your history.

[00:00:41]
So, that's a good thing to do. If you've like put a password in there something like you can actually just go in and edit directly and delete the passwords and stuff like that. We'll talk more about tail later, but that's where it is, in case you're wondering. It's in your, in fact, where am I right now?

[00:00:58]
I'm in my home directory. So, if I just hit ls -a, you can see one of those is bash history, right there. Another thing to keep in mind is your bash_history is not written out to on the fly every single time. It actually buffers all of your commands in your current session and then as soon as I log out, it'll write all that stuff to bash_history.

[00:01:20]
So, it's not always writing to it you have to, you have to log out for it to write till your bash_history. Let's talk about another one. So, if I do ls -a, right? Let's do this again, ls -a, let's say, I want it for whatever reason to run it again.

[00:01:46]
You can actually put exclamation point, exclamation point and it's actually going to do the exact same command that you did last time. You'll often hear people call this bang bang. I don't know why computer scientists choose to call explanation points bang, but they do. So, for good or for worse, that's what I'm gonna call it too.

[00:02:08]
It's probably good for you to get used to hearing it. Now why is this useful in this particular case, this isn't useful. I could have typed ls -a, or I could have hit up enter. However, you can actually insert that into another command. So, the reason why I use bang bang all the time, is let's say I do like.

[00:02:28]
I do something I needed to use do pseudo, which I'll talk about pseudo in a bit. You can do pseudo bang, bang, it will actually rerun the last command with pseudo. And again, we'll talk about pseudo in a bit, but that's when I use bang, bang. That just works in bash, I don't even think that even works in zsh.

[00:02:47]
So, be aware of that. So, if you're on Mac that might not work. Yeah. So, actually went and found out why they called it bang. It turns out in the 1950s, secretarial dictation typesetting manuals in American refer to the mark as bang. And they refer to it as being from comic books, right?

[00:03:14]
Because it looks like an exclamation point, right? And I didn't know that I don't. So, comic books are the reason why you call exclamation points bang in computer science. So, there's your dumb fact for the day. Well, actually, probably not for the day, I probably have a lot more dumb facts to tell you.

[00:03:34]
So buckle up, okay? Clear, you've probably seen me type that a billion times by now, but that just clears your screen. But again, it's still up here, if you scroll up. I think you can also do so if I do like Ctrl+L, that'll also do so if it's Ctrl+N, it also does clear.

[00:03:55]
I never remember that but I can always remember clear, siren clear all the time. And last thing here, copy and paste on the command line. So, if I come in here and say something like this and I wanna copy it, obviously you can highlight all that and I hit Command C and then Command V like that all works on Macs.

[00:04:17]
But I do wanna call out on Windows, copy and paste on Windows is Ctrl+C, right? And, so Ctrl+C means something totally different on the terminal. So, you're the normal way that you would copy and paste doesn't work. So, you actually have to do Ctrl+Shift+C and Ctrl+Shift +V. And that's to disambiguate it from what Ctrl+C and Ctrl+V mean, which is something totally different in the terminal.

[00:04:45]
In fact, it's literally the next thing we're gonna talk about which is how to send signals. But just be aware if you're on Windows to do copy and paste, you have to do Ctrl+Shift+C and Ctrl +Shift+V. But if you're on Mac, Command+C and Command+V work just as normal.

[00:05:02]
Now, I say all that, be extremely careful what you copy and paste into your command line. Your command line can cause a lot of damage, [COUGH] if you're not careful. Particularly, if you're copying things off of the internet, you can copy and paste things that you don't necessarily want to happen, like send all of my passwords to someone that's going to mine Bitcoin on my computer, right?

[00:05:29]
So, you need to be really careful what you copy and paste. Actually, it's to the point where a website can detect that you're copying something off of it, and they can swap what they're putting into your palette, right? And that's really easy to do with JavaScript. So, if you're copying off of a website, make sure it's a website that you trust, like GitHub or StackOverflow or something like that.

[00:05:51]
That if you're copying off of someone's blog, don't directly paste it into your terminal, paste it into like VS Code, make sure that it's, good. And then, copy and paste it again from VS Code into your command line. And that'll just save you some time. This has actually happened to me before where an attacker has put bad stuff into my clipboard.

[00:06:15]
So, just be really, really careful with that. It's actually even possible to put a return characters that you can copy and paste something, and it'll paste it, and it won't even give you the opportunity to read it. It'll immediately run it, because they put the return character at the end, so it's pretty sophisticated.

[00:06:30]
Most emulators, I know for example, terminal.app and iTerm will say, hey, there's a return character at the end of this. Are you sure you wanted to run this? It'll ask you but not all of them will. So, all that to say, be extremely careful what you copy and paste into your terminal.

[00:06:47]
It can be catastrophic, what you put in there. And then, the last thing just as a word of advice, like when you're in copying and pasting things, I think I say this my intro to web dev course as well. Figure out what you're copying and pasting, right? Like if you're copying a super long command that does a ton of crazy stuff for you.

[00:07:07]
It's worth your time to pull apart the command to figure out what it does because obviously, it was useful to you in some capacity as you would be copying and pasting it. It's a good opportunity for you to go figure out what's useful about it, right? Just in general, like it's fine to copy and paste things, just figure out what you're copying and pasting.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now