Will Python ever outperform C ++
When to use Bash and when to use Perl / Python / Ruby? [closed]
We've all been doing our scripts using bash so far, but I'm starting to feel a little silly about it. While we can of course do anything we want with Bash (it's pretty powerful), I wonder if we shouldn't use a proper scripting language instead (in our case, most likely Ruby).
How do you decide when to use Perl / Python / Ruby over bash for a script? I don't think an init script with Ruby makes sense, but how about a slightly longer script that adds email accounts?
Given an issue that both of you can deal with, use what you are most comfortable with. Ultimately, there are many small details and only experience can teach you how to see them.
Bash is a general purpose scripting language like Python, Ruby and Perl, but each has different strengths than the rest. Perl is great at analyzing text, Python claims to be the most elegant of the range, Bash scripts are great for "redirecting" If you know what I mean, and Ruby ... well, Ruby is special in a lot of things Von Wegen.
However, the differences between them only matter if you have adequate scripting experience. I suggest that you pick a language and push it to the limit before moving on to the next. A lot can be done in a shell script, more than most people would admit. Any language is as difficult as you want it to be. After you've written a few things, any language is "easy" for you.
Being familiar with the shell pays off quickly when you live on Linux. You might want to start with that. If you find a task in a shell script that is impossible or impractical for you to complete, use something else.
Also, remember that learning shell scripts is very easy. The real strength lies in other programs like awk, sed, tr, et al.
TL; DR - use bash just, to install a better language (if it's not already available), otherwise you are wasting unrecoverable, precious human time. If you can't do it by hand on the command line without errors, don't write with bash / shell.
It's 2015, so I would consider:
- The memory overhead for the Ruby / Python runtime compared to Bash is small (due to the shared libraries), while a non-trivial Bash script (i.e. one with more than 100 lines) probably cannot be managed anyway
- Ruby / Python starts up a bit slower, but it's likely that you're not running many full Ruby / Python processes in a tight loop 100 times per second (if you have such requirements, bash / shell is anyway too much hassle and you will probably have to fall on C / C ++)
- In Ruby / Python, almost all typical data links are faster - or at least comparable (or you need C / C ++ / Haskel / OCaml / whatever anyway).
- The actual performance / bottleneck in execution (or even productivity) will be as good as never "be a lack of bash / shell" for it is nothing more to write 'bash' and 'vi' and run code, and there is often no way to add / download or save anything else)
- Running other processes (like sed / awk / grep) is actually many times slower than calling a method on a live object in memory
- It's too easy to make mistakes in Bash / Shell than to use "real" methods, parameters, variables, and exceptions in Ruby / Python
- Agile is mainstream, while Bash has no support for it (missing unit test functions, libraries, OO, modularity, lint, introspection, logging, metaprogramming; almost impossible to revise without breaking something)
- Too many incompatibilities with other shells result in smaller environment variables completely damaging a script (and some major development tools like Puppet ignore the shebang lines and pass on or rewrite important shell variables), while Ruby / Python has a well-defined, relatively smooth migration have paths even for major version changes
- Learning a new language takes a fraction of the time debugging shell scripts due to shell-specific problems (especially variable names, no boolean values, no exceptions, etc.).
- Even startup scripts are a landmine ( especially, because they during of System starts fail can ). Given the recent security flaws in bash, it might be better to use plain C (with good libraries) - yes, C needs to be compiled, configured, etc. But even a simple shell script might need a repository, versioning, and package.
- Whatever is available with sed / awk / grep is likely already built into Ruby / Python - with no dependency or "differences" between versions of these tools on different platforms (which if it's in Your Setup works)
- Job security
- What's the use of securing a job you don't like? (unless you spend all these hours with hard to debug but trivial to create shell script errors)
I find there is no reason to use bash / shell with ruby / python installed.
And probably not even a bash script is needed to install Ruby / Python (with the exception of busybox, some system tools depend on Python / Perl being present anyway).
And every time you write a shell script, you are "practicing" just that - instead of learning something more powerful / productive.
Why do people use bash these days? Because it's a terrible habit that is difficult to break. A script is seldom done "forever" in the first few minutes - no matter how much people tend to think that way. Along with the error "It is the last mistake in this script".
Bottom line: only use bash / shell if you absolutely can forced are (e.g. busybox) because nowadays it is almost never that again "right tool for the job" is.
I use bash when my main focus is on file management. This can include moving, copying, and renaming files, using files as input to other programs, or saving the output of another program to files. I rarely write bash code that examines the contents of a file or generates output for writing to a file. I leave that to the other programs (which I can write in Perl or Python) that I start via bash.
I use Perl and Python when my main focus is reading data from files, processing that data, and writing output to files. When I use the command, Back Ticks or (in Python) the module too extensively (in Perl), I think about writing the script in bash. On the other hand, I sometimes start adding so much functionality to a Bash script that at some point it makes more sense to rewrite it in Perl / Python than to deal with the (in comparison) limited support of Bash for variable areas, functions, data structures, etc. To deal with.
I like the criteria in this blog post.
- If there are no arguments to pass, it is likely a shell script.
- If there's not much for the control logic (other than a single loop or if / else) it's likely a shell script.
- If the task is to automate command line statements, it is almost certainly a shell script.
I found this Perl versus Bash analysis useful ...
For simplicity, I'll copy an author's summary of 1) if Bash gives better results and 2) if Perl gives better results ...
When bash is better ...
- Job failure
- Commands on exit
- Process job output lines
- Here documents
- File equivalences
- File timestamp comparisons
- Tilde extension
When Perl is better ...
Perl is still fed up with most uses. Reasons I might choose Perl are (but are not limited to):
- It will be faster. Mainly because I don't have to start new processes for many of the things I want to do (base name and directory name are the most obvious examples, but in general Cut, Grep, Sort and Wc can also be eliminated).
- The handling of strings in Bash is rudimentary at best, and the whole $ IFS thing is super-clunky.
- Conditions in shell scripts can be incorrect.
- Quoting in shell scripts can be a nightmare.
- The bash case statement leaves a lot to be desired beyond simple cases (NPI).
- Suck arrays in bash. Hashes in bash (assuming your bash is new enough to even have them) suck even harder.
- As soon as the processing of files or the issuing of commands goes beyond the simple case listed above, Perl begins to smoke heavily.
So it's not that Bash will take control of Perl anytime soon. But after all these years I still find that a simple shell script is sometimes easier than a simple Perl script. As I said, I welcome all attempts to convince myself of something else. But there's nothing wrong with having a few different tools in your toolbox either.
In my experience, Bash versus Python is a compromise between development time and flexibility. A rudimentary solution to a problem can usually be created more quickly in a bash script than in a python script.
In Python, you will usually think more about the structure of your solution than about the corresponding bash script. Python is more meaningful than a bash script, so it can be scaled and changed better over time. It also generally remains more readable.
Bash is closer to the filesystem and is great for first drafting solutions to problems that are NOT well defined. Because of this, a bash script can be a great first choice to prototype to fully port to Python once the problem is better understood.
Bash is a Unix shell that contains a scripting language. It's more of an instruction processor. You control the way you execute commands, you actually execute them.
Perl / Ruby / Python are general purpose languages.
If you want a shell script, use bash
When you have a more complex task or you don't want to be in touch with the shell. Use python etc.
I would actually never compare these languages. Python etc. are portable. You can do it anywhere. Bash is only for Unix.
Python etc has tons of reusable libraries that solve millions of tasks.
It's almost the same if you ask. "When to Use Paint and When to Use Photoshop"
I would use Ruby again to process emails as there are many reusable libraries out there.
But the best way would be to combine bash and ruby. That would be correct. When you create an email processing script in Ruby and Bash, this Ruby script is called and other commands are executed.
So if you need a command processor, use bash. You execute and control Unix commands.
UPDATE after 7 years (March 2019)
While the main part of my answer hasn't changed, I want to point it out.
Bash is also a powerful scripting language. For word processing, it might be a perfectly legitimate choice.
Please read mkaito's comments below. They are all completely true.
Shell scripts like bash, ksh, zsh, sh and fish are known to be surprising when compared to general high-level languages like Ruby, Python or Perl. While a shell script might start out as a shorter file than its general-purpose script, the surprises lead to a lot of defensive code, such as to enable strict modes.
For example, most shell languages continue executing lines in a shell script even if one of the commands fails. In contrast, a general-purpose language fails immediately and often with heavy load on the first mistake, resulting in safer and more predictable behavior in scripts with even low complexity.
Very biased article.
I don't find bash that difficult to debug. Python is often too rigid, whereas bash can be very creative. If you're a good out-of-the-box thinker, you'll like bash.
I'm running bash scripts with millions of DNA sequencing reads on thousands of files. And contrary to what everyone is saying, the same versions of the scripts in C ++ don't really run much faster (they'll be disconnected in a matter of minutes).
I don't think Bash, like Perl, is particularly user-friendly or easy to read. This scares people because most people aren't great abstract thinkers. But brighter, more creative programmers love it and use it a lot. When you know yourself and you know you have a brain, don't let that put you off. If you're a basic thinker, you might be sticking to something like Python. To each his own.
From the "Lama Book"
- Randal L. Schwartz, Tom Phoenix and Brian D Foy, Learning Perl (Sebastopol, CA: O'Reilly, 2011), chap. 1.2 "What does Perl stand for?" , §§ "Why didn't Larry [the creator of Perl] just use a different language?", P. 5:
Perl tries to bridge the gap between low-level programming (like in C or C ++ or Assembly) and high-level programming (like "shell" programming [e.g.]). Low-level programming is usually hard to write and ugly, but fast and unlimited; It's hard to beat a well-written, low-level program on any given machine. And there's not much you can do about that. High-level programming at the other extreme is usually slow, tough, ugly, and limited. There are many things that shell or batch programming cannot do at all if there is no command on your system that provides the functionality you need. Perl is simple, almost unlimited, mostly fast, and kind of ugly.
As a rule of thumb, use the simplest language that is good enough for your particular task. And its peculiarities are only really useful to that extent.
And as far as readability goes, bash is awful if your programming style is awful. If you just throw the code in there, it will go dark.
However, if you edit the code in the shortest functions split up and name things in a way that is easy to understand, this is the clearest language you can find. Because it's very close.
As an example, this is my newest code in Bash. Notice how easy it is to understand and type:
- What is monobasic acid
- Blockchain is not hackable
- What is a PCI slot
- How was Paraguay founded
- Communication is the most useful class
- Why are people divided into religions
- How can we add two bcd numbers
- What is the purpose of compiling code
- 918Kiss has an online top up
- What is a web driver
- People still label others with accents
- Which US election had the most voters?
- What is the national dish of Albania
- Why are sitcoms not so popular
- How effective are pregnancy kits
- Is canvas suitable for badminton
- There are 22 Indian languages
- What is a suborbital shell
- What is media studies
- PPP effect on the Indian economy
- What are the best home lubricants
- What is terraforming the planet
- How can I turn off AirPlay mirroring