Linux for Dummies Quick Referenceby Phil Hughes
Fun, Fast & Cheap!®
Easy Access to Linux Commands, Networking Tips, and E-Mail Hints! A Quick Reference for the Rest of Us!® Get in and get right out with just the information you need without reading tons of extra material! Inside, you'll find clear-cut, plain-English explanations for performing common Linux tasks now/sup>/sup>
Fun, Fast & Cheap!®
Easy Access to Linux Commands, Networking Tips, and E-Mail Hints! A Quick Reference for the Rest of Us!® Get in and get right out with just the information you need without reading tons of extra material! Inside, you'll find clear-cut, plain-English explanations for performing common Linux tasks now!
- Clear-cut advice on how to choose the right Linux distribution and install it
- Alphabetic listing of common shell commands
- Keyboard shortcuts for working with XWindows
- Step-by-step instructions for using text editors and e-mail
- Tons of tips on how to handle Dos, Windows, Mac, or Unix files
- Standard networking and system administration tasks
- Plus a concise reference for regular expressions
Read an Excerpt
Part II: The shell is the command interpreter -- it acts as your interface to the operating system by accepting your input and performing the tasks you request.
Understanding the Shell
Before I get into the nitty-gritty of shells, let me mention that graphical user interfaces are available for LINUX. That means for those of you who prefer the point-and-grunt interface of a mouse, there is hope.
In this part...
- Picking the shell that's best for you
- Getting programs to read and write files
- Connecting the output of one program to the input of another program
- Using quotes to control the interpretation of your input
- Customizing your environment
- Getting familiar with shell conventions
- Examining special characters and what they do
Multiple shells are included with LINUX distributions. They include ash, Bash, ksh, tcsh, and zsh. The most popular shell by far is Bash, a product of the GNU Project of the Free Software Foundation. Bash stands for Bourne Again SHell (named after Stephen Bourne, who wrote the first programmable shell for UNIX).
In this book, I use the Bash shell as the standard. As a user, you may want to consider ksh as an alternative -- particularly if you work with UNIX platforms. The ksh shell is a public-domain implementation of the POSIX-compliant Korn shell, which was written by David Korn at AT&T. The ksh shell is commonly available on most UNIX platforms.
Basic Directory Commands
Use the following commands to perform basic directory management tasks.
cd dirname -- To change your current directory
mkdir dirnames -- To create a new directory
rmdir dirname -- To remove an empty directory
pwd -- To find out which directory you're in
pwd (for print working directory) and the name of the current directory appears on screen. To verify the name of your home directory, type
pwd after logging in.
Some characters have a special meaning to the shell (that is, the shell treats the characters as directions to perform some action). If you need to enter these characters as part of a filename and you don't want the shell to interpret their special meaning, then you need to quote these characters.
Quoting an individual character
To quote any single character, precede it with a backslash (
\). For example, to list the names of all files that contain an
*, type this:
ls -a *\**
In the previous line of code, the shell interprets the first and third asterisk but not the second one. Therefore, it matches any set of characters that contains an asterisk.
You can use double and single quotes (
') to quote whole strings. Their meaning is somewhat different.
- Single quotes quote almost everything.
- Double quotes allow the shell to interpret words that start with a
$, which are commonly known as shell variable references.
- You can use a single quote to turn off the special meaning of a double quote and vice versa.
For example, in the following command, the double quotes prevent the shell from interpreting the special meaning of the single quote in
it's but they allow
$money to be treated as a reference to a shell variable. The double quotes also prevent the shell from interpreting the white space so that
it's my $money is treated as a single argument rather than three separate ones.
grep "it's my $money" junkfile
Using the close-quote character
The single close-quote character (
') is useful for telling the shell to execute a command within the back quotes, insert the output of the execution into the original command line in place of the command in back quotes, and then execute the newly built command line. You can use this trick to display a message that contains the current date.
echo Gee, today is 'date' soon it will be my birthday
The Bash shell maintains a command history, a list of up to 500 of the most recently entered commands. If you type the command
history, the shell displays your history list.
history command is useful when you want to reexecute a command without having to retype it. (The number of commands saved can be changed by setting the
HISTSIZE shell variable.) You can then use the commands described in the following sections to go back through the list and edit and reexecute a command.
Setting command history mode
How you edit recent commands depends on whether your shell is set to vi mode or Emacs mode.
- For vi mode, type
set -o viand press Enter.
- For Emacs mode, type
set -o emacsand press Enter.
Using Emacs mode
If you are in Emacs mode, you can press Ctrl+P to access the previous command, press Ctrl+N to access the next command, or use the arrow keys. When you finish editing, press Enter to execute the command. (See Part V for details on the available editing commands.)
Using vi mode
If you are in vi mode, press Esc and then use standard vi commands (
k to move up,
j to move down, and so on) to access and edit the history list. When you are ready to execute the edited command, press Enter. (See Part V for details on the available editing commands.)
Customizing the Environment
LINUX, in the UNIX tradition, has always allowed you to customize your work environment, and this section shows you some of the ways to do that.
Creating shell aliases
While you can use shell and environment variables to remember what's in a character string, a shell alias is specifically designed to allow you to make up names for commands.
| The name of the alias. If not specified, |
|Command string assigned to the alias. If command contains any spaces or special characters, it must be quoted.|
Suppose that you want to print files occasionally on a printer named
soy (instead of on your default printer). Normally you type
lpr -P soy file to print to the other printer, but you want an easier way. The following command establishes an alias called
lpsoy that would do this for you.
alias lpsoy='lpr -P soy'
Once the alias is created, you can direct your printer output to printer
soy by using the alias. For example, to print the file lentil.loaf, type
You can used the
unalias command to delete a shell alias. For example, to delete your alias
Displaying environment variables
Any environment variable is also available as a shell variable, so you can display an environment variable just as you would a shell variable -- by using the
$ prefix as an argument to a command (such as
echo) that displays the value of its arguments.
Option or Argument
|Name of the environment (or shell) variable to display|
Figuring out which variables are local to your current shell and which are from the environment can be confusing. Type
export with no variable names to see a list of variables that are exported.
Suppose that you have written some shell scripts that e-mail recipes to your fellow vegetarians. The scripts need to know where you keep your list of e-mail addresses. The shell scripts also need to know the name of the directory where the recipes are located. The easiest way to pass this information would be to set some environment variables. Whenever the shell starts the scripts, the scripts can just read in the values.
Setting environment variables
Environment variables are saved in such a way that they are available to any shell that is a child of the current shell. Thus, environment variables are the right place to save things like your search path and the name of your printer.
The order of the commands doesn't matter in the preceding example. The
export statement performs the binding. If the shell variable is already set, its current value is exported to the environment. If the value is changed at a later time, the environment variable follows the value of the shell variable.
Option or Argument
|Name of the variable to set|
|A string value to be assigned. You must quote value if it contains spaces or other characters that have special meaning to the shell.|
If you want to invoke one command with a different value for either a shell or environment variable, but not change its value subsequent to the command, you can specify the variable value on the command line. For example, to run a script called
way_cool with the
PRINTER variable set to
fastone, just type
Setting shell editing modes
With the Bash and Korn shells, you have your choice whether to use vi-style or Emacs-style command line editing (see Part V). My advice is that if you use the vi or Emacs text editor, set your shell mode to the editing mode that matches your editor (which will help you get more comfortable with your editor). If you don't currently use either editor, try each mode and pick the one that you are most comfortable with.
To set the shell mode to vi, use the following command:
set -o vi
To set the shell mode to Emacs, use this command:
set -o emacs
You can place either of these commands in your .profile file so that the mode will be automatically set at login time.
One significant difference between the using the editing commands in vi and those in Emacs is that in vi mode you need to press Esc to activate the editing command (you have to switch back and forth between insert and command mode in vi command-line mode). In Emacs, you need to just type the key combinations as shown in the following table.
|K||Moves back one command in your history list|
|J||Moves forward one line in your history list|
|H||Moves back one character in the displayed command line|
|L||Moves forward one character in the displayed command line|
|b||Moves backward one word|
|f||Moves forward one word|
|0||Moves to beginning of line|
|$||Moves to end of line|
|i||Enter insert mode|
|Esc||Exit insert mode|
|a||Appends to line|
|X||Deletes backward one character|
|x||Deletes forward one character|
|dw||Delete forward one word|
|D||Deletes forward to end of line|
|Esc+_||Inserts last word of previous command|
After you've mastered these commands, check out the man pages (enter
man bash or
man ksh) for additional editing commands.
Standard environment variables
Environment variables are used to set your working environment. You change their values to change your default environment. You may also want to add new variables that your own programs use.
| ||Display location (for the X Window System)|
| ||Path to your home directory|
| ||Name of this computer system|
| ||Your login name|
|Path to your e-mail file|
| || Command search path -- colon-separated list. For example, |
| ||Path to default shell|
| ||Terminal type|
Say that you want to create a new environment variable called
WOW and set it to be equal to the directory path where you keep all your good files. Then you can reference this directory any time you want to copy or access a file in that directory.
The following commands set the variable and then list the file
super from that directory using the
less command. Placing the
export command in your .profile file makes
WOW available every time you log on.
To add a new directory to your current search path, enter
This command adds a new directory named newdir to the end of the path (last directory to be searched).
Directory Naming Conventions
A popular practice is to name directories with a leading capital letter (for example,
Secret). This way, all directories display together in a file list (
ls) and are easily identified. (See Part III for details.)
If you are unsure which entries in a list are directories, type
ls -F and press Enter. A / is displayed at the end of any directories.
A reliable way to access a file in your home directory (or one of its branches) is to use a tilde (
~). It acts as an abbreviation for the path to your home directory For example, if your current directory is not your home directory but you want to get a list of the files in your home directory, you can type
ls ~ as an alternative to
You can also follow the tilde with a user name (such as
~tempeh) to access information in the directory of the user named tempeh. For example, to change your current directory to a directory named Carrots that is in the home of a user whose login name is tempeh, type
Meet the Author
About the Author Phil Hughes is President of Specialized Systems Consultants (SSC) and publisher of Linux Journal magazine. He has authored or edited many of SSC's Pocket Reference cards for the UNIX operating system and utility programs. Phil has worked in computing as a programmer and design specialist since 1968. He has worked with UNIX since 1980 as a systems programmer, consultant, trainer, and writer. In 1983, armed with that one English class he took in college, Phil turned SSC into a company that specializes in documentation for UNIX systems.
Most Helpful Customer Reviews
See all customer reviews