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

The "Hashbang" 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 explains that hashbang, prounounced "shabang" is a line that starts with the characters '#'! in a Bash script and that indicates how the bash file should run. For instance, the hashbang can indicate in what language a file should be interpreted. This section also includes a demonstration of how to add a hashbang is a script.

Preview
Close

Transcript from the "Hashbang" Lesson

[00:00:00]
>> Let's go and let's talk about hashbang, which is a term that I did not make up, but it's kinda fun to say out loud, hashbang. Or it's often called shebang, and I'm not making these up, that's actually what they're called. So open your gen_files again, and at the very top line, oops.

[00:00:23]
We're going to put a hash and a bang or hash an exclamation point. And then you say slash bin slash bash. And that there can be a space there, or not a space there, up to you. So what is this part up here? This first line is going to be read by bash, which is going to say, here's the thing that you need to interpret this file, right?

[00:00:53]
So in this particular case, we're saying, this is a shell script, so use bash to interpret this shell script. And the reason why this is interesting is because let's say this was a Python file. You could actually say like, this is a Python file, please interpret this as a Python file and then you wouldn't have to use Python to invoke it.

[00:01:09]
You just say, hey bash, run this file. I don't know how to invoke it and then bash using this shebang at the top would say, I use Python to run this file. Does that make sense? Right? So it's kinda giving a clue to bash. Here's how you run this file.

[00:01:25]
So in this case, we're saying to bash, use bash to interpret this. Now, why is this interesting? So I saved this I'm going to quit out of it. Let's move gen_files to gen_files but with without the.sh at the end. Okay, so now if you look in here, it has no file extension gen_files, and then I'm going to say ch mod plus x on that gen_files.

[00:02:00]
Okay, so now if you look in here you can see that it's a green, which in my LS colors means that it's invocable. Or if you look at it with the permissions, you can see here that it has write permissions, or sorry, these x permissions for everyone. Now what I can say is ./gen_files, and it still works.

[00:02:22]
Which is pretty cool cuz now it's an executable file, that thanks to that shebang, it knows how to run that file. So notice that I'm not using source, I'm not using dot, I'm not using bash. I'm just saying, run this file, and it figures out how to do that from the shebang right?

[00:02:41]
So to kind of prove my point here, we can actually do this like we can define it as a different interpreter besides bash. Let's like write a really quick node one. So I'm going to say first which node to find out where the path to the node is because you have to get a fully qualified path.

[00:02:56]
So snap in node, that's where my node is. So I'm gonna copy that and I'm gonna say vi node test with no file extension. I'm going to give it a shebang of snap bin node. And then I'm just gonna say console.log. This is running from node.js, okay? I'm gonna right click that.

[00:03:25]
And then I'm gonna say chmod + x node_test I'm going to say dot slash node test. And you can say this is running from node.js. So it actually loaded up node.js, ran it, and then work from there. So you can actually do that with any program. You can kind of say, hey, go use this to run this file.

[00:03:45]
That's what the point of this shebang. So if I left that line out, it'd be like, I don't know what any of this is, I don't know how to run this. And so it wouldn't work. Does that make sense? Cool. So that's the point of the hash, bang, shebang, whatever you want to call it.

[00:04:03]
It's a good idea if you're writing bash scripts to just always put bin bash at the top there because that's that's how that's going to work. And the other part that's really nice about using shebangs like this, is you can write a bash script, and then you can share that bash script with like your friend.

[00:04:17]
And your friend could be using zsh. They could be using fish. You can kind of guarantee that it's gonna work the way that you anticipate because even if you're running it from fish shell or from PowerShell or something like that, it's going to go out and find the correct bash to run that.

[00:04:32]
And then it's going to work the way that you anticipate it without them having to like rewrite your entire script for their shell. If that makes sense.

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