Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Sorry, what do you mean to imply here... that Bash is difficult?


Is it even up for debate, that bash is hard to get right?


Bash isn't particularly hard. Unfortunately, it carries a significant amount of historical baggage that make things very confusing. Many examples and existing bash scripts usew these older feature which can make learning bash even more confusing.

A couple examples of what I mean are:

    # old style command substitution (don't use this)
    echo "`ls *.mp3 | wc -l` MP3 files in $PWD"
    # new style
    echo "$(ls *.mp3 | wc -l) MP3 files in $PWD"

    # while it is used in some place, the use of $*
    # to mean "all arguments" is probably always wrong
    for i in $* ; do do_something $i ; done
    # instead, you almost always want "$@"
    # (and always use quotes on variable expansion)
    for i in "$@" ; do do_something "${i}" ; done
A lot of really nasty sometimes-incorrect behavior goes away when you use the modern replacements.

Another big thing that confuses people at first with sh/bash style shell script is that they treat it like a regular programming language. Instead, realize that most of the magic happens as "expansions" of the command line. Thinking about bash a something closer to a fancy macro language doing simple string manipulates can help a lot.

Finally: RTFM. Seriously. Modern versions of bash have a very nice manual. Cargo-culting pieces of existing scripts may solve an immediate problem, but it won't teach you the real language nearly as well as simply reading bash(1) (especially the "EXPANSIONS" section).


Is there a good online resource (other than man pages) for those of us who are exceptionally lazy to learn modern Bash?


greycat's writing are the best thing for bash, in my opinion. The submitted article is his 'bash pitfalls'... his formal bash guide is here: http://mywiki.wooledge.org/BashGuide

greycat is a guy who idles in #bash on freenode and has helped thousands of people.


Thanks!


The funny thing is, I don't consider myself a very smart person or a programmer, but I've gotten bash down pretty well at this point. And I'm a person who has a looooot of trouble with C++ etc., most of my code is shit, if not just unusable.

Though, I have been using linux command-line daily for 12 years now for sysadmining tasks and such. I know there's a lot of weird stuff to worry about, (especially as is noted in the 'bash pitfalls' guide), but I think with a little bit of practice (about 6 months' worth) and good instruction (I love greycat's guides), anyone can get a reasonable good handle of bash.


Agree. Also, http://wiki.bash-hackers.org/doku.php has tons of great info. Sad that TLDP gets all the Google ranking for most bash searches when wooledge and BHW typically have much more detailed and modern (e.g. using "${var}" and [[ instead of [ ) explanations.

Lastly, if you're really stuck on a bash problem, head on over to #bash in Freenode and the awesome people there will very likely help! Greycat hangs out there.


> [[ instead of [

I have yet to see a good rationale for this. If you need the additional operators [[ provides, such as regular expression matching, great. Otherwise, why not just use [ and remain portable?


If you're worried about portability in shell scripts, you should probably be using sh anyway.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: