views:

567

answers:

11

I want to stop losing precious time when dealing with linux/unix's shell.

If I could get to understand it all so well, that would be so great. Otherwise:

  • I may end up loosing a day just for setting up a crontab.
  • I'll keep wondering why the shebang in this script doesn't work.
  • I'll keep wondering what's the real difference between:
    • . run.sh
    • ./run.sh
    • . ./run.sh
    • sh run.sh
    • sh ./run.sh

You see, it's that kind of things that cripples my linux/unix life.

As a programmer, I want to get much better at this. I suppose it's best to stay with the widely-used bash shell, but I might be wrong. Whatever tool I use, I need to understand it down to its guts.

What is the ultimate solution?

+1  A: 

The best way to learn is by reading, and then trying things out. MAN pages are often very helpful, and there are tons of Shell scripting tutorials out there. If shell scripting is what you are after, just read, and then practice the things you read by writing little scripts that do something neat or fun. If you are looking for more info on all of the different command line applications that can be run from a shell, those are more distro dependent, so look in the documentation for your favorite distro.

cdeszaq
+2  A: 

I think immersing yourself in it is the answer. It's like learning to walk, to type, to use an ergonomic keyboard, or type in dvorak. Commit to it completely. And yes, you will absolutely slow down. And you'll be forced to look at your hands or google things constantly. But eventually it will come to you.

Funny story, I killed my apartment's internet access by doing an ifconfig release. Completely didn't know that ifconfig renew was not a command. Had to call a friend when google didn't load ;) dhcpcd later and I was back to googling everything.

Tom Ritter
+3  A: 

Try the Linux Documentation Project's BASH pages here [tldp.org].
PS: The difference is that . is the same as the source command. This only requires read permission. To use ./run.sh, you need execute permissions. When you use 'sh', you are explicitly specifying the command you want to run the script with (here, you only need read permission). If you are using a shell to execute it, there shouldn't be a problem there. If you want to use a different program, such as 'python', you could use python run.py. Another trick is to add a line #!<program> to the beginning of your script. In your case, #!/bin/sh would do, and for Python, #/usr/bin/env python is best.

Lucas Jones
+1  A: 

use the shell a lot, type "[prog-name] --help" a lot, type "man [prog-name]" a lot, and make notes on what works and what does not -- even if those notes seem obvious at the time. By tomorrow, they might not be so obvious, again. OTOH, in a couple of weeks they definitely should be!

Have a gander at some of the many books on working with shells, like From Bash to Z Shell (which covers Bash and Z), slog through a shell-scripting tutorial, or Gnu's Bash manual.

Michael Paulukonis
+14  A: 

Just for fun:

  1. . run.sh --- "Source" the code in run.sh. Usually, this is used to get environment variables into the current shell processes. You probably don't want this for a script called run.sh.

  2. ./run.sh --- Execute the run.sh script in the current directory. Generally the current directory is not in the default path (see $PATH), so you need to call out the relative location explicitly. The . character is used differently than in item #1.

  3. . ./run.sh --- Source the run.sh script in the current directory. This combines the use of . from items #1 and #2.

  4. sh run.sh --- Use the sh shell interpretor on run.sh. Bourne shell is usually the default for running shell scripts, so this is probably the same as item #2 except it finds the first run.sh in the $PATH rather than the one in the current directory.

  5. sh ./run.sh --- And this is usually the same as #2 except wordier.

Command line interfaces, such as the various shell interpretors, tend to be very esoteric since they need to pack a lot of meaning into a small number of characters. Otherwise typing takes too long.

In terms of learning, I'd suggest using bash or ksh and don't let anyone talk you into something else until you are comfortable. Please don't learn with csh or you will need to unlearn too much when you start with a Bourne-type shell later.

Also, crontab entries are a bit trickier than other uses of shell. My guess is you lost time because your environment was set differently than on the command line. I would suggest starting somewhere else if possible.

Jon Ericson
Thanks for the information, it helps.
Daniel Jomphe
+7  A: 

Man pages are probably the "ultimate" solution. I'm always amazed at what gems they contain.

You can even use man bash to answer some of the questions you raise in this question.

mipadi
Very interesting, thank you! (man bash)
Daniel Jomphe
I'm surprised at all that's available in bash's manpage. It even links to Gnu's Bash manual, and other useful stuff. All of this, just 9 key presses away from my eyes when I would have needed it the most...
Daniel Jomphe
Yeah, I used to use Google a lot for that stuff...until I realized that the man page for Bash is a treasure trove of useful information, all in one handy place.
mipadi
The man page for pretty much everything is the authoritative documentation... it took me a while to realize that when I first switched to linux.
rmeador
for GNU tools, info pages are often much better than the manpages, because GNU generally doesn't like manpages (i believe because their format is proprietary). just do "info coreutils". "info bash" also gives a much more structured output of things than the manpage. very recommended!
Johannes Schaub - litb
@litb: I strongly dislike the info pages. Part of the problem is that I find it easier to navigate one big page rather than a bunch of little pages. But to each their own, I suppose.
Jon Ericson
Nice. A classic RTFM :) haven't seen that acronym used much on SO, but maybe it should appear more.
popcnt
The format for man pages, IIRC, is troff, which is obscure but not proprietary.
David Thornley
+1  A: 

Oreily has an older book that teaches you about bash, and bash scripting at the end. Most everything you need to know is on the web, but spread out.

J.J.
According to titlez.com, this is still the best-selling book on bash shell on Amazon. Thanks!
Daniel Jomphe
@Danial: You welcome. I have the 2nd Revision. I am an experience linux user, and I still pick it up from time to time for answers.
J.J.
+1  A: 

There are some decent online guides that will help you feel more comfortable in the shell(s). For example:

Spend some time reading those kinds of tutorials and, above all, playing in the shell. Pretty soon, it'll start to feel like $HOME. (Okay, sorry for the bad pun...)

Brian Clapper
A: 

The way I really learned my way around in Linux was to install gentoo. It takes forever but you begin to see how everything ties together.

Go grab the latest instructions and start following them. Do it enough times and it starts to stick.

After a while you get comfortable building everything from scratch.

darren
A: 

I find trying to learn a new command using the man pages sometimes a bit overwhelming.

I prefer man pages for refreshing my memory.

When I want to learn a command I look for examples then use the man pages to fine tune what I want it to do.

Berek Bryan
+1  A: 

While sticking with Bash might be best, while just learning, the fish shell might be a little bit easier to use. I played around with it for a few weeks, and while it didn't seem as powerful as bash, it did seem pretty user friendly.

Matt McMinn
I tried fish a few months ago. I enjoyed it a lot. It's just too bad because it didn't work well for my needs. When I'm more used to bash and more dedicated to using shells, maybe I'll give it another try. I just wouldn't have to write scripts for every tool that bash ahs scripts for and I need.
Daniel Jomphe