views:

74

answers:

4

ALthough I've had to dabble in shell scripting and commands, I still consider myself a novice and I'm interested to hear from others what they consider to be crucial bits of knowledge.

Here's an example of something that I think is important:

I think understanding $PATH is crucial. In order to run psql, for instance, the PostgreSQL folder has to be added to the $PATH variable, a step easily over looked by beginners.

+2  A: 

Do yourself a favor and get this book: Learning the Bash Shell

ennuikiller
+4  A: 

Concept of pipes. The fact that you can easily redirect output and divide complex task to several simple ones is crucial.

sha
I agree, pipes are certainly an important tool, especially when grepping output from other commands.
LDK
+1  A: 

Read and understand:

Dennis Williamson
+1  A: 

If you're writing shell scripts, an important habit to get into is to always put double quotes around variable substitutions. That is, always write "$myvariable" (and similarly "$(mycommand)"), never plain $myvariable or $(mycommand), unless you understand exactly why you need to leave them out. (Again, the question is not “should I use quotes?”, it's “why would I want to omit the quotes?”)

The reason is that the shell does nasty things when you leave a variable substitution unquoted. (Those nasty things are called field splitting and pathname expansion. They're good in some situations, but almost never on the result of a variable or command substitution.)

If you leave out the quotes, your script may appear to work at first glance. This is because nasty things only happen if the value of the variable contains some special characters (whitespace, \, *, ? and [). This sort of latent bug tends to be revealed the day you create a file whose name contains a space and your script ends up deleting your source tree/thesis/baby pictures/...

So for example, if you have a variable $filename that contains the name of a file you want to pass to a command, always write

mycommand "$filename"

and not mycommand $filename.

Gilles
This does not apply to zsh
ZyX