[kwlug-disc] KWLUG - The Kitchener Waterloo Linux User Group new content notification: 2010-08-27 23:04

webhost at kwlug.org webhost at kwlug.org
Fri Aug 27 23:04:50 EDT 2010


Greetings mail-forum-merge,

------------------------------------------------------------------------------
Recent content - 4 new posts
------------------------------------------------------------------------------

1. Bash: The Linux Command Line
Published Book page by john
[ http://kwlug.org/node/762 ]

If you are like many Linux users you're first view of Linux was the
graphical user interface (GUI). If you're adventurous or curious and
have tried to do some advanced things with LInux you may have
encountered the Linux command line. Like all operating systems there is
a low-level interface that opens up all the power of the operating
system. In Linux the command line is so powerful that many advanced
users use it for so many tasks that it becomes indispensable. Those who
use it find it easier, faster and more powerful that the graphical
tools.
Graphical tools have the advantage of being more intuitive so new users
can become effective very fast. However mouse clicks and hunting and
searching for menu options and icons are often very slow for many tasks
making the command line faster for lots of operations. 
Command line environments are not as intuitive as GUIs. This set of
articles is designed to help users get accustomed to Bash.

---

2. Bash Basics - For New Users
Published Book page by john
[ http://kwlug.org/node/763 ]

If you're new to Linux and Unixes in general, then you are new to Bash.
Bash is the command line shell that is the default for most Linux
distros. 
What is Bash
You probably already know that the command line is the most basic way of
interacting with the operating system. Bash is the default command line
and is very powerful on Linux in fact the operating system actually
boots using mostly Bash commands. If it can boot an operating system
imagine what you can do with it. It's used to automate complex jobs
through scripting, when learned it can be used more quickly and more
effectively than the GUI for many tasks, and it allows you to access
many of the top-quality tools that don't have a GUI interface.
Getting into Bash
You can get to the command line in many ways. You can choose the
"Terminal" or "Console" item from the GUI menu, this will launch an
X-Windows terminal emulator with a bash shell running inside. You can
also use one of the virtual text consoles. Pressing Ctrl-Alt and a
function key at the same time switches to another virtual console. Most
GUIs have Ctrl-Alt-F2 as a text console. To return to the GUI try
Ctrl-Alt-F1 through to F8 to find the GUI console. Finally another way
to obtain a shell prompt is over the network using SSH. With SSH you log
into your computer using the same user and password as your GUI and are
presented with a Bash shell.
Core ideas of Bash
Using the command line can be quite simple. The term "command line" is
suitable, you type a command, press Enter and Bash executes the command.
Most commands take additional arguments. Consider the cd command, this
command changes the current directory, i.e. the directory that
subsequent commands will work in. Think of it like opening a new folder
in a GUI environment. The cd command can work without any arguments,
entering cd on the command line and pressing the Enter key will change
the current directory to your user's home directory. That's useful, but
you first had to be somewhere else. To get somewhere else type in a
directory name after the cd command, e.g. cd /tmp. This changes the
current directory to /tmp, a system-wide directory for holding all sorts
of temporary files.
You'll notice something about the cd command we just used. The command
was first on the line and the argument after it. You'll also notice that
a space was used to separate the command from the argument, this is
required. 
Let's take a look at another command. The cp command copies files and
takes two or more arguments. It takes one or more files to copy and a
destination. The destination can be either a new file name in which case
the file is renamed, or it can be a destination directory to which the
file(s) are copied in which case the file names don't change.

cp my_report.txt MyReport.txt
cp baked_beans.txt corn_bread.txt documents/recipies
What really are commands?
Commands come in two general varieties. There are built-in commands that
are programmed into the Bash shell. These are often simple commands or
ones that are designed to affect how Bash works. The command we used
above, cd is a built-in command. When Bash sees that you've typed a
built-in command it interprets that command itself.
The other type of command is an executable program. This can be a
compiled executable that exists as a binary, machine language program,
or it can be a script or any other type of interpreted program. Scripts
are text files that are interpreted by another program. Some scripts are
Bash scripts written to be interpreted by the Bash shell.
All non-built-in commands are files.
Options
We have seen that commands can take arguments, they can also take
options. What is the difference between an option and an argument? Well
there really isn't anything technically different, options can be
thought of as a specific type of argument. It's a customary thing
really. Options are switches that change the function of the command
where a arguments are thought of as the object the command is working
with. This isn't an absolute truth since it's up to the programmer of
the command to determine how the arguments are used.
Options are arguments that begin with one or more hyphens (-). They
change how the command operates. Take the -i of the mv command, which
moves files. The -i switch makes the mv command prompt before
overwriting files.

mv -i baked_beans.txt corn_bread.txt documents/recipes

Some options themselves require an additional argument. Take the mail
command, it can be used to send email. The arguments are a list of email
addresses to send the email to. Optionally subject line text can be
specified on the command line:

mail -s "Buy Viagra Now" john at sucker.com charles at filter.com

In the above case the -s option requires the subject line text to
follow. I had to put the text in quotes because it includes spaces. If I
didn't then Bash would think that the subject line was "Buy" and the
other words would be email addresses.
More recently programmers have started using longer option names, either
because it makes them more memorable, or because they ran out of single
letter options. These new-style options customarily begin with two
hyphens (--).
We can see this in the ls command. This command lists files and is one
of the oldest commands and has a huge list of possible options. We will
use the -all option. Normally ls will not list hidden files, the --all
option causes ls to display hidden files as well.

ls --all 
More Commands
There are a lot of command available for use in Linux. On my laptop,
which doesn't have everything installed, there are over 4,000 commands.
You can see a long list of commands by looking in the /bin and /usr/bin
directories. 

ls /bin /usr/bin

These are the main directories for user programs. If you want to know
more about a program then use the man program, it displays the manual
page for a specific command. To see the man page for the ls command type
man ls.
You can find a list of the built-in commands by looking at the man page
for Bash (man bash) and searching for the SHELL BUILTIN heading (hint:
while viewing the page type /^SHELL BUILTIN and press Enter.)

---

3. Bash Environment Variables
Published Book page by john
[ http://kwlug.org/node/764 ]

The shell is configured in a few different ways, but one of the main
ways is through what are called environment variables. These variables
are not only used by the shell, but can be accessed by all Linux
programs.
To explain environment variables I'll start by showing one of the most
basic variables, the PATH variable. This variable defines which
directories are searched to find commands. So when you type a command
like vi the Bash shell looks at the contents of the PATH variable and
searches each of the directories for a file called vi. The first one
that is found is executed. So let's look at the contents of PATH by
typing this into a shell prompt:

echo $PATH

You might see something like this:

/usr/lib/qt-3.3/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/lib/ccache:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin

We used the echo command to print something to the screen and if we put
a dollar sign in front of an environment variable name the shell
actually substitutes the contents of the variable before running the
command line.
The above is my PATH variable and it shows many directories are
separated with colons.
Another way to look at environment variables is the set command. Run set
and you see a huge list of variables that exist.
To set or change a variable you can use the following syntax:

VARIABLE="value"

Commonly we might add something to PATH. For example you may create
scripts in a bin directory in your home directory and want to run these
without putting a full path name in for each command. This is a special
example because we want to add to the value not replace it. To do this
we can use the PATH variable while setting it. So to add /home/john/bin
to the PATH we would run:

PATH="$PATH:/home/john/bin"

In the above example, Bash substitutes $PATH for the contents of the
PATH variable before assigning the new value to PATH. This way the new
directory is added to the end.
Environment variables also have a certain property. We often want
processes that we run from a shell to inherit the environment variables
that we set. If we simply define variables like above it only affects
the variable for this shell and not child programs. To allow subsequent
programs to inherit the variable we need to export the variable:

export PATH
Shell Prompt
One other common use for environment variables is to redefine what the
shell prompt looks like. This is held in the PS1 and PS2 variables.
Setting PS1='$ ' sets the command prompt to a very simple dollar sign.
If you want to get elaborate there are many variables that you can put
into the command prompt so that it displays things like the host name,
current directory, date and other variable facts. Let's change this to
the host name and current directory. To do this we will use the \h and
\w variables. In order for the backslash characters to be left alone we
need to make sure that we use single quotes, not double quotes when we
set the variable.

PS1='\h@[\w]$ '
export PS1

To find other variables to use in prompts view the man page for bash
(i.e. run man bash) and search for the PROMPTING section.
Clearing variables
One may think that by setting a variable to an empty string will clear
it. In fact it does not. There is a different between an unset variable
and variable that is set to an empty string. 
To delete a variable use the unset command:

unset PS1

This will make your prompt nothing. To restore it repeat the PS1 setting
in the Shell Prompt section above.
Making permanent changes
You will find that any changes that you make in the shell only work for
that shell instance and any shell or program launched from it. As soon
as you exit the shell the environment is lost.
All these changes disappear when you exit the shell. If you want to
configure a variable that is permanent edit the .bashrc file in your
home directory and add the above lines to the end of the file. You may
also find environment variables set in your .profile file as well,
although changes to this file will require logging out of the GUI and
back in to see the changes.
System-Wide Changes
There are other places where environment variables are set. You can set
them for all users in the /etc/profile and /etc/bashrc files.
How Other Programs Use Environment Variables
Programs other than Bash can be configured using environment variables
too. One that comes to mind is rsync. This program synchronises files
between two directories even if they are on different servers. It uses a
few different variables but the one I use most is RSYNC_RSH which tells
rsync to use ssh to connect to remote servers.
The easist way to see if a program uses environment variables is to look
at its man page. Search the page for the word ENV.
Other ways to set Variables
When executing another program you can specify environment variables
before the name of the command. So to set rsync to use ssh we need to
set the RSYNC_RSH variable. We could set as we have done above and
export it, or we can specify it on the command line before the command:

RSYNC_RSH=ssh rsync /source/dir /dest/dir

The variable is passed to the command but is never set in the current
shell. We can demonstrate this by running bash and giving an echo
command as an argument:

H=Hello W=World bash -c 'echo sub shell: $H $W'
echo 'this shell: $H $W'

We see the words "sub shell: Hello World" printed to the screen. If we
were to repeat the same echo command on the next line by itself it would
print only "this shell:" because the variables were never set in the
current shell.
NOTE: The use of single quotes is important. Any dollar signs used
within single quotes are not interpreted as variable names. This way the
bash sub shell actually sees the dollar signs. If we used double quotes
the current shell would replace the variables before the sub shell was
executed.

---

4. Bash Aliases and Functions
Published Book page by john
[ http://kwlug.org/node/765 ]

Aliases and functions are interesting ways to customize Bash. You can
create handy short forms and simplify complex commands.
Aliases
Within Bash one can create different names for commands. Aside from
simply creating a different name, the command can include options and
other arguments. This is called an alias in Bash.
Creating aliases are easy. Let's create a simple alias for the ls
command that does a long listing, i.e. a listing with permissions, file
size and date. I type ls -l very often and it would be handy to not have
to type out the whole thing every time. I know, it's only 4 characters
(there's a space), but those keys can add up over time and carpel tunnel
syndrome is a serious career risk for people like me.
So let's shorten it to a single simple l using an alias with this
command:

alias l='ls -l'

Once set we can now use the l command and it works just like typing the
whole thing out.
We can see what aliases are set using the alias command all by itself:

alias

After we have set the above alias it should look like:

alias l='ls -l'

We can use aliases like real commands providing options and arguments.
When specified additional options or arguments are added to the end of
the aliased command. Let's say we want to add the -h option (make file
size human readable) when we run l and we also want to list a specific
file:

l -h .bashrc

>From the results it's as if we typed this command: ls -l -h .bashrc.
We can delete an alias using the unalias command. If we also use the -a
option of this command it removes all aliases:

unalias l
Alias Scope
Aliases have a limited scope in that they are only available to the
shell in which they were defined. Subshells do not see them, nor can
they inherit them.
To make an alias available to all shells define the alias in your
.bashrc file or in the system-wide /etc/bashrc file.
Alias Limitations
Aliases are limited in that any arguments are added to the end of the
aliased command. Although we make an alias out of a complex command the
arguments always go the end. So let's say we want to add paging to our l
command by piping the result through less, we might try this:

alias l='ls -l | less'

When we use the l command without an alias it works as expected but as
soon as we add a file name it, say the same .bashrc. When we try this we
see the .bashrc file listed to the screen. That's because the actual
command executed is ls -l | less .bashrc
Because of this limitation we can't do many other neat things. This is
where we can use functions.
Functions
Functions are more powerful than aliases. Not only can we re-arrange
parameters but we can actually create simple programs using them.
The topic of functions really becomes a programming lesson quickly so
I'll only go into a few simple examples here to solve the limitations of
alias.
Let's say we want to achieve the last failed example, we want to page
the output of ls -l. we would write a function like this:

unalias l
l() {
    ls -l "$@" | less;
}

I've used separate lines to write this function but it can all be put on
a single line if that's more convenient. It's proper programming style
to have it on seprate lines.
When you type this in you'll notice that you get a different prompt
after the first line. This is the PS2 prompt that we talked about in the
Bash Environment Variables page. This prompt means that the command is
not complete. Once you type the closing brace (}) you will be back to
the normal prompt.
Notice that I've also deleted the l alias. This is because both cannot
exist at the same time.
With the l function created you can now use it like a command. Feel free
to create more complex multi-line functions that do very complex things
if you like.
To see the functions you've created run the set command. The functions
will listed after the environment variables.
Functions can be deleted using the unset command:

unset l
Function Scope
Functions are also local to the instance of bash in which they are
created. To make it available to sub shells add it to your .bashrc file
or the system-wide /etc/bashrc file.


-- 
This is an automatic e-mail from KWLUG - The Kitchener Waterloo Linux
User Group.
To stop receiving these e-mails, change your notification preferences at
http://kwlug.org/user/28/notify





More information about the kwlug-disc mailing list