old-www/LDP/www.debian.org/doc/manuals/debian-tutorial/ch-shell.html

1025 lines
39 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<title>Debian Tutorial (Obsolete Documentation) - Using the shell</title>
<link href="index.html" rel="start">
<link href="ch-docs.html" rel="prev">
<link href="ch-files.html" rel="next">
<link href="index.html#contents" rel="contents">
<link href="index.html#copyright" rel="copyright">
<link href="ch-about.html" rel="chapter" title="1 About this manual">
<link href="ch-introduction.html" rel="chapter" title="2 Introduction">
<link href="ch-start.html" rel="chapter" title="3 Getting started">
<link href="ch-basics.html" rel="chapter" title="4 The Basics">
<link href="ch-docs.html" rel="chapter" title="5 Reading documentation and getting help">
<link href="ch-shell.html" rel="chapter" title="6 Using the shell">
<link href="ch-files.html" rel="chapter" title="7 More on files">
<link href="ch-editor.html" rel="chapter" title="8 Creating and editing text files">
<link href="ch-custom_shell.html" rel="chapter" title="9 Customizing the shell">
<link href="ch-X.html" rel="chapter" title="10 The X Window System">
<link href="ch-text_tools.html" rel="chapter" title="11 Text tools">
<link href="ch-file_tools.html" rel="chapter" title="12 File tools">
<link href="ch-disks.html" rel="chapter" title="13 Using disks">
<link href="ch-dpkg.html" rel="chapter" title="14 Removing and installing software">
<link href="ch-troubleshooting.html" rel="chapter" title="15 Troubleshooting">
<link href="ch-advanced.html" rel="chapter" title="16 Advanced topics">
<link href="ch-next.html" rel="chapter" title="17 Where to from here?">
<link href="ch-helping.html" rel="chapter" title="18 Contributing to Debian: How can I help?">
<link href="ap-apps_appendix.html" rel="appendix" title="A A brief survey of available applications">
<link href="ap-components_appendix.html" rel="appendix" title="B Summary of GNU/Linux system components">
<link href="ap-booting_appendix.html" rel="appendix" title="C Appendix C: Booting the system">
<link href="ap-misc_appendix.html" rel="appendix" title="D Miscellaneous">
<link href="ch-about.html#s-acknowldgements" rel="section" title="1.1 Acknowledgements">
<link href="ch-introduction.html#s-introduction-debian" rel="section" title="2.1 What is Debian?">
<link href="ch-introduction.html#s-introduction-DFSG" rel="section" title="2.2 What's free software?">
<link href="ch-introduction.html#s-introduction-how" rel="section" title="2.3 How to Read This Book">
<link href="ch-introduction.html#s-introduction-ldp" rel="section" title="2.4 The Linux Documentation Project">
<link href="ch-start.html#s-start-multi" rel="section" title="3.1 A multiuser, multitasking operating system">
<link href="ch-start.html#s-start-login" rel="section" title="3.2 Logging in">
<link href="ch-start.html#s-start-keys" rel="section" title="3.3 Keys">
<link href="ch-start.html#s-start-commandline" rel="section" title="3.4 Command history and editing the command line">
<link href="ch-start.html#s-start-root" rel="section" title="3.5 Logging in as root">
<link href="ch-start.html#s-start-VC" rel="section" title="3.6 Virtual consoles">
<link href="ch-start.html#s-start-shutdown" rel="section" title="3.7 Shutting down">
<link href="ch-basics.html#s-basics-commandline" rel="section" title="4.1 The command line and <samp>man</samp> pages">
<link href="ch-basics.html#s-basics-files" rel="section" title="4.2 Files and Directories">
<link href="ch-basics.html#s-basics-processes" rel="section" title="4.3 Processes">
<link href="ch-basics.html#s-basics-shell" rel="section" title="4.4 The shell">
<link href="ch-basics.html#s-basics-jobs" rel="section" title="4.5 Managing processes with Bash">
<link href="ch-basics.html#s-basics-bash" rel="section" title="4.6 A few Bash features">
<link href="ch-basics.html#s-basics-identity" rel="section" title="4.7 Managing your identity">
<link href="ch-docs.html#s-docs-sources" rel="section" title="5.1 Kinds of documentation">
<link href="ch-docs.html#s-docs-info" rel="section" title="5.2 Using info">
<link href="ch-docs.html#s-docs-textfiles" rel="section" title="5.3 Viewing text files with more and less">
<link href="ch-docs.html#s-docs-howtos" rel="section" title="5.4 HOWTOs">
<link href="ch-docs.html#s-docs-support" rel="section" title="5.5 Getting help from a person">
<link href="ch-docs.html#s-docs-sysinfo" rel="section" title="5.6 Getting information from the system">
<link href="ch-shell.html#s-shell-variables" rel="section" title="6.1 Environment variables">
<link href="ch-shell.html#s-shell-path" rel="section" title="6.2 Where commands live: the <samp>PATH</samp> variable">
<link href="ch-shell.html#s-custom_shell-aliases" rel="section" title="6.3 Aliases and shell functions">
<link href="ch-shell.html#s-shell-io" rel="section" title="6.4 Controlling input and output">
<link href="ch-shell.html#s-shell-modifiers" rel="section" title="6.5 Specifying how and when to run commands">
<link href="ch-shell.html#s-shell-wildcards" rel="section" title="6.6 Filename expansion (&quot;Wildcards&quot;)">
<link href="ch-shell.html#s6.7" rel="section" title="6.7 Interactive/non-interactive">
<link href="ch-files.html#s-files-permissions" rel="section" title="7.1 Permissions">
<link href="ch-files.html#s-files-tour" rel="section" title="7.2 What files are on my system? Where can I put my own files?">
<link href="ch-files.html#s-file-manager" rel="section" title="7.3 Using a filemanager">
<link href="ch-editor.html#s-editor-textfile" rel="section" title="8.1 What's a text file?">
<link href="ch-editor.html#s-editor-editor" rel="section" title="8.2 Text editors">
<link href="ch-editor.html#s-editor-vi" rel="section" title="8.3 Creating and editing a text file with <code>vi</code>">
<link href="ch-custom_shell.html#s-custom_shell-dotfiles" rel="section" title="9.1 .rc files and <samp>ls -a</samp>">
<link href="ch-custom_shell.html#s-custom_shell-etc" rel="section" title="9.2 System-wide vs. user-specific configuration">
<link href="ch-X.html#s-X-starting" rel="section" title="10.1 Starting the X environment">
<link href="ch-X.html#s-X-intro" rel="section" title="10.2 Intro: What is X">
<link href="ch-X.html#s-X-basics" rel="section" title="10.3 Basic X operations">
<link href="ch-X.html#s-X-startup" rel="section" title="10.4 Customizing your X startup">
<link href="ch-text_tools.html#s-text_tools-regexps" rel="section" title="11.1 Regular expressions">
<link href="ch-file_tools.html#s-file_tools-backup" rel="section" title="12.1 Backup tools">
<link href="ch-file_tools.html#s-file_tools-compression" rel="section" title="12.2 File compression with <samp>gzip</samp>">
<link href="ch-file_tools.html#s-file_tools-split" rel="section" title="12.3 Splitting files into smaller pieces">
<link href="ch-file_tools.html#s-file_tools-find" rel="section" title="12.4 Finding files">
<link href="ch-file_tools.html#s-file_tools-file" rel="section" title="12.5 Determining a file's contents">
<link href="ch-disks.html#s-disks-concepts" rel="section" title="13.1 Concepts">
<link href="ch-disks.html#s-disks-mount" rel="section" title="13.2 <samp>mount</samp> and <samp>/etc/fstab</samp>">
<link href="ch-disks.html#s-network-ppp" rel="section" title="13.3 PPP">
<link href="ch-dpkg.html#s-dpkg-dpkg" rel="section" title="14.1 The <code>dpkg</code> package utility">
<link href="ch-dpkg.html#s-dpkg-what" rel="section" title="14.2 What a package maintenance utility does">
<link href="ch-dpkg.html#s-dpkg-apt" rel="section" title="14.3 Apt">
<link href="ch-dpkg.html#s-dpkg-dselect" rel="section" title="14.4 Using dselect.">
<link href="ch-dpkg.html#s-dpkg-commandline" rel="section" title="14.5 Using dpkg manually">
<link href="ch-dpkg.html#s-dpkg-compiling" rel="section" title="14.6 Compiling software yourself">
<link href="ch-dpkg.html#s-dpkg-proprietary" rel="section" title="14.7 Proprietary software not in .deb format">
<link href="ch-troubleshooting.html#s-troubleshooting-frozen" rel="section" title="15.1 Debian is frozen or crashed!">
<link href="ch-troubleshooting.html#s-troubleshooting-termgarbage" rel="section" title="15.2 My terminal isn't behaving properly">
<link href="ch-troubleshooting.html#s-troubleshooting-vi" rel="section" title="15.3 The computer beeps when I press a key, and my screen shows a text file and/or a lot of <samp>~</samp> symbols.">
<link href="ch-advanced.html#s-advanced-scripting" rel="section" title="16.1 Introduction to shell scripting">
<link href="ch-advanced.html#s-advanced-files" rel="section" title="16.2 Advanced files">
<link href="ch-advanced.html#s-advanced-kernel" rel="section" title="16.3 Compiling the kernel">
<link href="ch-advanced.html#s-advanced-security" rel="section" title="16.4 A few words on security">
<link href="ch-advanced.html#s-advanced-programming" rel="section" title="16.5 Programming on Linux">
<link href="ch-next.html#s-next-manuals" rel="section" title="17.1 Other Debian manuals to read">
<link href="ch-next.html#s-next-resources" rel="section" title="17.2 Other resources">
<link href="ch-helping.html#s-helping-bugs" rel="section" title="18.1 Submit bug reports">
<link href="ch-helping.html#s-helping-other" rel="section" title="18.2 Other things">
<link href="ap-apps_appendix.html#s-A_apps-whatever" rel="section" title="A.1 (Subheadings could copy the structure of the menu system)">
<link href="ap-misc_appendix.html#s-unix-history" rel="section" title="D.1 Unix History">
<link href="ap-misc_appendix.html#sD.2" rel="section" title="D.2 GNU/Linux History">
<link href="ap-misc_appendix.html#sD.3" rel="section" title="D.3 The Linux kernel's version numbering">
<link href="ch-introduction.html#s-introduction-debian-os" rel="subsection" title="2.1.1 What's an operating system, and what sort of operating system is Debian?">
<link href="ch-introduction.html#s-introduction-debian-how" rel="subsection" title="2.1.2 Who creates Debian?">
<link href="ch-basics.html#s-basics-commandline-description" rel="subsection" title="4.1.1 Describing the command line">
<link href="ch-basics.html#s-basics-files-intro" rel="subsection" title="4.2.1 Introduction to files">
<link href="ch-basics.html#s-basics-files-using" rel="subsection" title="4.2.2 Using files: a tutorial">
<link href="ch-basics.html#s-basics-bash-completion" rel="subsection" title="4.6.1 Tab Completion">
<link href="ch-docs.html#s-docs-support-posting" rel="subsection" title="5.5.1 Dos and Don'ts of asking a question">
<link href="ch-shell.html#s-shell-variables-parents" rel="subsection" title="6.1.1 Parent and child processes">
<link href="ch-shell.html#s-interactive-shells" rel="subsection" title="6.7.1 Interactive shells">
<link href="ch-files.html#s-files-permissions-groups" rel="subsection" title="7.1.1 File Ownership">
<link href="ch-files.html#s-files-permissions-mode" rel="subsection" title="7.1.2 Mode">
<link href="ch-files.html#s-files-permissions-tutorial" rel="subsection" title="7.1.3 Permissions in practice">
<link href="ch-editor.html#s-editor-vi-creating" rel="subsection" title="8.3.1 Creating a file">
<link href="ch-editor.html#s-editor-vi-editing" rel="subsection" title="8.3.2 Editing an existing file">
<link href="ch-editor.html#s-editor-vi-editing-movement" rel="subsection" title="8.3.2.1 Moving around in a file">
<link href="ch-editor.html#s-editor-vi-editing-deletion" rel="subsection" title="8.3.2.2 Deleting text">
<link href="ch-editor.html#s-editor-vi-editing-moremoving" rel="subsection" title="8.3.2.3 Sophisticated movement">
<link href="ch-editor.html#s-editor-vi-editing-numbers" rel="subsection" title="8.3.2.4 Repeating commands">
<link href="ch-editor.html#s-editor-vi-editing-advanced" rel="subsection" title="8.3.2.5 Advanced reference">
<link href="ch-X.html#s-X-basics-mouse" rel="subsection" title="10.3.1 The mouse">
<link href="ch-X.html#s-X-basics-clients" rel="subsection" title="10.3.2 X clients">
<link href="ch-X.html#s-X-basics-trouble" rel="subsection" title="10.3.3 Troubleshooting">
<link href="ch-X.html#s-X-basics-leaving" rel="subsection" title="10.3.4 Leaving the X environment">
<link href="ch-disks.html#s-disks-mount-mounting" rel="subsection" title="13.2.1 Mounting a filesystem">
<link href="ch-disks.html#s-disks-mount-cdromexample" rel="subsection" title="13.2.2 Example: Mounting a CD-ROM">
<link href="ch-disks.html#s-disks-mount-fstab" rel="subsection" title="13.2.3 <samp>/etc/fstab</samp>: Automating the mount process">
<link href="ch-disks.html#s-disks-mount-removable" rel="subsection" title="13.2.4 Removable disks (floppies, Zip disks, etc.)">
<link href="ch-disks.html#s-network-ppp-introduction" rel="subsection" title="13.3.1 Introduction">
<link href="ch-disks.html#s-network-ppp-preparation" rel="subsection" title="13.3.2 Preparation">
<link href="ch-disks.html#s-network-ppp-wvdial" rel="subsection" title="13.3.3 The Easy Way: <samp>wvdial</samp>">
<link href="ch-disks.html#s-network-ppp-pppd" rel="subsection" title="13.3.4 Doing It Manually">
<link href="ch-dpkg.html#s-apt-config" rel="subsection" title="14.3.1 Configuring Apt">
<link href="ch-dpkg.html#s-apt-usage" rel="subsection" title="14.3.2 Using apt-get.">
<link href="ch-advanced.html#s-advanced-scripting-why" rel="subsection" title="16.1.1 What and why">
<link href="ch-advanced.html#s-advanced-scripting-example" rel="subsection" title="16.1.2 A simple example">
<link href="ch-advanced.html#s-advanced-files-hardlinks" rel="subsection" title="16.2.1 The real nature of files: hard links and inodes">
<link href="ch-advanced.html#s-advanced-files-types" rel="subsection" title="16.2.2 Types of files">
<link href="ch-advanced.html#s-advanced-files-types-symlinks" rel="subsection" title="16.2.2.1 Symbolic links">
<link href="ch-advanced.html#s-files-misc-types-devices" rel="subsection" title="16.2.2.2 Device files">
<link href="ch-advanced.html#s-advanced-files-types-devices-null" rel="subsection" title="16.2.2.2.1 <samp>/dev/null</samp>">
<link href="ch-advanced.html#s-advanced-files-types-pipes" rel="subsection" title="16.2.2.3 Named pipes (FIFOs)">
<link href="ch-advanced.html#s-advanced-files-types-sockets" rel="subsection" title="16.2.2.4 Sockets">
<link href="ch-advanced.html#s-advanced-files-proc" rel="subsection" title="16.2.3 The <samp>proc</samp> filesystem">
<link href="ch-advanced.html#s-advanced-files-permissions" rel="subsection" title="16.2.4 Advanced aspects of file permissions">
<link href="ch-advanced.html#s-advanced-files-permissions-absolute" rel="subsection" title="16.2.4.1 Using numeric arguments with <samp>chmod</samp>">
<link href="ch-advanced.html#s-advanced-files-chattr" rel="subsection" title="16.2.5 chattr">
<link href="ch-advanced.html#s-advanced-files-copying" rel="subsection" title="16.2.6 Large-scale copying">
<link href="ch-advanced.html#s-advanced-files-undocumented" rel="subsection" title="16.2.7 Other concepts not yet covered, but should be">
</head>
<body>
<p><a name="ch-shell"></a></p>
<hr>
<p>
[ <a href="ch-docs.html">previous</a> ]
[ <a href="index.html#contents">Contents</a> ]
[ <a href="ch-about.html">1</a> ]
[ <a href="ch-introduction.html">2</a> ]
[ <a href="ch-start.html">3</a> ]
[ <a href="ch-basics.html">4</a> ]
[ <a href="ch-docs.html">5</a> ]
[ 6 ]
[ <a href="ch-files.html">7</a> ]
[ <a href="ch-editor.html">8</a> ]
[ <a href="ch-custom_shell.html">9</a> ]
[ <a href="ch-X.html">10</a> ]
[ <a href="ch-text_tools.html">11</a> ]
[ <a href="ch-file_tools.html">12</a> ]
[ <a href="ch-disks.html">13</a> ]
[ <a href="ch-dpkg.html">14</a> ]
[ <a href="ch-troubleshooting.html">15</a> ]
[ <a href="ch-advanced.html">16</a> ]
[ <a href="ch-next.html">17</a> ]
[ <a href="ch-helping.html">18</a> ]
[ <a href="ap-apps_appendix.html">A</a> ]
[ <a href="ap-components_appendix.html">B</a> ]
[ <a href="ap-booting_appendix.html">C</a> ]
[ <a href="ap-misc_appendix.html">D</a> ]
[ <a href="ch-files.html">next</a> ]
</p>
<hr>
<h1>
Debian Tutorial (Obsolete Documentation)
<br>Chapter 6 - Using the shell
</h1>
<hr>
<h2><a name="s-shell-variables"></a>6.1 Environment variables</h2>
<p>
Every process has an <em>environment</em> associated with it. An environment
is a collection of <em>environment variables</em>. A variable is a changeable
value with a fixed name. For example, the name <samp>EMAIL</samp> could refer
to the value <samp>joe@nowhere.com</samp>. The value can vary ---
<samp>EMAIL</samp> could also refer to <samp>jane@somewhere.com</samp>.
</p>
<p>
Since your shell is a process like any other, it has an environment. You can
view your shell's environment by entering the <samp>printenv</samp> command.
Here's some example output:
</p>
<pre>
PAGER=less
HOSTNAME=icon
MAILCHECK=60
MOZILLA_HOME=/usr/local/lib/netscape
PS1=$
USER=hp
MACHTYPE=i486-pc-linux-gnu
EDITOR=jed
DISPLAY=:0.0
LOGNAME=hp
EMAIL=hp@pobox.com
SHELL=/bin/bash
HOSTTYPE=i486
OSTYPE=linux-gnu
HISTSIZE=150
HOME=/home/hp
TERM=xterm-debian
TEXEDIT=jed
PATH=/home/hp/local/bin:/usr/sbin:/home/hp/.bin:/home/hp/local/bin:/usr/sbin:/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games:.
_=/usr/bin/printenv
</pre>
<p>
On your system, the output will be different, but similar.
</p>
<p>
Environment variables are one way to configure the system. For example, the
<samp>EDITOR</samp> variable lets you select your preferred editor for posting
news, writing email, and so on. The <samp>HISTSIZE</samp> variable tells Bash
how many command lines to keep in its history; you can return to that many
command lines with the up arrow key.
</p>
<p>
Setting environment variables is simple. Once you learn how, you'll probably
want to set them automatically whenever you log on; see <a
href="ch-custom_shell.html">Customizing the shell, Chapter 9</a> for
instructions.
</p>
<p>
For practice, try customizing your shell's prompt and your text file viewer
with environment variables:
</p>
<ol type="1" start="1" >
<li>
<p>
<samp>man less</samp>
</p>
<p>
View the online manual for the <samp>less</samp> command. In order to show you
the text one screenful at a time, <samp>man</samp> invokes a <em>pager</em>
which shows you a new page of text each time you press the space bar. By
default, it uses the pager called <samp>more</samp>.
</p>
<p>
Go ahead and glance over the man page for <samp>less</samp>, which is an
enhanced pager. Scroll to a new page by pressing space; press <samp>q</samp>
to quit. <samp>more</samp> will also quit automatically when you reach the end
of the man page.
</p>
</li>
</ol>
<ol type="1" start="2" >
<li>
<p>
<samp>export PAGER=less</samp>
</p>
<p>
After reading about the advantages of <samp>less</samp>, you might want to use
it to read man pages. To do this, you set the environment variable
<samp>PAGER</samp>.
</p>
<p>
The command to set an environment variable within bash always has this format:
<samp>export <var>NAME</var>=<var>value</var></samp>. If you happen to run
<samp>tcsh</samp> or another C Shell derivative, the equivalent command is
<samp>setenv <var>NAME</var> <var>value</var></samp>.
</p>
<p>
<samp>export</samp> means to move the variable from the shell into the
environment. This means that programs other than the shell will be able to
access it.
</p>
</li>
</ol>
<ol type="1" start="3" >
<li>
<p>
<samp>echo <var>$PAGER</var></samp>
</p>
<p>
This is the easiest way to see the value of a variable. <samp>$PAGER</samp>
tells the shell to insert the value of the <samp>PAGER</samp> variable
<em>before</em> invoking the command. <samp>echo</samp> echoes back its
argument: in this case, the it echoes the current <samp>PAGER</samp> value,
<samp>less</samp>.
</p>
</li>
</ol>
<ol type="1" start="4" >
<li>
<p>
<samp>man more</samp>
</p>
<p>
Read the <samp>more</samp> manual. This time, <samp>man</samp> should have
invoked the <samp>less</samp> pager.
</p>
<p>
<samp>less</samp> has lots of features <samp>more</samp> lacks. For example,
you can scroll backward with the <samp>b</samp> key. You can also move up and
down (even sideways) with the arrow keys. <samp>less</samp> won't exit when it
reaches the end of the man page; it will wait for you to press <samp>q</samp>.
</p>
</li>
</ol>
<ol type="1" start="5" >
<li>
<p>
<samp>PAGER=more man more</samp>
</p>
<p>
If you want a different setting temporarily, you can put a new value in effect
for the current command line only. Put the
<samp><var>NAME</var>=<var>value</var></samp> at the start of the command line,
followed by the command to execute. Be sure to omit <samp>export</samp>.
</p>
<p>
You can try out some <samp>less</samp>-specific commands, like <samp>b</samp>,
to verify that they don't work with <samp>more</samp> and you are indeed using
<samp>more</samp>.
</p>
</li>
</ol>
<ol type="1" start="6" >
<li>
<p>
<samp>echo $PAGER</samp>
</p>
<p>
The value of <samp>PAGER</samp> should still be <samp>less</samp>; the above
setting was only temporary.
</p>
</li>
</ol>
<ol type="1" start="7" >
<li>
<p>
<samp>unset PAGER</samp>
</p>
<p>
If you don't want to specify a pager anymore, you can <samp>unset</samp> the
variable. <samp>man</samp> will then use <samp>more</samp> by default, just as
it did before you set the variable.
</p>
</li>
</ol>
<ol type="1" start="8" >
<li>
<p>
<samp>echo $PAGER</samp>
</p>
<p>
Since <samp>PAGER</samp> has been unset, <samp>echo</samp> won't print
anything.
</p>
</li>
</ol>
<ol type="1" start="9" >
<li>
<p>
<samp>PS1=hello:</samp>
</p>
<p>
Just for fun, change your shell prompt. <samp>$</samp> should become
<samp>hello:</samp>.
</p>
<p>
<samp>export</samp> is not necessary, because we're changing the shell's own
behavior. There's no reason to export the variable into the environment for
other programs to see. Technically, <samp>PS1</samp> is a <em>shell
variable</em> rather than an environment variable.
</p>
<p>
If you wanted to, you could <samp>export</samp> the shell variable,
transforming it into an environment variable. Then other programs could see
it: specifically, the <em>children</em> of the current shell process. The next
section explains this.
</p>
</li>
</ol>
<hr>
<h3><a name="s-shell-variables-parents"></a>6.1.1 Parent and child processes</h3>
<p>
All processes come from an earlier process, called their <em>parent
process</em>. [<a href="footnotes.html#f10" name="fr10">10</a>] The
<code>ps</code> command is a useful tool for exploring processes, and it can be
used to examine parent-child relationships.
</p>
<ol type="1" start="1" >
<li>
<p>
<samp>ps f</samp>
</p>
<p>
This command asks to see a list of processes belonging to you, in a format that
shows how processes are related.
</p>
</li>
</ol>
<p>
<samp>ps f</samp> might produce output like this:
</p>
<pre>
$ ps f
PID TT STAT TIME
7270 p5 S 0:00 bash
15980 p5 R 0:00 \_ ps f
19682 p4 S 0:00 bash
15973 p4 S 0:00 \_ man ps
15976 p4 S 0:00 \_ sh -c /bin/gzip -dc '/var/catman/cat1/ps.1.gz' | { export MAN_PN LESS; MAN_PN='ps(1)'; LESS=&quot;$LESS\$-Pm\:\$i
15977 p4 S 0:00 \_ /bin/gzip -dc /var/catman/cat1/ps.1.gz
15978 p4 S 0:00 \_ sh -c /bin/gzip -dc '/var/catman/cat1/ps.1.gz' | { export MAN_PN LESS; MAN_PN='ps(1)'; LESS=&quot;$LESS\$-Pm\
15979 p4 S 0:00 \_ less
$
</pre>
<p>
Here you can see that I have a number of processes running, including two
shells. The shells have child processes: shell process 7270 has child process
15980 (<samp>ps f</samp>) and shell 19682 has child process 15973 (<samp>man
ps</samp>). <samp>man ps</samp> has in turn invoked a complex set of
subprocesses in order to display a man page. Don't worry about what these
subprocesses do for now.
</p>
<p>
Parents and children have a complex relationship. Most of the time, when a
parent dies the child will die as well. So you can kill a whole set of
processes --- for example, all the <samp>man ps</samp> children in the above
example --- by killing the parent process, 15973.
</p>
<p>
Children inherit the environment variables of their parents, and some other
attributes such as the current working directory.
</p>
<p>
When a shell runs a command, it spawns the command as a child process. So the
<samp>man</samp> command inherits the shell's environment; if you've set the
<samp>PAGER</samp> variable, <samp>man</samp> will be able to see it.
</p>
<p>
If you fail to <samp>export</samp> a variable, only the shell itself will see
it, and it will not be passed on to children such as <samp>man</samp>.
</p>
<hr>
<h2><a name="s-shell-path"></a>6.2 Where commands live: the <samp>PATH</samp> variable</h2>
<p>
When you type a command into the shell, it has to find the program on your hard
disk before executing it. If the shell had to look all over the disk, it would
be very slow; instead, it looks in a list of directories contained in the
<samp>PATH</samp> environment variable. This list of directories makes up the
shells' <em>search path</em>; when you enter a command, it goes through each
one in turn looking for the program you asked to run.
</p>
<p>
You may need to change the <samp>PATH</samp> variable if you install programs
yourself in a nonstandard location.
</p>
<p>
The value of <samp>PATH</samp> is a colon-separated list of directories. The
default value on Debian systems is:
</p>
<pre>
/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games
</pre>
<p>
This value is defined in the file <samp>/etc/profile</samp> and applies to all
users. You can easily change the value, just as you can change any environment
variable.
</p>
<p>
If you type the command <samp>ls</samp>, the shell will first look in
<samp>/usr/local/bin</samp>; <samp>ls</samp> isn't there, so it will try
<samp>/usr/bin</samp>; when that fails, it will check <samp>/bin</samp>. There
it will discover <samp>/bin/ls</samp>, stop its search, and execute the program
<samp>/bin/ls</samp>. If <samp>/usr/bin/X11/ls</samp> existed (it doesn't, but
pretend), it would be ignored.
</p>
<p>
You can see which <samp>ls</samp> the shell is going to use with the
<samp>type</samp> command. <samp>type ls</samp> will give you the answer
<samp>/bin/ls</samp> --- try it yourself.
</p>
<p>
Try asking where <samp>type</samp> itself resides:
</p>
<pre>
$ type type
type is a shell builtin
</pre>
<p>
<samp>type</samp> isn't actually a program; it's a function provided by the
shell. However, you use it just like an external program. [<a
href="footnotes.html#f11" name="fr11">11</a>]
</p>
<p>
There are a number of commands like this; type <samp>man builtins</samp> to
read the man page describing them. In general, you don't need to know whether
a command is a builtin or a real program; however, builtins will not show up in
the output of <samp>ps</samp> or <samp>top</samp> since they aren't separate
processes. They're just part of the shell.
</p>
<hr>
<h2><a name="s-custom_shell-aliases"></a>6.3 Aliases and shell functions</h2>
<p>
If you use the same command often, you might get tired of typing it.
<samp>bash</samp> lets you write shorter <em>aliases</em> for your commands.
You can also write <em>shell functions</em>, which are custom commands made up
of several other commands.
</p>
<p>
Say you always use the <samp>--almost-all</samp> and <samp>--color=auto</samp>
options to <samp>ls</samp>. You quickly get tired of typing <samp>ls
--almost-all --color=auto</samp>. So you make an alias:
</p>
<pre>
alias myls='ls --almost-all --color=auto'
</pre>
<p>
Now you can type <samp>myls</samp> instead of the full command. To see what
<samp>myls</samp> really is, run the command <samp>type myls</samp>. To see a
list of aliases you've defined, type simply <samp>alias</samp> on a line by
itself.
</p>
<p>
Shell functions are a little more flexible than aliases. An alias simply
substitutes a longer command when you type a shorter one. Functions let you
use a series of commands to perform some action.
</p>
<p>
First let's see how a shell function could be used in place of the above alias:
</p>
<pre>
myls() {
ls --almost-all --color=auto $*
}
</pre>
<p>
The above is called a <em>function definition</em>, because it gives a function
name (<samp>myls</samp>), then defines the meaning of the name (some commands
to execute). To define a function, write its name, followed by
<samp>()</samp>. Then include the commands to execute inside braces
(<samp>{}</samp>). The portion inside braces is known as the <samp>body</samp>
of the function.
</p>
<p>
The arguments to the function can be referred to as <samp>$*</samp>. So if you
type:
</p>
<pre>
myls /usr /etc
</pre>
<p>
<samp>$*</samp> will be <samp>/usr /etc</samp>, the two arguments. If there
are no arguments, <samp>$*</samp> will be empty.
</p>
<p>
You can also refer to the arguments by number. So <samp>$1</samp> in the
function body would be replaced by <samp>/usr</samp>, and <samp>$2</samp> would
be replaced by <samp>/etc</samp>. Type in this function (you can type it at
the shell prompt; hit return after each line):
</p>
<pre>
print_arguments() {
echo &quot;First argument: $1&quot;
echo &quot;Second argument: $2&quot;
echo &quot;All arguments: $*&quot;
}
</pre>
<p>
You can verify you entered the function definition correctly with the
<samp>type</samp> command; <samp>type print_arguments</samp> should say:
</p>
<pre>
print_arguments is a function
print_arguments ()
{
echo &quot;First argument: $1&quot;;
echo &quot;Second argument: $2&quot;;
echo &quot;All arguments: $*&quot;
}
</pre>
<p>
Try the function out. If you enter <samp>print_arguments one two</samp> it
will display:
</p>
<pre>
First argument: one
Second argument: two
All arguments: one two
</pre>
<p>
There are many more complex things you can do in a shell function; you're
limited only by your imagination. For more, see <a
href="ch-advanced.html#s-advanced-scripting">Introduction to shell scripting,
Section 16.1</a>.
</p>
<hr>
<h2><a name="s-shell-io"></a>6.4 Controlling input and output</h2>
<p>
Stdin, stdout, pipelines, and redirection
</p>
<p>
Every process has at least three connections to the outside world. The
<em>standard input</em> is one source of the process's data; the <em>standard
output</em> is one place the process sends data; and the <em>standard
error</em> is a place the process can send error messages. (These are often
abbreviated <samp>stdin</samp>, <samp>stdout</samp>, and <samp>stderr</samp>.)
</p>
<p>
The words `source' and `place' are intentionally vague. These standard input
and ouput locations can be changed by the user; they could be the screen, the
keyboard, a file, even a network connection. The user can specify which
locations to use.
</p>
<p>
When you run a program from the shell, usually standard input comes from your
keyboard and standard output and error both go to your screen. However, you
can ask the shell to change these defaults.
</p>
<p>
For example, the <samp>echo</samp> command sends it output to standard output,
normally the screen. But you can send it to a file instead, with the
<em>output redirection operator</em>, '<samp>&gt;</samp>'. For example, to put
the word &quot;Hello&quot; in the file <samp>myfile</samp>:
</p>
<pre>
echo Hello &gt; myfile
</pre>
<p>
Use <samp>cat</samp> or your text file pager (<samp>more</samp> or
<samp>less</samp>) to view <samp>myfile</samp>'s contents.
</p>
<p>
You can change the standard input of a command with the <em>input redirection
operator</em>, '<samp>&lt;</samp>'. For example, <samp>more &lt; myfile</samp>
will display the contents of <samp>myfile</samp>. This is not useful in
practice; for convenience, the <samp>more</samp> command accepts a filename
argument. So you can simply say <samp>more myfile</samp> and the effect will
be the same.
</p>
<p>
Under the hood, <samp>more &lt; myfile</samp> means that the shell opens
<samp>myfile</samp>, then feeds its contents to the standard input of
<samp>more</samp>. <samp>more myfile</samp>, without the redirection operator,
means that the <samp>more</samp> command receives one argument,
<samp>myfile</samp>, opens the file itself, and then displays the file.
</p>
<p>
There's a reason for the double functionality, however. For example, you can
connect the standard output of one command to the standard input of another.
This is called a <em>pipeline</em>, and it uses the <em>pipe operator</em>,
'<samp>|</samp>'.
</p>
<p>
Perhaps you want to see the GNU General Public License in reverse. To do this,
you use the <samp>tac</samp> command (it's <samp>cat</samp>, only backward).
Try it out:
</p>
<pre>
tac /usr/doc/copyright/GPL
</pre>
<p>
Unfortunately, it goes by too quickly to read. So you only get to see a couple
of paragraphs. The solution is a pipeline:
</p>
<pre>
tac /usr/doc/copyright/GPL | more
</pre>
<p>
This takes the standard output of <samp>tac</samp>, which is the GPL in
reverse, and sends it to the standard input of <samp>more</samp>.
</p>
<p>
You can chain as many commands together as you like. Say you have an
inexplicable desire to replace every <samp>G</samp> with <samp>Q</samp>; for
this you use the command <samp>tr G Q</samp>, like this:
</p>
<pre>
tac /usr/doc/copyright/GPL | tr G Q | more
</pre>
<p>
You could get the same effect using temporary files and redirection. For
example:
</p>
<pre>
tac /usr/doc/copyright/GPL &gt; tmpfile
tr G Q &lt; tmpfile &gt; tmpfile2
more &lt; tmpfile2
rm tmpfile tmpfile2
</pre>
<p>
Clearly a pipeline is more convenient.
</p>
<hr>
<h2><a name="s-shell-modifiers"></a>6.5 Specifying how and when to run commands</h2>
<p>
&quot;modifiers&quot; like batch, at, nohup, nice
</p>
<hr>
<h2><a name="s-shell-wildcards"></a>6.6 Filename expansion (&quot;Wildcards&quot;)</h2>
<p>
Often you want a command to work with a group of files. &quot;Wildcards&quot;
are used to create a <em>filename expansion pattern</em>: a series of
characters and wildcards that expands to a list of filenames. For example, the
pattern <samp>/etc/*</samp> expands to a list of all the files in
<samp>/etc</samp> [<a href="footnotes.html#f12" name="fr12">12</a>].
<samp>*</samp> is a wildcard which can stand for any series of characters, so
the pattern <samp>/etc/*</samp> will expand to a list of all the filenames
beginning with <samp>/etc/</samp>.
</p>
<p>
This filename list is most useful as a set of arguments for a command. For
example, the <samp>/etc</samp> directory contains a series of subdirectories
called <samp>rc0.d</samp>, <samp>rc1.d</samp>, etc. Normally to view the
contents of these, you would type:
</p>
<pre>
ls /etc/rc0.d /etc/rc1.d /etc/rc2.d /etc/rc3.d /etc/rc4.d /etc/rc5.d /etc/rc6.d /etc/rcS.d
</pre>
<p>
This is tedious. Instead, you can use the <samp>?</samp> wildcard:
</p>
<pre>
ls /etc/rc?.d
</pre>
<p>
<samp>/etc/rc?.d</samp> expands to a list of filenames which begin with
<samp>rc</samp>, followed by any single character, followed by <samp>.d</samp>.
</p>
<p>
Available wildcards are:
</p>
<dl>
<dt><samp>*</samp></dt>
<dd>
<p>
Matches any group of 0 or more characters
</p>
</dd>
</dl>
<dl>
<dt><samp>?</samp></dt>
<dd>
<p>
Matches exactly one character
</p>
</dd>
</dl>
<dl>
<dt><samp>[...]</samp></dt>
<dd>
<p>
If you enclose some characters in brackets, the result is a wildcard which
matches those characters. For example, <samp>[abc]</samp> matches either a, or
b, or c. If you add a <samp>^</samp> after the first bracket, the sense is
reversed; so <samp>[^abc]</samp> matches any character that is not a, b, or c.
You can include a range, such as <samp>[a-j]</samp>, which matches anything
between a and j. The match is case sensitive, so to allow any letter, you must
use <samp>[a-zA-Z]</samp>.
</p>
</dd>
</dl>
<p>
Expansion patterns are simple, once you see some concrete examples:
</p>
<dl>
<dt><samp>*.txt</samp></dt>
<dd>
<p>
This will give you a list of any filename which ends in <samp>.txt</samp>,
since the <samp>*</samp> matches anything at all.
</p>
</dd>
</dl>
<dl>
<dt><samp>*.[hc]</samp></dt>
<dd>
<p>
This gives a list of filenames which end in either <samp>.h</samp> or
<samp>.c</samp>.
</p>
</dd>
</dl>
<dl>
<dt><samp>a??</samp></dt>
<dd>
<p>
This gives you all three-letter filenames that begin with <samp>a</samp>.
</p>
</dd>
</dl>
<dl>
<dt><samp>[^a]??</samp></dt>
<dd>
<p>
This gives you all three-letter filenames that do <em>not</em> begin with
<samp>a</samp>.
</p>
</dd>
</dl>
<dl>
<dt><samp>a*</samp></dt>
<dd>
<p>
This gives you every filename that starts with <samp>a</samp>, regardless of
how many letters there are.
</p>
</dd>
</dl>
<hr>
<h2><a name="s6.7"></a>6.7 Interactive/non-interactive</h2>
<p>
Bash has two different modes: <em>interactive</em> and
<em>non-interactive</em>. Interactive means you can type into it, and have it
do things for you. Non-interactive shells interpret shell scripts, similar to
DOS batch files. You give it a list of commands to carry out, and it goes and
does them, but without your intervention. You don't see all the commands being
typed in, Of course any output will be recorded somewhere (the standard output,
or stdout, normally the screeen or a log file). We will get more into
non-interactive shells a little later on.
</p>
<hr>
<h3><a name="s-interactive-shells"></a>6.7.1 Interactive shells</h3>
<p>
Interactive shells will take a very long time for one to master, just because
they're so powerful --- you'll probably never learn everything! There is just
so much out there that a shell can do, and of course it's always changing. We
will talk about <samp>bash</samp> here, and some basic commands that will make
your life with a shell easier. In bash, one can have several different things
going on all at once, and this can get confusing.
</p>
<p>
A shell is a Line Oriented or command line environment. The shell will always
prompt you with a prompt, whenever it is waiting on you to do things. The
default debian prompt is a $. At the $ prompt is where you can type in
commands to tell linux to do things, it can be a program name, or it can be a
&quot;builtin&quot; command that the shell provides for your convenience.
</p>
<hr>
<p>
[ <a href="ch-docs.html">previous</a> ]
[ <a href="index.html#contents">Contents</a> ]
[ <a href="ch-about.html">1</a> ]
[ <a href="ch-introduction.html">2</a> ]
[ <a href="ch-start.html">3</a> ]
[ <a href="ch-basics.html">4</a> ]
[ <a href="ch-docs.html">5</a> ]
[ 6 ]
[ <a href="ch-files.html">7</a> ]
[ <a href="ch-editor.html">8</a> ]
[ <a href="ch-custom_shell.html">9</a> ]
[ <a href="ch-X.html">10</a> ]
[ <a href="ch-text_tools.html">11</a> ]
[ <a href="ch-file_tools.html">12</a> ]
[ <a href="ch-disks.html">13</a> ]
[ <a href="ch-dpkg.html">14</a> ]
[ <a href="ch-troubleshooting.html">15</a> ]
[ <a href="ch-advanced.html">16</a> ]
[ <a href="ch-next.html">17</a> ]
[ <a href="ch-helping.html">18</a> ]
[ <a href="ap-apps_appendix.html">A</a> ]
[ <a href="ap-components_appendix.html">B</a> ]
[ <a href="ap-booting_appendix.html">C</a> ]
[ <a href="ap-misc_appendix.html">D</a> ]
[ <a href="ch-files.html">next</a> ]
</p>
<hr>
<p>
Debian Tutorial (Obsolete Documentation)
</p>
<address>
29 Dezember 2009<br>
<br>
Havoc Pennington <code><a href="mailto:hp@debian.org">hp@debian.org</a></code><br>
<br>
</address>
<hr>
</body>
</html>