Thursday, April 30, 2009

Learning BASH (part 1)

What's BASH?

BASH (GNU Bourne-Again SHell) is the scripting language used in mostly Linux Environments. If you don't know what Linux is or don't have a Linux based OS installed on your computer right now visit: http://www.linux.com/whatislinux/114354. For this blog post, I will recklessly assume that you are using Ubuntu, as that's what I'm using right now. If you're not using Ubuntu, things will hopefully work out anyway (they should, at least). BASH and Linux can be compared to DOS and Windows.

Why Learn It?

If your Operating System doesn't boot any more, booting into a BASH console (Ubuntu recovery mode) instead into an X Window Server (the thing which manages the windows which you're probably looking at) is a great and easy way to recover your files and fix your booting process. BASH can also be an easy and quick way to get things done around your great open source OS.

How to Learn It:

Now that I've (hopefully) convince you to learn BASH, let's learn it =D

First of all, you will need to find a way to access a BASH console. In Ubuntu such a terminal can be found under: /usr/bin/gnome-terminal (navigate to /usr/bin/ and double click on gnome-terminal to open it). I suggest you create a shortcut to it by dragging it onto your Desktop while holding CTRL + SHIFT.

Once open, you will hopefully see this (if Simon is your name and Simon-Ubuntu-8 is your computers name):



Want to change the Colors to something (hopefully) better looking? Click on "Edit" in the toolbar, then click on "Profile Preferences" on the drop down menu. Finally click on the "Colors" tab on the window which just opened. After changing the colors to what you want them to be, close the options window and lets get started =D

Do you see the "$" at the end of the first line on the terminal? That means the terminal is waiting for you to command it. Let's not make it wait for too long =D

Make sure the terminal window has your mouse focus by clicking on it, then type:
man
Hopefully the terminal replied like this:
What manual page do you want?
And then displaying your name and the "$" sign once again, ready for the next command.

Now what have you just done? You have told the console to run a file called "man" (stands for manual) and to display the results. Do you want to know where the "man" file is located?

Type in:
which man
And here's what I got:
/usr/bin/man

As you can see, typing in "which" and then a command name will tell you the file where that command is stored. Now let's go back to the "man" command. Typing in "man" and then a command name will tell you everything about that command.

Try typing:
man ls
You will get something which looks like this:


If you look at the bottom left corner, it will tell you what you are looking at and the line you are currently on. You can navigate up and down by using (Surprise!) the UP and DOWN arrow keys. Once you are done reading about the "ls" command you can press Q (as in Quit) to go back to your familiar console. The "man" command is VERY IMPORTANT, use it to check up on commands you don't know about before you use them. ALWAYS.

As you have hopefully figured out by now, the "ls" command lists the files of the directory you are currently looking at. Try it out if you want to =D

That's all nice and good but how do you know in which directory you are at the moment, and how do you change it?

Do a man look up on "pwd" by typing:
man pwd
After reading, press Q to exit, as I told you above.

Hopefully you have figured it out by now, the "pwd" command Prints the Working Directory, so the directory you are currently in.

Try it:
pwd
I got:
/home/simon
Now you could type:
ls
To get a list of files in the /home/simon directory.

Now how do we navigate around our directories?

Use the "cd" command. Typing in "man cd" gets you: "No manual entry for cd", so don't even bother. Instead, read this: Typing "cd" and a sub-directory name will change your current directory to that.
Let's try it =D

Find your current directory by typing:
pwd
Here's mine:
/home/simon
Now type "cd" and a sub-directory:
cd Desktop
Then find your current directory again by typing:
pwd
And be surprised, it changed:
/home/simon/Desktop

Here are some other useful things you can do with the "cd" command:
Type in "cd /" to navigate to the root directory.
Type in "cd .." move to the parent directory.

The End (for now).

What, but there is so much left to learn?!
Yes, there is. I will write more later this week (hopefully). This tutorial was quite fun to write so I think I will make a tutorial series.

In the mean time, look up these basic commands (using "man" or Google):
  1. echo
  2. sleep
  3. su (useful)
  4. true
  5. false
  6. netstat (useful)
  7. ifconfig (useful)
  8. ping (somewhat useful)
  9. open (useful)
  10. cat (useful)
  11. mkdir (useful)
  12. rmdir (useful)
Have Fun =D

No comments: