-   Programming (
-   -   bash vs perl vs python (

DJOtaku 02-04-2008 09:32 PM

bash vs perl vs python
I've written my fair share of bash scripts and I've written a few python scripts/programs.

I was wondering, when is perl better than bash? So far I haven't encountered much that bash couldn't handle. The only reason for my python scripts/programs have been to learn that language, not necessarily because bash couldn't do the same thing.

And between perl and python, when is it better to use one over the other? Or are they pretty much equivalent?


chrism01 02-04-2008 10:32 PM

Well, bash is intepreted line by line as it runs and depends on calling a lot of external progs (depending on what you want to do).
You often have to use temp files as intermediate storage for result sets.
It (shell) was originally designed to talk to the system and automate cmd sequences (shell files).

Perl is more like C, it's largely self contained with a huge library of free code ( and it's compiled (sort of see ), so it runs much faster, eg about 80-90% speed of C, but easier to program (eg variable sizes are dynamic).
Syntax is similar to C. Docs:

I'll pass on Python, never seen/used it.


ghostdog74 02-04-2008 10:34 PM


Originally Posted by DJOtaku (Post 3046298)
I've written my fair share of bash scripts and I've written a few python scripts/programs.

I was wondering, when is perl better than bash?

It is better than bash only when you think it is.


And between perl and python, when is it better to use one over the other? Or are they pretty much equivalent?
i would rather you not ask this question here because it will most definitely lead to unnecessary "arguments". The best solution is to find that out for yourself. Learn Perl and Python. Then do up a piece of application using both languages to reach your own conclusion.

sundialsvcs 02-04-2008 10:53 PM

My general take...

(1) Although bash has rudimentary scripting capabilities, it is primarily meant to be "a shell." You can do a lot of things with its scripting-language, if you work at it hard enough, but that isn't what this tool is really built for.

(2) General-purpose programming languages such as Perl and Python, and to a slightly lesser extent PHP and Ruby, are tools that are "built for" general-purpose programming. Any of these can be invoked from the shell, and you can write a good command-program in any of them.

(3) Also don't overlook some lighter-weight but nevertheless very purpose-driven tools such as 'awk.' Unix/Linux environments give you a very rich library of "tools for the job."

(4) Make it your business ... it can also be informative and fun ... to study, and over time to master, as many tools and languages as you can. In other words, don't waste time quibbling over "Perl vs. Python." Instead, learn both!

:twocents: (If anyone swaggers up to the bit-bar spoiling for a fight over which one or the other is "best," tip your hat, wish them a very nice day and leave the bar immediately. "Flameboys" aren't even any good for toasting marshmallows.)

DJOtaku 02-04-2008 11:44 PM

I guess it just comes down to me not yet having any programs "un-trivial" enough to warrant writing in perl rather than bash script. For example:

Here are just a few examples of some my bash scripts.

This one I use to create animations from the PNG files created in Blender. (Without audio - more for test renders)


# Written by Eric Mesa
# requires mencoder
# Version 0.1 - created the script

#ask the user a series of questions to fill in the fields below:

echo "what is fps?"
read fps

echo "what bitrate? [recommend 3000 for smaller animations]"
read bitrate

echo "what is the title?"
read title

echo "what is genre?"
read genre

echo "what is output name?"
read outputname

mencoder "mf://*.png" -mf fps=$fps -ovc xvid -xvidencopts bitrate=$bitrate -info name="$title":artist="Eric Mesa/drop the bomb productions":copyright="2007 creative commons by-nc-sa":genre="$genre" -o $outputname.avi

So, could this be improved by converting to perl or is it too simplistic?

Most of my scripts are like that one, for example:



#Written by Eric Mesa
#requires ffmpeg
#original version copied from
#questions added by Eric

echo "what is path to file?"
read orig

echo "what is ouptut filename?"
read output_dvd

ffmpeg -i $orig -target dvd -aspect 4:3 $output_dvd.mpg
#where in the next part n is the pass number
#ffmpeg -i input.avi -target dvd -aspect 4:3 -hq -passlogfile logfile.log -pass n output.mpg

tcextract -i $output_dvd.mpg -x mpeg2 > output.m2v

tcextract -i $orig -x ac3 -a 0 > output.wav

mplex -f8 -o $output_dvd.mpg ouput.m2v output.wav

My most complex one was written by a friend of mine to help me backup my home directory:


#oldest snapshot to be deleted (4 means there are 4 kept)
B30="`ls -rd ${SNAPDIR}/snapshot* | awk -F' ' '{print NR,$1}' | awk -F' ' '{if($1>4)print$2}'`"
B0=${SNAPDIR}/snapshot.`date +%Y%m%d%H%M%S`

wall "about to begin rsync backup"
echo "Starting backup `date`"

echo "Starting rsync..."
rsync -avz --delete /home/e/ ${SNAPDIR}/snapshot.current

echo "Rotating Backups..."
#created hardlinks so there don't have to be multiply copies of the files around
cp -al ${SNAPDIR}/snapshot.current ${B0}
rm -fr ${B30}

echo "Finished Backup 'date'"
wall "rsync backup done"

would that one benefit from perl?


But in the end, yes, I've learned Python - was doing some wxPython coding tonight. And I've learned some rudimentary Perl. I just can't figure out when to use it over bash or python. In order words, I didn't want to start a flame war, but rather I'd like to know the strengths of each and use each where it's the strongest.

chrism01 02-05-2008 12:20 AM

Well, as mentioned, for automation (like you've done) bash is prob sufficient, although I'd reccommend checking exit status' ie

if [[ $? -ne 0 ]]

especially for important stuff like backups ;)

Perl/Python are more for actual data processing (like C) or where your script is non-trivial in length/complexity (for some value of ...), especially the latter.
I'm pretty sure you can find a dozen other threads on this sort of qn 'best lang..' if you search around.
Use the best lang for a given task, or the one you are most comfortable with if several options are available.
(If you need/would like a pre-written module to help in a given task, I believe ie perl modules is one of the largest around.)
YMMV as they say....

jschiwal 02-05-2008 12:57 AM

IMHO, bash is best for smaller scripts. AWK is great for handling structured data files.
Bash however only has one dimensional arrays and doesn't support floats or data structures. You can use functions but if you need more structure in the program, or if your program is large or speed is important, look at something else. Remember that for a larger program, debugging support will become more important.

On my system at least, there are python scripts used with the hal system. HPLIP also uses Python scripts. When I used Mandrake Linux, its administration scripts tended to be written in PERL. SuSE's tend to be written in bash.

The blender3d program can use Python plug-ins. Amarok uses ruby plug-ins. You wouldn't run a website in bash. You might in Perl, and some large websites do. Plone uses Python. So one criteria could be what the other's are written in in the context of what you are doing. If you are writing a service wrapper to in /etc/init.d/ you would base it on the skeleton script, be it bash or perl.

paddy3118 02-05-2008 01:45 AM

Bash is a quite capable scripting language, as you have found, but, If you start using arithmatic, using bash arrays, using array variable modifiers,... then it is usually time to put down bash and switch to a more capable language such as Python - my choice, and I have to know and use Python, Perl, TCL, Awk, and Bash.

Here's a large Bash script I wrote in Bash because I new a lot more about background job management in Bash - but, on reflection, might have been better written in Python because of later additions -

And here's why I limit my use of Perl:

- Paddy.

nc3b 02-05-2008 02:11 AM

I tend to use php for most of the stuff.. It really is easy because of the enormous support you get with it. It has support for many many libraries (including curl), there is etc.. No matter what you are trying to do, there already is a function or class for it. Don't know about the performance though.. Some say python is better in that field. Don't really know :study: Cheers:)

Zmyrgel 02-05-2008 03:40 AM

I really dislike Perl. "Perls like C"... Main reason I don't like Perl is that it's not like C :)

What really stopped me from learning more Perl was it's attitude that there's no proper way to do things, the whole "there's more than one way to do it" -attitude. Describing a way to do switch case structure for 2 pages was where I drew the line.
Why not to stick with one way that works?

Python, though, has kept my interest up since I started learning it. Everything in the language seems a logical.

I think both of them can achieve pretty much same stuff.

DJOtaku 02-05-2008 04:55 PM

Thanks for that! I think I'm much better prepared to make a decision about it.

Su-Shee 02-07-2008 06:21 AM

I use bash, if it's just a short one or two liner or I just throw it on the command line.

I use Perl for literally everything else, especially when it comes to _any_ kind of string and character handling and regular expressions which are perl's strengths - many concepts from AWK are nicely incorporated into perl and sugar-coated with additional features. I also use Perl if I need networking stuff or some GUI added.

I use Python, if I _really_ need "real" object orientation and Perl's blessed hash-OO isn't enough. As I'm starting to experiment with Perl 6, I may have no need for Python in the future.

Another choice might be Ruby - I've got no desire or need for it personally, but just to mention it...

Personally, Perl's still my favorite for many reasons starting with "community and spirit" over "CPAN" and by far not ending with "extremely well documented".

All times are GMT -5. The time now is 02:31 AM.