Linux Online Advertisement
[ Register ]

[ Applications ]
[ Documentation ]
[ Distributions ]
[ Download Info ]
[ General Info ]
[ Book Store ]

Advertisement

[ Courses ]
[ News ]
[ People ]
[ Hardware ]
[ Vendors ]
[ Projects ]
[ Events ]
[ User Groups ]
[ User Area ]

Linux in a Nutshell, Fourth Edition

[ About Us ]
[ Home Page ]
[ Advertise ]

Getting Started with Linux - Lesson 6

Shells in Linux

Sea shells by the seashore, Can you say that ten times fast? How about 'Shells in Linux'? That's not as difficult. Learning to use shells in Linux isn't going to be difficult either.

Why you need to use a shell

As I mentioned in a previous lesson, a shell is just a way for your computer to receive commands. The most common shell used for working in Linux is the 'bash' shell. Our lesson will deal with that one.

The most common commands a computer receives are ones to copy files, move files around, list files and delete files. Popular operating systems have perfected this to such a degree that they have graphic interface programs to do all this for you just by moving the mouse around and clicking on a few buttons. Linux has these programs too, but anybody who's seriously thinking of using Linux on a day-to-day basic should be familiar with the commands that you type in by hand. Some people see this as a throw back to the old days. I see it as a way to have more power over your computer because even those operating systems that are billed as more 'user friendly' have provided you with a shell, just in case you need it. And sometimes you do!

The '.bashrc' file

Before you start using the 'bash' shell you should be aware of a file that sits in your home directory called '.bashrc'. You'll find a lot of files on the system that end in 'rc'. Those files allow you to configure a certain program to run just the way you like it. The best way to find it is to type.

'ls .bashrc'
(ls lists files)

You can open that file with vi, joe or pico, as we talked about in the last lesson.

For example, in your home directory you would type 'pico .bashrc'

An introduction to aliases

In that file, you can add something called an 'alias'. Everybody knows what 'alias' means- 'an assumed name'. An 'alias' in this file are some lines that you write so that your bash shell assumes that one command is really a variation of it. As you already know, you can modify a command with a dash '-' and a letter To see where the .bashrc file was, you could have typed 'ls -a' and that would have shown you every file in the directory, including those that start with '.' If you find yourself using these '-letter' combinations a lot, you can modify your .bashrc file so that even though you type the simple command, like 'ls', you actually get 'ls -a'.

Some of these aliases may be very important to keep you from sending that novel you just wrote into non-existence by accident. I have a couple of entries in my .bashrc file to keep me from getting into trouble. They are:

alias cp='cp -v -i'
alias rm='rm -i'
alias mv='mv -i'

Let me explain them.

'cp' is the command to copy a file to another place or to make a copy of a file under a different name. In order not to copy a file to a place where there's already a file by the same name, you could type cp -v -i, (-v for verbose, -i for interactive) and it would ask you if you really want to do it in case there's another file by the same name. Then the -v would show you where it went. This is probably a good idea all the time, so you could create an alias for it in your '.bashrc' file.

'rm' is the remove/delete command. In Linux, this means gone forever [cue ominous organ music] You obviously have to be very careful with this one, because in the bash shell there is really no 'trash' bucket to pick it out of if you delete it. That's why I've added the -i (interactive) command to my alias, so that it asks me if I really want to delete that novel I just wrote.

'mv' is for moving files to a different place or renaming a file. I have an alias for it for the same reasons as the 'cp' command.

Adding aliases to the .bashrc file

Well, you now have '.bashrc' open in 'pico' or your new, favorite text editor. It would be a good idea to add this line first, so you know what you've done.

# my personal aliases

The pound sign (#) tells the shell not to read that line. It's known as a 'comment'. Then you would add:

cp='cp -v -i'

on the next line write:

rm='rm -i'

so we don't send anything into byte heaven without a warning. And finally

mv='mv -i'

So you're aliases will look like this

# my personal aliases
alias cp='cp -v -i'
alias rm='rm -i'
alias mv='mv -i'

Save that file and logout and login again. Now you have a safer, easier shell environment. As you get more proficient at Linux, you can add more aliases as you see fit.

Now your shell's ready to go. If you type logout and then login again, your aliases will work. There is also a short-cut. If you type:

source .bashrc

your aliases will be ready to go.

[Previous] [Next]




Comments: feedback (at) linux.org
Advertising: banners (at) linux.org
Copyright Linux Online Inc.
Compilation ©1994-2008 Linux Online, Inc.
All rights reserved.