Fedora And Red Hat System Administration/Shell Basics

From Wikibooks, open books for an open world
Jump to navigation Jump to search

From the moment you connect to an SSH session, log into a text console, or open a terminal from a graphical environment, you are interacting with a shell program. To describe it simply, the shell provides your command line. It receives the commands you type and runs programs as needed to service those commands.

Bash, the most popular shell for GNU/Linux systems, is built on ideas going back to the original UNIX shell, the Borne Shell. While the Borne shell was revolutionary for its day, it is rather clunky by today's standards. Concepts like command line editing, command history and job control evolved later in other shells that sought to replace the original Borne shell such as the C shell and the Korn Shell. While these features greatly enhanced shell usability, they also came at the price of breaking compatibility with Borne shell syntax. When the GNU Project designed their shell they chose to implement these new ideas but still maintain compatibility with the original Borne shell. The called their shell the Borne Again Shell, or simply Bash.

The basic ideas for the shell differ little from the original, but command history, commandline editing, and features used in shell scripts are more idiosyncratic.

Running commands from the shell[edit | edit source]

Commands at the shell take a basic form of:

COMMAND [-OPTIONS]... [ARG]...

(It is common convention to put variables in capitols and square brackets around optional elements. The ellipsis indicates the preceding can be repeated).

The COMMAND can be one of four things:

  • A Program - The shell will search your PATH environment variable for programs named COMMAND. Most programs are in /bin or /usr/bin. Documentation for commands are usually found in manual pages (Accessed with `man COMMAND')
  • A Shell Built-in - Built-in commands are commands processed directly by the shell such as cd, echo and alias. Documentation for built-ins can be found in the manual page for bash or with the help built-in: `help COMMAND'.
  • An alias - Aliases are processed before the command is run and are replaced by another command, possibly adding options or arguments.
  • A Function - Functions call a sequence of commands and are more often used in shell scripting than on a typical command line.

Example Built-in Commands[edit | edit source]

Some of the simplest commands are Built-ins. echo for example simply prints the arguments passed to it:

[jtk@athena ~]$ echo Hello, World!
Hello, World!

The pwd built-in prints the working directory; cd changes the working directory. (We'll define "working directory" later when we discuss filesystem basics).

[jtk@athena ~]$ pwd
/home/jtk
[jtk@athena ~]$ cd /usr/share/doc/bash-3.1/
[jtk@athena bash-3.1]$ pwd
/usr/share/doc/bash-3.1
[jtk@athena bash-3.1]$ cd
[jtk@athena ~]$ pwd
/home/jtk

The help built-in provides documentation on built-ins. Without arguments it gives a list of built-ins. With Arguments, help can provide details on a particular built-in command:

[jtk@athena ~]$ help
GNU bash, version 3.1.7(1)-release (i386-redhat-linux-gnu)
These shell commands are defined internally.  Type `help' to see this list.
Type `help name' to find out more about the function `name'.
Use `info bash' to find out more about the shell in general.
Use `man -k' or `info' to find out more about commands not in this list.

A star (*) next to a name means that the command is disabled.

 JOB_SPEC [&]                       (( expression ))
 . filename [arguments]             :
 [ arg... ]                         (( expression ))
 alias [-p] [name[=value] ... ]     bg [job_spec ...]
 bind [-lpvsPVS] [-m keymap] [-f fi break [n]
 builtin [shell-builtin [arg ...]]  caller [EXPR]
 case WORD in [PATTERN [| PATTERN]. cd [-L|-P] [dir]
 command [-pVv] command [arg ...]   compgen [-abcdefgjksuv] [-o option
 complete [-abcdefgjksuv] [-pr] [-o continue [n]
 declare [-afFirtx] [-p] [name[=val dirs [-clpv] [+N] [-N]
 disown [-h] [-ar] [jobspec ...]    echo [-neE] [arg ...]
 enable [-pnds] [-a] [-f filename]  eval [arg ...]
 exec [-cl] [-a name] file [redirec exit [n]
 export [-nf] [name[=value] ...] or false
 fc [-e ename] [-nlr] [first] [last fg [job_spec]
 for NAME [in WORDS ... ;] do COMMA for (( exp1; exp2; exp3 )); do COM
 function NAME { COMMANDS ; } or NA getopts optstring name [arg]
 hash [-lr] [-p pathname] [-dt] [na help [-s] [pattern ...]
 history [-c] [-d offset] [n] or hi if COMMANDS; then COMMANDS; [ elif
 jobs [-lnprs] [jobspec ...] or job kill [-s sigspec | -n signum | -si
 let arg [arg ...]                  local name[=value] ...
 logout                             popd [+N | -N] [-n]
 printf [-v var] format [arguments] pushd [dir | +N | -N] [-n]
 pwd [-LP]                          read [-ers] [-u fd] [-t timeout] [
 readonly [-af] [name[=value] ...]  return [n]
 select NAME [in WORDS ... ;] do CO set [--abefhkmnptuvxBCHP] [-o opti
 shift [n]                          shopt [-pqsu] [-o long-option] opt
 source filename [arguments]        suspend [-f]
 test [expr]                        time [-p] PIPELINE
 times                              trap [-lp] [arg signal_spec ...]
 true                               type [-afptP] name [name ...]
 typeset [-afFirtx] [-p] name[=valu ulimit [-SHacdfilmnpqstuvx] [limit
 umask [-p] [-S] [mode]             unalias [-a] name [name ...]
 unset [-f] [-v] [name ...]         until COMMANDS; do COMMANDS; done
 variables - Some variable names an wait [n]
 while COMMANDS; do COMMANDS; done  { COMMANDS ; }
[jtk@athena ~]$ help help
help: help [-s] [pattern ...]
     Display helpful information about builtin commands.  If PATTERN is
    specified, gives detailed help on all commands matching PATTERN,
    otherwise a list of the builtins is printed.  The -s option
    restricts the output for each builtin command matching PATTERN to
    a short usage synopsis.
[jtk@athena ~]$ help cd
cd: cd [-L|-P] [dir]
     Change the current directory to DIR.  The variable $HOME is the
    default DIR.  The variable CDPATH defines the search path for
    the directory containing DIR.  Alternative directory names in CDPATH
    are separated by a colon (:).  A null directory name is the same as
    the current directory, i.e. `.'.  If DIR begins with a slash (/),
    then CDPATH is not used.  If the directory is not found, and the
    shell option `cdable_vars' is set, then try the word as a variable
    name.  If that variable has a value, then cd to the value of that
    variable.  The -P option says to use the physical directory structure
    instead of following symbolic links; the -L option forces symbolic links

Example Program Commands[edit | edit source]

Most commands you'll use are programs. That is, they are a separate program run in a separate process from the bash shell. These programs can be found in a number of locations on the system. The directories searched are specified in the PATH environment variable, but more on that later. Most programs are found in either the /bin or /usr/bin directories and we'll focus on those for now.

If I want to get a listing of files in a directory I would use the ls command. The actual program that runs when you type "ls" is found at /bin/ls. If called without arguments, ls will give a listing of files in the current directory. An argument can be used to tell it which directory to list. Options can be used to tell it how to list the files:

[jtk@athena ~]$ ls
bin   Desktop   google-earth  svn
[jtk@athena ~]$ ls -l
drwxrwxr-x 2 jtk jtk      4096 Dec  1 12:32 bin
drwxr-xr-x 8 jtk jtk      4096 Jan 11 13:51 Desktop
drwxr-xr-x 9 jtk jtk      4096 Dec 17 22:21 google-earth
-rw-r--r-- 1 jtk jtk  16791707 Dec  5 16:43 livejournal.tar.bz2
drwxrwxr-x 2 jtk jtk      4096 Dec  8 13:23 svn
[jtk@athena ~]$ ls -lt
drwxrwxr-x 2 jtk jtk      4096 Dec  1 12:32 bin
-rw-r--r-- 1 jtk jtk  16791707 Dec  5 16:43 livejournal.tar.bz2
drwxrwxr-x 2 jtk jtk      4096 Dec  8 13:23 svn
drwxr-xr-x 9 jtk jtk      4096 Dec 17 22:21 google-earth
drwxr-xr-x 8 jtk jtk      4096 Jan 11 13:51 Desktop
[jtk@athena ~]$ ls /home/
db2inst1  jtk  lost+found
[jtk@athena ~]$ ls -l /home/
total 24
drwxr-xr-x  7 db2inst1 db2iadm1  4096 Jul 17  2006 db2inst1
drwx------ 11 jtk      jtk       4096 Feb  2 11:47 jtk
drwx------  2 root     root     16384 Apr 19  2006 lost+found

Notice the option string "-lt". The "l" specifies a "long" listing while the "t" specifies that the entries should be sorted by modification time. These are single character options and could have been specified separately as "-l -t", but they are usually grouped together like this.

It is customary to put options before arguments such as in the command "ls -l /home". With GNU utilities this is optional, but some utilities will complain if any options appear after the same argument. (There is no distinction between option and argument to the shell or the operating system. When the command is executed by the kernel, they are all arguments and it is up to the program to interpret them).

Example Running Command as Root User[edit | edit source]

If you are a user which is not root and you need to install or run a command which requires admin credentials then just do the following:

[jtk@athena ~]$ su root
Enter root password
Run the command you want (as root).

It's that's easy.

If you are a user and want to list the IP address of the machine you can use sudo command, for now lets imagine you are allowed to use sudo command

# sudo /sbin/ifconfig 

Similarly you can use sudo to run any superuser command