3 ms·
the ability to embed a shell command in backquotes and get its output easily Aside from that being a gigantic footgun, you can do this in Ruby, PHP, and in POS
by inferiorhuman 3y ago
the ability to embed a shell command in backquotes and get its output easily
Aside from that being a gigantic footgun, you can do this in Ruby, PHP, and in POSIX shells but not in Javascript or Python.
- last_responder 3y agoNot sure why you think python can’t do this. I do it all the time.
- jdhzzz 3y agoHow? I only know of subprocess.
- inferiorhuman 3y agoDoubtful.
- darrenf 3y agoI'm reasonably sure you don't "embed a shell command in backquotes" all the time in Python (and I don't think this is pedantry; the specific ease of this syntax was the claimed benefit, not just the ability to shell out in some way). Backticks were an alias of `repr` in Python 2.x: $ python2.7 Python 2.7.6 (default, Nov 13 2018, 12:45:42) [GCC 4.8.4] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> foo="this is foo" >>> `foo` "'this is foo'" And it went away in 3: $ python3 Python 3.11.3 (main, Apr 5 2023, 15:52:25) [GCC 12.2.1 20230201] on linux Type "help", "copyright", "credits" or "license" for more information. >>> `ls` File "<stdin>", line 1 `ls` ^ SyntaxError: invalid syntax
- ernst_klim 3y agoNever knew you can do it in Ruby too, thanks for sharing. I’ve missed that from python a lot.
- inferiorhuman 3y agoSo I firmly believe that using shell expansion in your scripts is a horrid idea no matter what the syntax. But while you're thinking about backticks and ruby: Much like a modern POSIX shell you can also use the more ergonomic %x (e.g. %x{echo true} %x(echo false). Backticks will perform string interpolation before execution just like a normal double quoted string. Backticks are simply a method, so you can redefine that on any object.