Loading video player…

Seeing the Shebang in Action

Resources linked in this lesson:

00:00 In the last lesson, you learned in theory what the shebang is. In this lesson, you’ll get into some practicals. To follow along properly with the rest of this course, you need a Unix-based operating system like Linux or macOS.

00:13 If you are in a Windows environment, then you need Windows Subsystem for Linux and the shell that it provides. There are links in your slides as discussed from the previous lesson that show you how to install this.

00:25 I’m currently on macOS, so I’ll be using my code editor’s shell. You can follow along with any code editor of choice. I’ll be using Visual Studio Code for the rest of this course.

00:35 In your code editor, you’ll start with creating a project folder to hold your code files. I’ll use my file explorer and call mine examples/,

00:45 and then in it create a Python script, which is a .py file. This I’ll call script.py.

00:52 So you should now have the folder and a Python script. To validate the importance of the shebang, you’ll be executing a Python script directly to see what actually happens. In this script, you won’t do much, just printing out a text in the terminal.

01:07 So in here, you can have a print() function with Hello, World. So you can say print() and in there, pass in the string “Hello, World!”.

01:18 With just this print() function, head over to your terminal and you’ll try to call the script directly to see what happens. Your shell should be able to execute scripts or text files that contain source code expressed in a high-level interpreted language like Python or JavaScript.

01:35 So if I call examples/script.py, you would expect to see the result with a printed text, right?

01:43 But no. Instead, you should see something like Z shell if you’re in macOS, or Bash, if you are in Linux, permission denied. And then the name of the script.

01:53 This is because for security reasons, files are not made executable by default because they could be harmful. But you can solve this first encountered problem by making this script executable.

02:05 And to do that, you can give the execute permission to this specified file. To see what permission state this file is in before granting the execute permission, in your terminal, or Unix-based operating systems, you can call the ls -l command on the script.

02:21 for this particular script, I see -rw-r--r--.

02:30 Unix file permissions control who can read, write, or execute a file or directory. In this group series of characters, r is for read permission, w is for write permission, and x is for execute permission.

02:44 So the permission string like - rwxr--r-- shows read, write, and execute access for the owner, and then read access only for group, and finally read access only for others.

03:02 The first character tells you the file type. If it’s a -, that’s a regular file. If it’s a d, that’s a directory. And if it’s an l, that’s a symbolic link.

03:13 And in Unix-based operating systems, to grant execute permissions to a file, you use the chmod +x command, that is chmod +x, and then the name of the script.

03:26 Now back to your terminal. To make this script executable, you’d run chmod +x and then the name of the script, which in this case is examples/script.py.

03:41 With this in place, if you check with the ls -l command, again, now you should see -rwxr-. xr-x. The x in each section signifies that this script is now executable for the owner, group, and others.

03:58 And with that in place, you can go ahead to try to execute the script again. So now call examples/script.py.

04:07 After making the script executable and trying to run the script again, you should now get a different error: syntax error near unexpected token "Hello, World!".

04:17 This is happening because your shell is assuming that your script is written in the corresponding shell language, that is Z shell in my case or Bash if that’s what you’re using.

04:27 The print() function doesn’t work the same way in Bash because it’s not a recognized Bash or Z shell command, which is what it’s expecting in your file.

04:37 The shell just executes your scripts with an assumption that it’s using the corresponding language. Now, to fix this, you know that this is a Python script, so you need to tell the shell to use the Python interpreter, and here is where the shebang shines.

04:51 So in your script at the top, you can add the shebang, which is #!/ usr/bin/python3.

05:04 Just by adding this at the top of your script, the shell should now know what interpreter to use. So if I call my script again, I get the expected result: Hello, World! printed in the terminal because now the shell knows to use my specified Python interpreter to execute my Python scripts.

Become a Member to join the conversation.