For programs that are too complex for bash, but still need to use shell commands and process command line input. Props if it's included by default in major Linux distros.
Python's the only serious candidate that fits those requirements. Node is popular recently but I don't think e.g. Ubuntu installs it by default, or even has a modern version in its apt repos.
IMHO Python makes it too hard to run shell commands & perform other Unix magic. Personally, I like perl5, perl6, and tcl in these respects, but those languages aren't especially popular now.
EDIT: also virtualenvs make my head explode. I think node/npm is currently the best in terms of dependency management.
Without the "run the example on the command line" baggage, it reduces to:
println "ls -l".execute().text
assuming you just want to spit out the results. What's nice about this is that it's ultimately just syntactic sugar over the standard Java ProcessBuilder / Process mechanism, and it's relatively easy to support more advanced cases like defining your own input, output, and error streams for the underlying process, etc.
Perl 5 is old, but reliable. Many people deride it as a write-only language or executable line noise, but you can write ugly code in any language.
Perl 5 can be terse or verbose depending on your needs. It is not "one size fits all" like Python, it fits many different sizes depending on your needs (TIMTOWTDI).
You also have tons of modules available via CPAN, so you can get a script done quickly.
Don't get me wrong: I use Python too, but some systems only have Python 2 and Python's significant white space can be problematic at times. Perl is an old, reliable camel that you can trust to get you through your problem desert quickly, frequently before the Python caravan has started out.
Perl 5.30 just came out in May. So it is still actively being maintained and improved.
Perl 5 is included by default in most Linux distros and there are Perl distributions available for all major operating systems and platforms. (See http://www.cpan.org/ports/index.html for the comprehensive list.)
Perl 6 is also Perl, but Perl 5 was the glue of the Internet with millions of lines of Perl 5 code written and still in use.
Perl 6 was a bit slow to start and has not seen wide adoption. With Python, Ruby, and now even Powershell as scripting competitors, people have more options and aren't choosing it as much.
Perl 5 was an easy choice when it was practically a standard part of LAMP stacks via mod_perl in the Apache web server. Now there are so many backend web server choices you can pretty much use any major programming language.
I don't think Perl 6 will displace Perl 5. Perl 5 may even out last Perl 6.
Really Perl 6 and Perl 5 are different languages. I think even maintainers see it as that, please correct me if I'm wrong. I believe there was a discussion about renaming Perl 6 to avoid this confusion.
This question belongs to Randall Schwarz, author of Learning Perl and presenter of Floss Weekly. His default answer to this question, Perl, has changed a bit recently, and might surprise.
This won't be popular but... NodeJS. It's easy to grab input from the CLI, "shelling out" isn't really all that bad and often I want to do some "business logic" type stuff in code really quick and I know the toolset very well. It's much faster for me to do stuff in JS even if it might be slightly slower to execute. Execution time is rarely what I'm optimizing for in one-off scripts or even things I might run on a regular basis. My ability to understand the code and make changes is valued much higher for me.
I have a side project that is using JS (typescript fwiw) for a data pipeline, a server, browser code and a tool to set up elastic search. Using the same lingo throughout the app is cool and there is even some shared libraries across the stack.
Is that project OS or on github? I’m just about in love with typescript and would love examples of the things you are talking about. I’ve been playing around with setting up an example project that uses TS and a couple of tools I use regularly so I can just use it as a starter.
#LeftPad listen I get the negatives. I’m not on HN to argue with people who are too buy letting good be the enemy of great. I’ve got work to do and NodeJS lets me do that. If you want to hate or spread FUD then that’s on you, me? I’ve got better things to do.
The title should be "What's your favorite Linux scripting language?" Presumably you don't want to talk about Powershell (Windows), Javascript (Web), or Apache Groovy (JVM).
[prhodes@whiskey pdfs]$ dnf search rexx
Last metadata expiration check: 0:29:48 ago on Sat 06 Jul 2019 10:50:28 PM EDT.
========================================================== Name & Summary Matched: rexx ==========================================================
oorexx.x86_64 : Open Object Rexx
oorexx.x86_64 : Open Object Rexx
oorexx-libs.x86_64 : Libraries for ooRexx
oorexx-libs.i686 : Libraries for ooRexx
oorexx-libs.x86_64 : Libraries for ooRexx
brexx.x86_64 : Classic Rexx Implementation
oorexx-docs.x86_64 : Documentation for ooRexx
oorexx-docs.x86_64 : Documentation for ooRexx
rexx-imc.x86_64 : IMC's interpreter for the Rexx
oorexx-devel.x86_64 : Header files and libraries for ooRexx
oorexx-devel.i686 : Header files and libraries for ooRexx
oorexx-devel.x86_64 : Header files and libraries for ooRexx
Groovy and/or Python depending on what I'm doing. If I want to do something where I know using some Java library that I'm already familiar with will help, I will definitely use Groovy.
I especially like the .execute() method in Groovy. Just create a String that represents a valid command, call .execute() on it, and use the .text property to get the output. For example:
Lua. I never really got behind importing a package for everything, so the lack luster package management/community doesn't bother me. The syntax is fairly clean (its a nice break from braces and semi colons everywhere), it's easy to extend, and it's perf under LuaJIT is hard to argue with.
Most things I need to get done can be done in bash. I know it, I am comfortable using it, and everything I log into has it. So it's what I use. Python is great too of course, but I don't really know it- I haven't had a need.
I tried learning Perl once but reverted back to Bash the first thing I needed to do something in a hurry. And, my IDE is also my interpreter. It's pretty hard to argue with. I can just take a one-liner and throw it in a file and now I can distribute it. It's that simple.
I suppose if I could write Python one-liners (maybe you can, I don't know...) and it was as universally installed and configured, I'd use it- if I needed to.
What it comes down to is this: My needs are simple, and if I can't do it in bash, then I'm probably going about it wrong.
My favourite scripting language is POSIX shell. It is also my most used language in terms of different "projects" I write with it; most of these projects are very small and focused. I feel that shell programming is very misunderstood, this is conjectural of course, but people dismiss it out of hand and move on up to something like Python usually. I have some rules of thumb: if you need to use a hash data type then move on to something else; if your LOC starts getting big then probably move on; always use ShellCheck.
Just like every language ever, it has some warts, and it has some features that make it less readable and maintainable, but POSIX shell above all others (to me) having less features actually makes it easy to avoid the pitfalls.
Of course in general I don't write massively complicated programs in POSIX shell but that's not the point, but it can take you very far, you just have to wrap your mind around how to implement certain things compared to other languages. I think the time taken to do that, in the long run will pay off. Of course if figuring it out takes too long, then move on to something else.
As for the something else Perl is a very good candidate and is influenced in certain ways by shell programming. It just makes things easier. To me Perl is as readable as Python. I say this as someone who uses Python and Node.js in the day job, so in terms of LOC they are my main used languages, it's just that POSIX shell and Perl are better suited to the tasks as stated by the question. Also they are more portable, as in, both seem to be already present on a lot of Unices (I have no idea about macOS which I never have to touch or really care about).
People definitely shouldn't overlook Groovy for this kind of scripting. Especially if you are doing anything with the JVM it has the huge advantage of allowing you to easily integrate your actual application code into your scripts and call it directly, all while using a scripting language that is more powerful, versatile and cross-platform than other obvious options such as Python.
Haskell is pretty nice for scripting, too. It's very concise, and the strong static type system gives assurance that the code will probably work before even running it.
51 comments
[ 3.3 ms ] story [ 147 ms ] threadEDIT: also virtualenvs make my head explode. I think node/npm is currently the best in terms of dependency management.
import subprocess
subprocess.run(["ls", "-l"])
# or
subprocess.run("exit 1", shell=True, check=True)
Reference: https://docs.python.org/3/library/subprocess.html
exec ls -l
from subprocess import run run("ls -la".split())
This is what I do to reduce noise
Perl 5 can be terse or verbose depending on your needs. It is not "one size fits all" like Python, it fits many different sizes depending on your needs (TIMTOWTDI).
You also have tons of modules available via CPAN, so you can get a script done quickly.
Don't get me wrong: I use Python too, but some systems only have Python 2 and Python's significant white space can be problematic at times. Perl is an old, reliable camel that you can trust to get you through your problem desert quickly, frequently before the Python caravan has started out.
Perl 5.30 just came out in May. So it is still actively being maintained and improved.
Perl 5 is included by default in most Linux distros and there are Perl distributions available for all major operating systems and platforms. (See http://www.cpan.org/ports/index.html for the comprehensive list.)
Obsolete != Trendy / popular
Perl 6 was a bit slow to start and has not seen wide adoption. With Python, Ruby, and now even Powershell as scripting competitors, people have more options and aren't choosing it as much.
Perl 5 was an easy choice when it was practically a standard part of LAMP stacks via mod_perl in the Apache web server. Now there are so many backend web server choices you can pretty much use any major programming language.
I don't think Perl 6 will displace Perl 5. Perl 5 may even out last Perl 6.
I know he has taken a liking to Dart / Flutter, but I have not seen / heard what he said.
Sadly Rails outshined plain Ruby, except in Japan.
Seriously?
#LeftPad listen I get the negatives. I’m not on HN to argue with people who are too buy letting good be the enemy of great. I’ve got work to do and NodeJS lets me do that. If you want to hate or spread FUD then that’s on you, me? I’ve got better things to do.
https://news.ycombinator.com/newsguidelines.html
I have probably written a hundred data migration scripts (call bunch of services/databases, parse/move data around, etc) and Node is an awesome fit.
Speed isn't even a concern because my script isn't going to be the bottleneck.
FWIW, Rexx is readily available on Linux.
NetRexx is also open source and readily available, which is one more option for any Rexx junkies out there.
http://netrexx.org
I mostly use ruby, but perl5 and python will do, too.
To write: escript (part of Erlang)
I tried learning Perl once but reverted back to Bash the first thing I needed to do something in a hurry. And, my IDE is also my interpreter. It's pretty hard to argue with. I can just take a one-liner and throw it in a file and now I can distribute it. It's that simple.
I suppose if I could write Python one-liners (maybe you can, I don't know...) and it was as universally installed and configured, I'd use it- if I needed to.
What it comes down to is this: My needs are simple, and if I can't do it in bash, then I'm probably going about it wrong.
Just like every language ever, it has some warts, and it has some features that make it less readable and maintainable, but POSIX shell above all others (to me) having less features actually makes it easy to avoid the pitfalls.
Of course in general I don't write massively complicated programs in POSIX shell but that's not the point, but it can take you very far, you just have to wrap your mind around how to implement certain things compared to other languages. I think the time taken to do that, in the long run will pay off. Of course if figuring it out takes too long, then move on to something else.
As for the something else Perl is a very good candidate and is influenced in certain ways by shell programming. It just makes things easier. To me Perl is as readable as Python. I say this as someone who uses Python and Node.js in the day job, so in terms of LOC they are my main used languages, it's just that POSIX shell and Perl are better suited to the tasks as stated by the question. Also they are more portable, as in, both seem to be already present on a lot of Unices (I have no idea about macOS which I never have to touch or really care about).