views:

2283

answers:

21

All my scripting is done in Perl, I can execute one liners at the command line, and Perl regex seems way easier. Is there anything I can do in BASH that I can't do in Perl?

I just don't feel like a true hacker unless I spend the time to delve in BASH and start using Sed and Awk as well.

Is it worth it, or am I just asking for pain and frustration?

+4  A: 

I used Perl as my primary scripting language for a number of years and eventually started to pick up bits of bash, sed, and awk and I think they're all worth knowing. Essentially anything you can do in Perl, you can probably do in sed, awk, or bash, and with enough fancy codework, the inverse is probably also true. That said, there are many occasions where sed or awk or bash provides a more elegant solution to a problem. Perl has obviously borrowed heavily from those languages and is incredibly versatile, but those tools persist because they're still simple and useful in a lot of instances.

theraccoonbear
+38  A: 

Bash epitomises the spirit of UNIX -- combining multiple simple tools to solve a more complex problem, usually using pipes to direct the output of one command to the input of the next. This is a style of programming that I think it's worth knowing; too many people write a whole program to achieve something that can be done by combining a few existing tools on the command line.

So yes, learn bash. Even if you don't end up using it much yourself, you'll also be able to read the many bash scripts that are key parts of most UNIX/Linux systems.

TimB
While I totally agree with the points you make, I guess I think of "learning bash" as learning to write scripts with some amount of logic and flow control (e.g. ifs and loops) and not just daisy chaining other tools's IO together.
theraccoonbear
But you may need logic and flow control to chain things together, e.g. to parse the output of a command.
Mark Baker
+6  A: 

If you feel comfortable to fire up Perl for any simple script you need, you should just stay with Perl. You thereby save your brain's memory from some severe fragmentation with redundant information.

To put it straight: Using sed, awk and friends is fine, but you will find you can do the same job in Perl, in a cleaner way, which is more consistent to what you know already. BASH and the Unix toolchain are sometimes more rapid-development friendly than Perl, but on the other hand, it is full with strange or at least uncommon originalities. Learning & keeping them is quite time consuming and IMHO not worth it if you already have a similiarly-powerfull tool like Perl, which could even be seen as a successor.

ypnos
+35  A: 

If you're that comfortable in Perl, then keep using Perl.

Bash is handy to know if you're going to be editing existing scripts (e.g. stuff in /etc/init.d), and you'll probably find you can read it easily enough if you know Perl.

If you're the sort of person that likes learning new languages, get stuck in to bash, awk, sed etc. If you're concerned about pain and frustration, stick with what you know.

If you want to feel like a hacker, learn Lisp.

Matt Curtis
+7  A: 

No... if you know how to code in Perl/Python/Ruby don't bother learning bash.

DO learn how to use the UNIX command line though - i.e. pipes, redirects, & useful commands.

ceretullis
But to truly get the power of the command line you do need to know some bash. For instance, loops, short circuit logic operators, etc.
mpeters
If an activity requires a loop or control structures... chances are I'll be doing it again in the future... which means creating a script... if you already know perl/python/ruby... then it seems like a waste to learn bash ;(
ceretullis
ceretullis, no, that ain't right. There are a million one-off thnigs I find myself doing that may involve loops or whatever. Yeah, you could write a script, but you'd never use it again. I find you to be wrong.
smcameron
@smcameron: you can use scripting languages in interactive mode for the one-offs, no need to make and keep a scripts.
ceretullis
+6  A: 

You shouldn't feel that you have to learn something just because other people use it. I've been using unix since 1988 and have probably written less than 1000 lines of shell script.

All I use bash (ksh) for is as a shell - the scripting side is not useful - simple stuff is done with awk and pipes, and complex stuff requires C. Mostly awk though.

I can't think of a compelling reason to learn how to write bash scripts - beyond the basics.

The beauty and power of unix is that we can use whatever we are comfortable with and achieve about the same results.

Richard Harrison
I disagree with this post. Learning shell scripting will get you much further with Unix than anything else. If you can script your shell then you'll get more done with it. I often write a lot of 1-5 line 'scripts' inline on the shell command line to get stuff done real fast.
Adam Hawes
"complex stuff requires C?" Have you seen Perl? I have not used C for sysadmin tasks (complex or otherwise) since learning Perl.
JoelFan
By "complex stuff" I really mean what I can't do in AWK. I'm using perl more since learning it.
Richard Harrison
@Adam Hawes - you're free to disagree; but don't miss the final point which is that "we can use whatever we are comfortable with".
Richard Harrison
+13  A: 

You need to learn to read it.

You don't have to learn to write it.

But your sysadmin friends will look at you funny when you write perl wrappers for cron jobs.

The thing I find myself using bash most frequently for is something like this:

for serv in `cat server_names.txt`; do 
   ssh $serv 'some command'; 
done;

Sure, I could do it in Perl... but that just seems a bit overkill.

SquareCog
I'm a very proficient unix administrator... any cron job that takes more than a second to think of as shell commands gets done in perl.
ceretullis
Your for loop is better written as: for serv in $(<server_names.txt)
Roberto Bonvallet
@Roberto: $(<file) is not portable, and many modern shells do not support it. The for loop is best written: for serv in $(cat server_names.txt), but a while loop is better: while read -r serv; do ...; done < server_names.txt.
William Pursell
+1  A: 

I know both Perl and Bash. I use them both at work and home.

Yes, you should definitely learn Bash. You will then be able to use the best tool for the job. Yes, everything you can do with Bash can be done with Perl but that doesn't mean that you should.

Mr. Muskrat
+1  A: 

bash and Perl do the same job in slightly different ways. Perl is a little more general purpose and powerful. If you're comfortable using Perl than, sure, go ahead and use it.

My one caveat is: think about the guy who will have to maintain your code. Will he be as fluent in Perl as you?

Personally, I hate both of them. ;-P

dysfunctor
I'm not sure why this was down-voted, seems a sensible reply to me.
Mark Baker
I was overly-harsh on Perl. There's a lot you can do in Perl that you can't do in bash. I still hate them, though. :)
dysfunctor
+6  A: 

Learn bash. A good programmer has a number of strings to their bow and shell scripting is fundamental to any good UNIX developer.

Sure you can do many things in perl, which I love, but half the time the UNIX commands have already done the work for you. The combination of UNIX commands in a pipeline is the true secret of shell hacking. You have to do a lot of code to equal something like:

find | grep -l | xargs perl -e "xxx"

At the very least learn how to use then while, case and test commands, together with the variable operators in ${var:xxx} constructs.

Greg Cottman
bash != unix commands -- find | grep -l | xargs perl -e "xxx" works in ksh, in csh, in zsh, ... are there any unix shells that don't support pipes and redirects?
ceretullis
+5  A: 

Things like bash and awk still represent the Unix spirit of little languages:

  • bash is a language just for executing and connecting programs and it is extremely suitable for that
  • awk is a language just for processing records of data and it is extremely suitable for that

While perl borrows heavily from these two, it is designed as a general-purpose language. Sure, you can do these tasks in perl, and you might prefer that because you are familiar with its syntax.

But once you become familiar with bash - which is not so difficult, the language is much smaller than perl - it will become easier and certainly more economical to use the more specific tool.

I learned perl for web development and I used to use it for almost everything. Now, I find that most jobs are either simple enough for bash and awk, or difficult enough to justify a compiled language.

Bruno De Fraine
+5  A: 

Of course, as it's the default shell in most of the Linux distros.

But one more reason: what if Perl is not available? Not so long ago I had switched to new job, and although I know some bit of Python and Ruby and a few Perl, but they were not available on the live system, where I had to parse log files, create backups, and scrape data. I had to learn Bash (and awk), and they worth it.

Zsolt Botykai
This is not so uncommon. Many sysadmins remove anything that is not used by the running apps. Try asking a sysadmin in some foreign country to install Python so you can set up a script to do some cp's and rm's
Ubersoldat
That is totally unreasonable to remove Perl. Come on!
JoelFan
+2  A: 

I'm pretty familiar with the unix command line, and all the utilities (sed, awk, grep, find..) available. My general rule of thumb is if it is a one-liner, or a for loop to catch a bunch of files and do just a few operations to them, I use bash/tcsh/zsh, else I use Python. I found awk a pain to use, and anytime things needed more than the most basic awk usage, or something script-like, it is just simpler to use a real language.

I used Perl for a long time, and switched pretty exclusively to Python. If you know Perl, use it. This is the sort of thing it was made for.

MattG
+1  A: 

don't learn 'bash', learn sh/ksh/pdksh, its the basis of bash and knowing the basics of it instead of all the bad practices (bashisms) learning bash would teach you, would let you use any unix/posix system with ksh or sh (or bash). Even Services for Unix from Microsoft comes with a korn shell.

MkV
A: 

There is no correct and definitive answer to your question. It depends on what you are going to do. If it is to maintain some sort of code then, learning bash, awk and sed makes sense. If it is just for personal pleasure that is also acceptable.

However if it is to maintain a script that nobody can maintain, because nobody knows bash,awk and sed then it does not make sense to learn the languages and if you can rewrite the script with proper testing and reproduce it with no bugs then learning a new language does not make sense, with regards to your time scales.

Knowing more than on scripting language works well since in most cases people are not too familiar with perl, in which case they would most likely know at least bash. Also if you are on a team where most people know bash,awk,sed then it would make sense to learn those languages.

I believe that learning something new that you can get to use in your daily life as a developer not only adds value to your skill set but also helps keeping the mind sharp

biosFF
A: 

There are indeed lots of "yes" and "no".

I would say that this is because it is useful to learn some basic bash/zsh/tcsh/whatever (pipes, loops, redirection of streams,...).

But I would only learn the details of any shell language in case it is necessary to do so (which should be very rare). Shell languages are notoriously painful to use in many situations that would be easily tackled with Perl or Python, which are arguably more powerful (fewer lines and simpler code for the same result).

EOL
A: 

http://lindesk.com/2008/05/shell-script-language-use-perl-not-bash/

Terrible advice. At least subjective.
MattBianco
+1  A: 
William Pursell
A: 

A couple of points:

  • Knowing bash helps you really learn the crannies of the Unix/Linux command toolset, so it can be invaluable to a sysadmin.
  • Bash is everywhere and used in most everything from init scripts to enterprise application glue (look at some of the netbackup toolchain sometime).
  • If you need to troubleshoot someone else's (or some other product's bash script), you'll be happy that you know bash.
  • If perl isn't available, you will be happy that you know bash.
  • And finally, sometimes bash is just faster for those quick and dirty jobs of the I need this in 5 minutes variety. You can knock out a pipeline of commands and a simple loop in no time.

That's not to say you shouldn't learn perl, ruby, and python either. Bash is definitely an "and" skill. You should learn bash and something else (or multiple something elses).

Jim
A: 

There's this really nice book called "Minimal Perl" which has a lot of tricks for ditching bash. I really like to use both since I can't handle sed and awk. So if any of my scripts gets to messy, I know I have to use Perl or Python. Also, I find Perl to be much much much faster when working with files. But using Perl to send some files over SSH or to do some ifs and fors, is an overkill.

OTOH, Bash (or any other Unix Shell for that matter) suck on doing simple/complex math and are really hard to debug. bash -s foo.sh is not enough!

Ubersoldat
A: 

As many people say before, you don't need to learn Bash but you you would like to know the basics. But the basic of the shell (sh mainly because is in all systems and very minimal). So I will learn how to do a loop, what $() is in bash and its new magic, how to capture the arguments of the command line ($0, $1 etc), how the quotation works in Bash (and in the rest of the shells http://www.grymoire.com/Unix/Quote.html). But at least my advise is that if you don't need it don't spent to much time learning Bash but read about it to know where the answers are when you need it. And not only Bash but also Cshell or similar, in order to at least know how to set variables, conditional statements, alias and loops for your configuration files. Any thing more complex, you can do it with Perl but not because it is better (or not) but because you already know it and usually you would not note the difference (unless your perl-lib is really huge and you call your oneliner zillions of times).

Also as a Perl programmer I have invested some time learning to do Perl one liners efficiently, to pipe them in the shell, and how they substitute unix commands whit it. I will recommend the book: http://minimalperl.com/

Pablo Marin-Garcia