Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does it mean in linux scripts? #!/usr/bin/python -tt

I know that in the begining of .sh bash scripts is

#!/bin/bash

which points to the command interpeter executable.

But during watching Google Python Class http://www.youtube.com/watch?v=tKTZoB2Vjuk I noticed that for python they use

#!/usr/bin/python -tt

. Surfing the Internet I also have found such styles of this notation:

#!/usr/local/bin/python

and even

#!/usr/bin/env python

.

So, I'm new with Python and I'm ordinary Linux user and I have a few questions about this "magic" line:

  1. First of all, what is the right form of this line? and why?
  2. What does -tt key means in #!/usr/bin/python -tt ?
  3. What program is parsing this line in Linux?
  4. What syntax of this line for any script?
  5. Why this line is so necessary if each file have it's extension?
  6. And what about that in each computer interpreter for some kind of scripts will be stored in different place than in another? And script couldn't be run.

It's really interesting to me. What's this line? Why this line? How to write this line? Why in such a way?...

like image 444
Abzac Avatar asked Jan 29 '12 02:01

Abzac


People also ask

What does $? Mean in shell script?

$? = was last command successful. Answer is 0 which means 'yes'. Follow this answer to receive notifications.

What is $? In Linux?

The $? variable represents the exit status of the previous command. Exit status is a numerical value returned by every command upon its completion. As a rule, most commands return an exit status of 0 if they were successful, and 1 if they were unsuccessful.

What does ${} mean in Bash?

$() means: "first evaluate this, and then evaluate the rest of the line". Ex : echo $(pwd)/myFile.txt. will be interpreted as echo /my/path/myFile.txt. On the other hand ${} expands a variable.


3 Answers

Question #1) The line is called a shebang, and there's no right form that works universally. e.g.

#!python
#!/usr/bin/python
#!/usr/local/bin/python
#!/usr/bin/python -t

are all valid/acceptable forms, but may not work on all systems:

#!python will work only if the python executable is somewhere in your shell's PATH

#!/usr/bin/python only works if the python binary is actually in /usr/bin

#!/usr/local/bin/python also only works if python is in /usr/local/bin

Question #2)

#!/usr/bin/python -tt is passing the -tt option to python, as if you'd done:

$ python -t somescript.py

at the shell prompt. You can pass arbitary command line arguments to the interpreter on the shebang line.

Question #3)

The line is interpreted by the OS kernel and the shell you're currently using. The stuff after the #! simply tells the OS which program should be fired up to "execute" the rest of the script.

Question #4)

The script syntax depends on the language you're using. E.g. a PHP shell script must take the form of

#!/usr/bin/php
<?php
  ... php code here ...

A #!/usr/bin/perl perl script must use Perl syntax, etc... If you put PHP code with a Perl shebang, you'll just have Perl barf up the script with syntax errors, as PHP code is NOT perl code

Question #5)

Shebangs are for Unix systems, where file extensions were never really used to identify file types to the OS. A .c file was understood to be a C language source code file, but that's merely a convention. You could put a Bash shell script into a .c file, make it executable, and with the #!/bin/bash shebang, it would execute as a Bash script.

Determining executable types by file extension is more of a Windows thing.

Question #6)

That goes back the stuff in question #1 - if the shebang claims the interpreter is at some OTHER path than where it is, this particular script can't be executed until the shebang is fixed, or the interpreter is moved. Shebangs are very handy, but not infallible.

Thankfully, most interpreters are installed in fairly standard locations these days, so it'd be somewhat unusual to find (say) Perl installed at /some/wonky/weird/path instead of /usr/bin

like image 179
Marc B Avatar answered Nov 10 '22 00:11

Marc B


From the manpage:

-t Issue a warning when a source file mixes tabs and spaces for indentation in a way that makes it depend on the worth of a tab expressed in spaces. Issue an error when the option is given twice.

  1. The right form of the line is the one you want to use.
  2. It's the interpreter that reads this line known as shebang. If you write a python script with first line as "#!/usr/bin/python" & invoke it using bash, it's the /bin/sh interpreter that reads first line and starts the proper interpreter.
  3. It's a shebang. The syntax of feature consists of the character sequence #!, i.e. the number sign and an exclamation point character
  4. File extensions are not relevant in linux generally. You can have a python script that doesn't have a .py extension.

For ex.

shadyabhi@archlinux ~ $ cat a 
print "Hello World" 
shadyabhi@archlinux ~ $ python2 a 
Hello World 
shadyabhi@archlinux ~ $

Even the shebangs are only necessary if you want to start a script using $./script as in this case you didn't mention the interpreter you want to use.

like image 27
Abhijeet Rastogi Avatar answered Nov 09 '22 22:11

Abhijeet Rastogi


  1. #!/usr/bin/env python
  2. issue errors about inconsistent tab usage
  3. Kernel
  4. #!/path_to_the_interpreter or /usr/bin/env
  5. *nix does not check extensinon at all(except some DE could do that)
  6. This is why you should use #!/usr/bin/env

More info at wiki

like image 40
0xbadc0de Avatar answered Nov 09 '22 22:11

0xbadc0de