Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to learn your way through Linux's shell

I want to stop losing precious time when dealing with Linux/Unix's shell.

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

  • I may end up losing 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 the real difference is between:
    • . run.sh
    • ./run.sh
    • . ./run.sh
    • sh run.sh
    • sh ./run.sh

You see, it's these kind of things that cripple 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?

like image 638
Daniel Jomphe Avatar asked Jan 16 '09 19:01

Daniel Jomphe


People also ask

Is shell scripting difficult to learn?

A shell script have syntax just like any other programming language. If you have any prior experience with any programming language like Python, C/C++ etc. it would be very easy to get started with it.

How long will it take to learn shell scripting?

You can learn this language in this course called Linux Shell Scripting through many projects, which will take you around two weeks to a month to complete.

How do I navigate in Ubuntu terminal?

To navigate to your home directory, use "cd" or "cd ~" To navigate up one directory level, use "cd .." To navigate to the previous directory (or back), use "cd -" To navigate through multiple levels of directory at once, specify the full directory path that you want to go to.


2 Answers

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.

like image 104
Jon Ericson Avatar answered Oct 04 '22 18:10

Jon Ericson


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.

like image 22
mipadi Avatar answered Oct 04 '22 18:10

mipadi