Exploring Use Cases of Shebang
00:00
In previous lessons, you’ve used the shebang to indicate a specific Python interpreter for your scripts. In your case, the Python 3 interpreter, either by specifying the absolute path to your interpreter or by using /usr/bin/env
to discover the Python interpreter to be used.
00:18 But the shebang isn’t limited to just specifying the Python 3 interpreter. It actually allows you the flexibility to use any interpreter you need for your written program.
00:28 So with the shebang, you can specify interpreters for multiple programming languages.
00:34 If you’ve got Perl on your system, here’s an example of a script that is to be run using Perl.
00:40 Or if you wanted to run some JavaScript code using NodeJS, and you have it installed and accessible in your environment, here is an example script specified to be executed using NodeJS.
00:52
You must have spotted in the examples that for the two different programming languages, Perl and JavaScript, the shebang line looks similar. Both starting with the shebang, that is, hash symbol and an exclamation mark, /usr/bin/env
.
01:08 And the main differentiating thing is just the interpreter specified, that is, the Perl and Node executable. The point here is as long as you can point your shell to the right interpreter, you’ll be able to make runnable scripts for any scripting language.
01:25
And if there are multiple arguments needed to be passed to the interpreter /usr/bin/env
command has a -S
option that is used to split the string that follows into separate arguments, which are then passed to the interpreter.
01:40
On the slide here, the -i
is an extra argument passed to the Python 3 interpreter to specify that an interactive shell is to be activated after script execution.
Become a Member to join the conversation.