# The Linux Shell For Beginners

The shell or command line interface has many great features and makes you into a better computer user. In this section I will go into details of how that happens.

## Files and Directories

Files are where your data is kept. A file can be many things. When you are storing input, it goes into a file. This can be a text file, a drawing program, or a sound file. These are some linux essentials you can't forget.

Directories are organizational structures. They can organize your files and other directories. At any one time, you will be in a distinct directory. You have to be logged in to have a current working directory.

## The Shell

A shell is the interface to the operating system. It is text based and it accepts input as text. The input will usually invoke small programs or utilities that are installed in the operating system. There are many different shells but the most common one is <Bash>. This is part of the history and usage of linux.

When you first log in, the operating system will put you in your home directory. You can change this behavior, just so you know. When you change directories you can always find out where you are.

I can enter in the command:
pwd
and it will tell me what directory I am currently in.

Now, when you invoke a utility like "pwd" the shell executes this command. What it does and what you will see from then on depends entirely on the utility and what it is designed to do.

You can also modify commands. This is done by the use of "arguments".

pwd -L             "use from the environment"
pwd -P             "avoid all symbolic links"
pwd --version       "output version information and exit"
pwd --help          "display help and exit"

You can also have multiple arguments for a command. This can greatly change its
behavior.

Certain commands require certain arguments. A "cp" command, which copies, needs
to know what it is copying and where it is copying to.

cp directory1 directory2

You can also have options for any particular command. They are called "options"
because you do not have to use them to get the command to work. They work like
arguments, however, they extend the behavior of that command.

Options and arguments are usually preceded by a hyphen or two depending on
the command. If you need to use multiple arguments and options then use a
single hyphen with the corresponding letters.

pwd -LP

As you can see, there are no spaces in between the otpions. Most of the time
it does not matter in what order you put the arguments or options.

Most utilities will have a help feature.

pwd --help

It works the same for most commands. It will give you a lot of details about
the command. Arguments, options, and examples are very helpful to understand
how a command is supposed to be used.

## Using Commands

You usually have to be in the directory of a utility in order to run it. The
exception to this is, of course how the path is set. The path is a  variable the
operating system uses to check directories for programs to run. That makes it
very useful so you don't always have to be in the /bin directory for example.

Of course, this was never the case as the path variable was always used.
However, if you did not have a path set somehow, you would have to be in the
directory to use any utility you wanted.

There is a trick to run a program without using the path.

./script1.sh

This lets you run a utility without using the path variable. This can be useful
at times. Experienced users should not need to do this much. Keep it in mind as
an option though if you need it sometime.

## Redirecting Output

You can redirect the output of commands. The output can be sent to another command or even a file.

pwd > test.txt

This will run the "pwd" command, which tells the present working directory. The      results or output will be sent and stored into the test.txt file. This is very flexible and should be used when you need to do something like this.

This operation will delete the file if there is another with the same name. Be  careful using it.

## Redirecting Input

Just like output, you can redirect input. This is most often done with files. A file can contain a book list, for example. Commands like <cat> or <grep> can have the fileinput sent to it.

cat < booklist.txt

grep Magnus < booklist.txt

## Pipelines

You can connect two different commands through the use of a pipeline. This is
the pipeline symbol < | >. When it is used, it takes the output of the first
command and sends it to the input of the second command.

This is very similar to redirecting output and sending it to a file. The
difference is that we are just dealing with commands. This makes the pipeline very flexible and good to use when appropriate.

ls | lpr

The above example takes the output from the <ls> command and sends it to the <lpr> command. The <lpr> command is a print utility, so <lpr> will print the
files listed by <ls>.

 who | sort

This example takes the output of the <who> utility and sends it to the <sort>
utility. A list of users on your computer will be alphabetically sorted by
this one command.

who | grep jmoore

This is another good command to use. The <who> utility lists users and the
<grep> utility searches for patters that you specify. We want to search for a user. If you have a bunch of users and you need specific information then use
this to get your list and send the output to the <grep> utility.

There are many utilities that will work for this. Don't worry about knowing all at once. Over time, it gets easier to put them together when you need
specific information. You can also use three or more utilities at once with pipelines as long as nothing conflicts.

## Background Commands

You can run commands or utilities in the foreground or background. Most of
your commands will be in the foreground. There are good times when you want
to run them in the background though. If a command will take a long time to
run then it is a good candidate to run in the background.

The reason you would want to do this is that it frees up your shell for your to run other commands and do other tasks. When you run a command in the background it is now a job. The shell keeps track of it and assigns it a job number. You can even query this job number to check on the progress of the job.

You use the <&> sign to indicate the current command is to run in the background. One thing I do a lot is update computers on my network. I have a script I wrote for this.

updates.sh &

This will run my script in the background as a job. I can do other things because it is going to take a long time. This makes it very useful.

To use an earlier example you can do it with whatever you need to print.

ls | lpr &

Again, this throws the output of <ls> into the <lpr> print utility and prints everything in the background.

## Conclusion

In this chapter we talk about using the shell in linux. It is part of the history and usage of Linux. It is very useful and can make your work a lot easier. Commands can have options and arguments that you use after the command. These will modify the behavior of the command itself.

When you enter a command it needs to listed in the path variable or you need to be in the current directory of the program.

You can chain commands through the use of pipelines. Pipelines use the <|> symbol. They take the output of the first command and send it to the input of the second command. This is nice because you can get very specific and accurate information, quickly.

Commands can also be run in the background. This is another useful feature that will enhance your productivity. If there is a long task to run, start it and have it run in the background. It will go away from sight but still be running. You can then use your shell to do other tasks like create new users or modify permissions on files.