3 ms·
Bash isn't particularly hard. Unfortunately, it carries a significant amount of historical baggage that make things very confusing. Many examples and existing b
by pdkl95 11y ago
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).
- angersock 11y agoIs there a good online resource (other than man pages) for those of us who are exceptionally lazy to learn modern Bash?
- pen2l 11y agogreycat'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 http://mywiki.wooledge.org/BashGuide greycat is a guy who idles in #bash on freenode and has helped thousands of people.
- angersock 11y agoThanks!