Guide to Unix/Explanations/Shell Prompt

From Wikibooks, open books for an open world
< Guide to Unix‎ | Explanations
Jump to: navigation, search

The shell prompt (or command line) is where one types commands. When accessing the system through a text-based terminal, the shell is the main way of accessing programs and doing work on the system. In effect, it is a shell surrounding all other programs being run. When accessing the system through a graphical environment such as X11, it remains possible to open a terminal emulator and do useful work with the shell.

This chapter describes how to find a shell prompt and start using it.

Clipboard

To do:

Finding a shell prompt[edit]

People get shell prompts in different ways, such as:

  • They use a graphical environment (such as Aqua, GNOME, or KDE) and a terminal emulator.
  • They do not use GUI, but simply use TTY device; sometimes also use GUI and get to a TTY device with Ctrl+Alt+F[number] (most GNU/Linux systems allow 1-6 for [NUMBER]). To get back to The X Window System, use Ctrl+Alt+F[number 1 higher than the number of TTY devices].

Using the TTY device[edit]

Unix systems can use TTY devices for a shell. The first line looks like

login:

Type your username at this prompt, then type your password. This gives a shell.

Opening a terminal emulator[edit]

A terminal emulator (or console emulator) is a program that emulates the terminal hardware that early users traditionally used to login to Unix. It appears as a window in the graphical environment and allows access to the shell prompt.

There are several ways to open a terminal emulator:

  • With The X Window System, try xterm. Most setups either start you with an xterm or two when you login, or provide a menu from which you can start an xterm.
    • KDE provides a Terminal Program (Konsole). You can find it in the K Menu > System menu. If you right click on an empty part of your icon panel, you can Add an Application Button for the terminal.
    • GNOME provides a terminal emulator somewhere in the Programs menu.
    • LXDE provides the terminal emulator LXTerminal (at least in the Lubuntu distro). It can be found in the Accesories submenu of the launch menu, usually situated in the lower left corner of your desktop.
    • Using Enlightenment 17, right click on the desktop, then go to Enlightenment>Eterm.
  • On Mac OS X, use /Applications/Utilities/Terminal. That is, go to the Applications folder on your hard disk, and to the Utilities folder inside it, and double-click the icon for the Terminal program. You may want to keep the Terminal icon on your Dock.

Most terminal emulators, like other graphical programs, provide a menu bar to configure the terminal. For example, they allow you to change the font and colors; some people prefer white text on a black background. xterm is more difficult to configure; its menus can be found by holding the Control key and clicking with each of the three mouse buttons.

Using SSH to access a remote shell[edit]

The ssh program is a secure way to connect to a shell account on a remote server. The server must be running the sshd servers software to accept the connection. See the chapter on Connecting to Remote Unix.

Appearance of the prompt[edit]

The shell prompt normally ends in a $ sign. For simplicity, the examples in this book use a shell prompt like this:

$

Some older shell prompts end in % instead:

%

The C shell sometimes uses > instead:

>

Several shells have prompts that give more information, such as:

localhost:puffy {1}

You can also customize your shell prompt. For bash, use these special characters in the variables $PS[1-4]. $PS1 is what you usually see and $PS2 is what you see when you are doing a multi-line command with a backslash ('\'). For more see the manual.

Never copy/type the shell prompt used in this book. The shell will always give you a prompt if it is ready to accept commands.

Root shell prompt[edit]

If you become root or login as root, most systems change the shell prompt to end with #. The root account is allowed to do anything (delete or change any file) so the # is a reminder of the power of the prompt. Avoid using the # prompt when necessary; see the chapter on Becoming Root. In this book, root shell prompts look like this:

#

The basics of using the shell[edit]

Arguments and Options[edit]

When you enter a command, the shell does a few things in this order (if it succeeds, it executes the found command):

  1. The shell checks if the command is an absolute path (such as /bin/ls) and if that path is an executable file.
  2. If the command is not an absolute path, the shell:
    1. searches through its builtin commands for the entered command.
    2. Looks in the directories in enviornment variable PATH for the entered command. It starts its search with the first directory listed in PATH, then the second and so on.

For examples, we'll be using the command ls, which lists files and directories. This command lists the contents of /var (which may differ on your computer):

$ ls /var
account backups db      lib     msgs    run     tmp
audit   crash   empty   log     named   rwho    www
authpf  cron    games   mail    quotas  spool   yp

The first word, "ls", is the name of the program or command built into the shell to run. In this case, the program /bin/ls is run. The "/var" in this case is an argument; it tells ls what to list. Arguments are separated by whitespace, usually one space.

There are some special arguments called options. Each command decides what is an option, but for many commands, options with only one hyphen to start usually are short for a more descriptive option that starts with two hyphens and some that start with two hyphens don't have shorthands. Here is an example (read it as "el es dash el slash var"):

$ ls -l /var
drwxr-xr-x   2 root    wheel      512 Mar 20  2005 account
drwxrws---   2 root    wheel      512 Mar 20  2005 audit
drwxrwx---   2 root    authpf     512 Mar 20  2005 authpf
...
drwxr-xr-x   2 root    wheel      512 Jun 11 02:09 yp

Note that these commands require that options come before other arguments. For example, the following does not work (unless you have a file or directory called -l):

$ ls /var -l
ls: -l: No such file or directory
/var:
account backups db      lib     msgs    run     tmp
audit   crash   empty   log     named   rwho    www
authpf  cron    games   mail    quotas  spool   yp

Exception: on systems with the GNU C library (GNU and GNU/Linux), many programs (not all of them) will automatically treat the options as if they were in the front, so ls /var -l is the same as ls -l /var. This is nice for users who forget to type some option.

What if there really is a file called "-l"? Then it must be specified that "-l" is not an option. One does this using the "--" argument, which means "end of options". This is why it is inconvenient to have filenames start with hyphens.

$ ls -- -l
-l

Editing Commands[edit]

If the shell has command-line editing, then the arrow, End, and Home keys are useful. The left and right arrows allow the user to move the text cursor to edit the command and the Home and End keys allow the user to move the to the beginning or end of the line. For example, we want to list some specific files, like this:

$ ls -l /etc/passwd /etc/profile

But we typed:

$ ls l- /etc/passwrd /etc/profile

We can press the Home key to move the cursor to the beginning of the line, then use the right arrow to move the cursor to the right to delete the "-l". After this, we press Return to run the command as normal. (This will not work in many non-shell programs that lack command line editing!)

Shells with history features allow using the up arrow to recall previous commands to the shell prompt. These previous lines are not run again unless the user presses Return. The down arrow returns down the list.

If the arrow keys are broken, or you actually find some keyboard without arrows, then the Control Emacs navigation keys (Ctrl+B, Ctrl+F, Ctrl+P, Ctrl+N, Ctrl+E and Ctrl+A) also work in most shells.

The current/working directory[edit]

Use cd to change the directory you are "in." The syntax is cd followed by the pathname. cd bin would take you to the bin directory (located in the directory you are currently in), cd .. would take you up one level. cd ~ would take you to your home directory, and cd ~ followed by a username would take you to that user's home.

Although many shell prompts have the current directory's name or the end of the current directory's name right in the prompt (like '[user@localhost ~]$'), you can use the command pwd to Print the Working Directory.

$ cd /etc
$ pwd
/etc
$ ls passwd profile
passwd profile
$ cd ~john
$ pwd
/home/john

Finding help for commands[edit]

The first word of the command is the name of the command. For example, in the following command, "ls" is the command name, and "-l", "/etc/passwd", and "/etc/profile" are the arguments.

$ ls -l /etc/passwd /etc/profile

But how do we know what the "ls" command does? Most Unix-like systems provide online manual pages for each command. For example,

$ man ls

This opens the manual page in a program called the pager. The most common pagers are less and more. These let the user type <SPACE BAR> to scroll down a screenful, 'b' to scroll up a screenful, <RETURN> to scroll down by one line, and 'q' to quit the pager.

However, the manual pages are often not useful for persons who know almost nothing about the commands. The chapter on Commands will help. The section Guide to UNIX/Commands/Getting Help contains strategies for how to use man and the other help tools effectively.

Continue[edit]

  1. Shell Prompt
  2. Quoting and Filename Expansion
  3. Pipes and Job Control