by thrdbndndn · 2y ago · 0 comments
I use Windows most of time. Like the author, I have bunch of CLI scripts (in Python mainly) which I put into my ~/bin/ equivalent.
After setting python.exe as the default program for `.py` extension, and adding `.py` to `%pathext%`, I can now run my ~/bin/hello.py script at any path by just type `hello`, which I use hundreds of time a day.
I now use Linux more and more (still a newbie) but I never get it to work similarly here.
Firstly, Linux seems to have no concept of "associated program", so you can never "just" call .py file, and let the shell to know to use python to execute it. Sure, you can chmod +x to the script, but then you have to add a shebang line directly to the script itself, which I always feel uncomfortable since it's hard-coded (what if in future I don't want to execute my .py script with `/usr/bin/python` but `/usr/bin/nohtyp`?).
Furthermore, I failed to find any way to omit `.py` part when calling my script.
Again, none of the above is to question the design of the Linux -- I know it comes with lots of advantages.
But I really, really just want to run `hello` to call a `hello.py` script that is in my $PATH.
> But I really, really just want to run `hello` to call a `hello.py` script that is in my $PATH.
On Linux I'd say the shebang is still the right tool for this. If you want a lightweight approach, just have a `my_python` symlink in your path, then your shebang can be `/usr/bin/env my_python` (or heck just `/foo/bar/baz/my_python`, /usr/bin/env is already an abstraction).
If you want a more principled approach, look at the `update-alternatives` tool, which provides this sort of abstraction in a more general way: https://linuxconfig.org/how-to-set-default-programs-using-up...
Isn't that path and the behavior of the binary defined by POSIX though? I thought it's as stable as you can get.
That's why it's usually recommended that you use /use/bin/env bash vs /bin/bash in the shebang, as the latter isn't defined by POSIX
I don't see anything about the path being defined. Certainly possible I missed it, though.
I'm not able to check right now but I vaguely recall that I've used a system in the past with env in a location other than /usr/bin.
On Linux (really, all platforms other than Windows), file extensions are much less of a thing; executables of any kind have no extension just the +x flag (among other things, this means you can rewrite them in another language without breaking anything).
The .py extension is only relevant for modules meant to be imported; for scripts being run, if you really need to know, you are supposed to look at the shebang (usually #!/usr/bin/env python for externally-distributed scripts; this gets overwritten to #!/usr/bin/python or whatever for scripts packaged by the distro itself).
Note also that, while shebangs don't support multiple arguments, the GNU version of `env` supports a `-S` argument that can emulate them (the argument length problem remains though).
I'm not saying you're wrong, but let's be clear about what these are. I would point out that Linux inherited some, but not all of its naming conventions from Unix (as did macOS), but at least here, that is a secondary concern.
Carry on...
I can't think of a downside to the shebang. If you really wanted to run the script with a different interpreter, just specify it. "nohtyp hello" or whatever.
If that still bothers you too much, you could define an alias in your shell startup. For example, in bash, you might do:
alias hello="python3 /path/to/hello.py"
If you were so inspired, you could even write a short script to automatically create such aliases for the contents of a directory you specify.
There is, but not in the shell syntax. It's an application concern normally delegated to the desktop/GUI.
For shell scripts, the executable is usually declared in the script itself, by adding a Shebang and making the file executable. Think of the Shebang like a file extension of sorts.
If
chmod +x ./malware.py
./malware.py
does not work, check the path the Shebang points to.That being said, as long as an interpreter can execute the scripts as regular argument, you should be able to get this behavior also for xdg-open:
xdg-open malware.py
if you really want to do that by default.This should be equivalent to double-clicking file in the default file manager, IIRC (am on Mac now).
I had an alias "xop <file>" when using Linux as my primary desktop OS.
But I only used this for data files (images, documents etc) where the default already works.
Wouldn't recommend setting an interpreter as default for executable scripts.
You might want to not execute scripts by default, instead opening them in an editor for example.
xdg-open is a Gnome thing I think, but that doesn't mean it's unavailable for other desktops. I know it from Xubuntu (so Xfce).
So I'd really advise against that, But if you want to execute all python files by default in any GUI context, too you could set this kind of default there
'man xdg-open' might help, or maybe you could even select a specific Python executable as the default for .py files after double-clicking in the File Manager.
Again, bad advice
It won’t directly help reach your goal, but it is semi hard-coded. The ‘correct’ (but see https://unix.stackexchange.com/a/29620 for some caveats) way to write a shebang line is
#!/usr/bin/env python
That will make it run the first python in your path.> what if in future I don't want to execute my .py script with `/usr/bin/python` but `/usr/bin/nohtyp`?).
You could create a symlink called python to /usr/bin/nohtyp on your system in a directory that’s searched before /usr/bin (e.g. by adding ~/myCommandPreferences to the front of your PATH)
Might be you could use binfmt_misc for that.
https://www.kernel.org/doc/html/latest/admin-guide/binfmt-mi...
https://blog.cloudflare.com/using-go-as-a-scripting-language...
I keep all my scripts in ~/git/$Project and symlink them into ~/bin and I've added ~/bin to the end of my path.
- The shebang is only specially interpreted by the Linux loader, i.e. when executing the file directly.
- You can still run it with any other interpreter in the standard way: `nohtyp ~/bin/hello`. Python comments start with `#`, so the shebang does nothing with programs expecting Python code.
- This situation (a script without an extension) is common on Linux, so Linux-aware editors understand the shebang to indicate a file type. At least, vim understands this and automatically detects a python file type without the .py extension.
I get your wish of Windows-like behaviour, and even if you might be able to conspire to have Linux behave the way you want, it's certainly not how people expect it to work, so prefer the above scheme for any software you send to others. :)
/usr/bin/hello:
#!/usr/bin/bash
python3 /usr/bin/hello.py
/usr/bin/hello.py: print("Hello, world!")
Console: $ chmod +x /usr/bin/hello
$ hello
Hello, World!Sublime:
/usr/bin/subl:
#!/bin/sh
exec /opt/sublime_text/sublime_text "$@"
VSCODE: /usr/bin/code:
...
# Launch
exec /opt/visual-studio-code/bin/code "$@" $CODE_USER_FLAGSI believe the elegant solution to this is update-alternatives, which lets you tell the system which actual program to call. Maybe look into update-alternatives, I haven't looked into this much but it seems like it might interest you particularly. That's the closest equivalent to file association for the UNIX shell I would guess.
You could also have a specific folder that you control in your PATH that symlinks to the Python you want to use.
This handles the default, but you can still call your script with the program you want if you ever wish to bypass that.
Use an alias which you set up in your initialization scripts. You alias "hello" to "python3 /yada/yada/hello.py" which is essentially what Windows is doing for you behind the scenes.
you're thinking too much, people have had that shebang for 20 years without any problems
> But I really, really just want to run `hello` to call a `hello.py` script that is in my $PATH.
I don't really understand why you're so adamant about this, either make a python "hello" script with a shebang or just tab complete hell<tab> which you should do with most commands anyway so the .py doesn't matter
another option would be to alias but you'd have to do that manually for every frequent script you need
from a google search, https://stackoverflow.com/a/19305076
I'm not at the computer now to test though
I used to think this as well, but I've since come around to the opposite view. Having it as a "requirement" for what's likely the most popular cli execution strategy enforces a (somewhat disorganised but still useful) defacto standard across all scripts. I can open up any repo/gist/pastebin in the world & chances are if it's intended to be run, it'll contain this handy little statement of intent on the first line. I might not actually run (env-dependent) but I'm sure I can make it.
On the env-sensitivity though, if e.g. you're running nohtyp, as another commenter mentioned, /usr/bin/env has that covered.
That’s an interesting feature for a shell to have. Thanks!
Linux being of Unix ancestry which had no such concept as a file extension. It was the responsibility of the application or kernel to discern what type a file was. Typically by the first few bytes of a file and handle it appropriately.
I personally am a fan of the Unix way but I can see why some might prefer the DOS convention.
Whether that is good or bad is left as an exercise to the reader.
You still have to say '.py' at the end, though.
To remove the .py just rename the file to “hello”, or keep “hello.py” and create a symlink or a shell alias called “hello” that points to it.