Reputation: 2575
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.
Upvotes: 0
Views: 702
Reputation: 107809
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 or $myvariable
, 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?”)$(mycommand)
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
Upvotes: 1
Reputation: 360355
Read and understand:
Upvotes: 1
Reputation: 17860
Concept of pipes. The fact that you can easily redirect output and divide complex task to several simple ones is crucial.
Upvotes: 4
Reputation: 46965
Do yourself a favor and get this book: Learning the Bash Shell
Upvotes: 2