forked from swcarpentry/shell-novice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reference.html
251 lines (249 loc) · 18.6 KB
/
reference.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="generator" content="pandoc">
<title>Software Carpentry: The Unix Shell</title>
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" type="text/css" href="css/bootstrap/bootstrap.css" />
<link rel="stylesheet" type="text/css" href="css/bootstrap/bootstrap-theme.css" />
<link rel="stylesheet" type="text/css" href="css/swc.css" />
<link rel="alternate" type="application/rss+xml" title="Software Carpentry Blog" href="http://software-carpentry.org/feed.xml"/>
<meta charset="UTF-8" />
<!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body class="lesson">
<div class="container card">
<div class="banner">
<a href="http://software-carpentry.org" title="Software Carpentry">
<img alt="Software Carpentry banner" src="img/software-carpentry-banner.png" />
</a>
</div>
<article>
<div class="row">
<div class="col-md-10 col-md-offset-1">
<a href="index.html"><h1 class="title">The Unix Shell</h1></a>
<h2 class="subtitle">Reference</h2>
<h2 id="introducing-the-shell"><a href="00-intro.html">Introducing the Shell</a></h2>
<ul>
<li>A shell is a program whose primary purpose is to read commands and run other programs.</li>
<li>The shell’s main advantages are its high action-to-keystroke ratio, its support for automating repetitive tasks, and that it can be used to access networked machines.</li>
<li>The shell’s main disadvantages are its primarily textual nature and how cryptic its commands and operation can be.</li>
</ul>
<h2 id="navigating-files-and-directories"><a href="01-filedir.html">Navigating Files and Directories</a></h2>
<ul>
<li>The file system is responsible for managing information on the disk.</li>
<li>Information is stored in files, which are stored in directories (folders).</li>
<li>Directories can also store other directories, which forms a directory tree.</li>
<li><code>cd path</code> changes the current working directory.</li>
<li><code>ls path</code> prints a listing of a specific file or directory; <code>ls</code> on its own lists the current working directory.</li>
<li><code>pwd</code> prints the user’s current working directory.</li>
<li><code>whoami</code> shows the user’s current identity.</li>
<li><code>/</code> on its own is the root directory of the whole file system.</li>
<li>A relative path specifies a location starting from the current location.</li>
<li>An absolute path specifies a location from the root of the file system.</li>
<li>Directory names in a path are separated with ‘/’ on Unix, but ‘\’ on Windows.</li>
<li>‘..’ means “the directory above the current one”; ‘.’ on its own means “the current directory”.</li>
<li>Most files’ names are <code>something.extension</code>. The extension isn’t required, and doesn’t guarantee anything, but is normally used to indicate the type of data in the file.</li>
<li>Most commands take options (flags) which begin with a ‘-’.</li>
</ul>
<h2 id="working-with-files-and-directories"><a href="02-create.html">Working With Files and Directories</a></h2>
<ul>
<li><code>cp old new</code> copies a file.</li>
<li><code>mkdir path</code> creates a new directory.</li>
<li><code>mv old new</code> moves (renames) a file or directory.</li>
<li><code>rm path</code> removes (deletes) a file.</li>
<li><code>rmdir path</code> removes (deletes) an empty directory.</li>
<li>Use of the Ctrl, or “Control” key may be described in many ways e.g. Ctrl-X, Ctrl+X, Control-X, Control+X, <code>^X</code>.</li>
<li>The shell does not have a trash bin: once something is deleted, it’s really gone.</li>
<li>Nano is a very simple text editor — please use something else for real work.</li>
</ul>
<h2 id="pipes-and-filters"><a href="03-pipefilter.html">Pipes and Filters</a></h2>
<ul>
<li><code>cat</code> displays the contents of its inputs.</li>
<li><code>head</code> displays the first few lines of its input.</li>
<li><code>tail</code> displays the last few lines of its input.</li>
<li><code>sort</code> sorts its inputs.</li>
<li><code>wc</code> counts lines, words, and characters in its inputs.</li>
<li><code>*</code> matches zero or more characters in a filename, so <code>*.txt</code> matches all files ending in <code>.txt</code>.</li>
<li><code>?</code> matches any single character in a filename, so <code>?.txt</code> matches <code>a.txt</code> but not <code>any.txt</code>.</li>
<li><code>command > file</code> redirects a command’s output to a file.</li>
<li><code>first | second</code> is a pipeline: the output of the first command is used as the input to the second.</li>
<li>The best way to use the shell is to use pipes to combine simple single-purpose programs (filters).</li>
</ul>
<h2 id="loops"><a href="04-loop.html">Loops</a></h2>
<ul>
<li>A <code>for</code> loop repeats commands once for every thing in a list.</li>
<li>Every <code>for</code> loop needs a variable to refer to the current “thing”.</li>
<li>Use <code>$name</code> to expand a variable (i.e., get its value). <code>${name}</code> can also be used.</li>
<li>Do not use spaces, quotes, or wildcard characters such as ’*‘or’?’ in filenames, as it complicates variable expansion.</li>
<li>Give files consistent names that are easy to match with wildcard patterns to make it easy to select them for looping.</li>
<li>Use the up-arrow key to scroll up through previous commands to edit and repeat them.</li>
<li>Use Ctrl-R to search through the previously entered commands.</li>
<li>Use <code>history</code> to display recent commands, and <code>!number</code> to repeat a command by number.</li>
</ul>
<h2 id="shell-scripts"><a href="05-script.html">Shell Scripts</a></h2>
<ul>
<li>Save commands in files (usually called shell scripts) for re-use.</li>
<li><code>bash filename</code> runs the commands saved in a file.</li>
<li><code>$@</code> refers to all of a shell script’s command-line parameters.</li>
<li><code>$1</code>, <code>$2</code>, etc., refer to the first command-line parameter, the second command-line parameter, etc.</li>
<li>Place variables in quotes if the values might have spaces in them.</li>
<li>Letting users decide what files to process is more flexible and more consistent with built-in Unix commands.</li>
</ul>
<h2 id="finding-things"><a href="06-find.html">Finding Things</a></h2>
<ul>
<li><code>find</code> finds files with specific properties that match patterns.</li>
<li><code>grep</code> selects lines in files that match patterns.</li>
<li><code>--help</code> is a flag supported by many bash commands, and programs that can be run from within bash, to display more information on how to use these commands or programs.</li>
<li><code>man command</code> displays the manual page for a given command.</li>
<li><code>$(command)</code> inserts a command’s output in place.</li>
</ul>
<h2 id="glossary">Glossary</h2>
<dl>
<dt><span id="absolute-path">absolute path</span></dt>
<dd>A <a href="#path">path</a> that refers to a particular location in a file system. Absolute paths are usually written with respect to the file system’s <a href="#root-directory">root directory</a>, and begin with either “/” (on Unix) or “\” (on Microsoft Windows). See also: <a href="#relative-path">relative path</a>.
</dd>
<dt><span id="argument">argument</span></dt>
<dd>A value given to a function or program when it runs. The term is often used interchangeably (and inconsistently) with <a href="#parameter">parameter</a>.
</dd>
<dt><span id="command-shell">command shell</span></dt>
<dd>See <a href="#shell">shell</a>
</dd>
<dt><span id="command-line-interface">command-line interface</span></dt>
<dd>An interface based on typing commands, usually at a <a href="#read-evaluate-print-loop">REPL</a>. See also: <a href="#graphical-user-interface">graphical user interface</a>.
</dd>
<dt><span id="comment">comment</span></dt>
<dd>A remark in a program that is intended to help human readers understand what is going on, but is ignored by the computer. Comments in Python, R, and the Unix shell start with a <code>#</code> character and run to the end of the line; comments in SQL start with <code>--</code>, and other languages have other conventions.
</dd>
<dt><span id="current-working-directory">current working directory</span></dt>
<dd>The directory that <a href="#relative-path">relative paths</a> are calculated from; equivalently, the place where files referenced by name only are searched for. Every <a href="#process">process</a> has a current working directory. The current working directory is usually referred to using the shorthand notation <code>.</code> (pronounced “dot”).
</dd>
<dt><span id="file-system">file system</span></dt>
<dd>A set of files, directories, and I/O devices (such as keyboards and screens). A file system may be spread across many physical devices, or many file systems may be stored on a single physical device; the <a href="#operating-system">operating system</a> manages access.
</dd>
<dt><span id="filename-extension">filename extension</span></dt>
<dd>The portion of a file’s name that comes after the final “.” character. By convention this identifies the file’s type: <code>.txt</code> means “text file”, <code>.png</code> means “Portable Network Graphics file”, and so on. These conventions are not enforced by most operating systems: it is perfectly possible to name an MP3 sound file <code>homepage.html</code>. Since many applications use filename extensions to identify the <a href="#mime-type">MIME type</a> of the file, misnaming files may cause those applications to fail.
</dd>
<dt><span id="filter">filter</span></dt>
<dd>A program that transforms a stream of data. Many Unix command-line tools are written as filters: they read data from <a href="#standard-input">standard input</a>, process it, and write the result to <a href="#standard-output">standard output</a>.
</dd>
<dt><span id="flag">flag</span></dt>
<dd>A terse way to specify an option or setting to a command-line program. By convention Unix applications use a dash followed by a single letter, such as <code>-v</code>, or two dashes followed by a word, such as <code>--verbose</code>, while DOS applications use a slash, such as <code>/V</code>. Depending on the application, a flag may be followed by a single argument, as in <code>-o /tmp/output.txt</code>.
</dd>
<dt><span id="for-loop">for loop</span></dt>
<dd>A loop that is executed once for each value in some kind of set, list, or range. See also: <a href="#while-loop">while loop</a>.
</dd>
<dt><span id="graphical-user-interface">graphical user interface</span></dt>
<dd>A graphical user interface, usually controlled by using a mouse. See also: <a href="#command-line-interface">command-line interface</a>.
</dd>
<dt><span id="home-directory">home directory</span></dt>
<dd>The default directory associated with an account on a computer system. By convention, all of a user’s files are stored in or below her home directory.
</dd>
<dt><span id="loop">loop</span></dt>
<dd>A set of instructions to be executed multiple times. Consists of a <a href="#loop-body">loop body</a> and (usually) a condition for exiting the loop. See also <a href="#for-loop">for loop</a> and <a href="#while-loop">while loop</a>.
</dd>
<dt><span id="loop-body">loop body</span></dt>
<dd>The set of statements or commands that are repeated inside a <a href="#for-loop">for loop</a> or <a href="#while-loop">while loop</a>.
</dd>
<dt><span id="mime-type">MIME type</span></dt>
<dd>MIME (Multi-Purpose Internet Mail Extensions) types describe different file types for exchange on the Internet, for example images, audio, and documents.
</dd>
<dt><span id="operating-system">operating system</span></dt>
<dd>Software that manages interactions between users, hardware, and software <a href="#process">processes</a>. Common examples are Linux, OS X, and Windows.
</dd>
<dt><span id="orthogonal">orthogonal</span></dt>
<dd>To have meanings or behaviors that are independent of each other. If a set of concepts or tools are orthogonal, they can be combined in any way.
</dd>
<dt><span id="parameter">parameter</span></dt>
<dd>A variable named in the function’s declaration that is used to hold a value passed into the call. The term is often used interchangeably (and inconsistently) with <a href="#argument">argument</a>.
</dd>
<dt><span id="parent-directory">parent directory</span></dt>
<dd>The directory that “contains” the one in question. Every directory in a file system except the <a href="#root-directory">root directory</a> has a parent. A directory’s parent is usually referred to using the shorthand notation <code>..</code> (pronounced “dot dot”).
</dd>
<dt><span id="path">path</span></dt>
<dd>A description that specifies the location of a file or directory within a <a href="#file-system">file system</a>. See also: <a href="#absolute-path">absolute path</a>, <a href="#relative-path">relative path</a>.
</dd>
<dt><span id="pipe">pipe</span></dt>
<dd>A connection from the output of one program to the input of another. When two or more programs are connected in this way, they are called a “pipeline”.
</dd>
<dt><span id="process">process</span></dt>
<dd>A running instance of a program, containing code, variable values, open files and network connections, and so on. Processes are the “actors” that the <a href="#operating-system">operating system</a> manages; it typically runs each process for a few milliseconds at a time to give the impression that they are executing simultaneously.
</dd>
<dt><span id="prompt">prompt</span></dt>
<dd>A character or characters display by a <a href="#read-evaluate-print-loop">REPL</a> to show that it is waiting for its next command.
</dd>
<dt><span id="quoting">quoting</span></dt>
<dd>(in the shell): Using quotation marks of various kinds to prevent the shell from interpreting special characters. For example, to pass the string <code>*.txt</code> to a program, it is usually necessary to write it as <code>'*.txt'</code> (with single quotes) so that the shell will not try to expand the <code>*</code> wildcard.
</dd>
<dt><span id="read-evaluate-print-loop">read-evaluate-print loop</span></dt>
<dd>(REPL): A <a href="#command-line-interface">command-line interface</a> that reads a command from the user, executes it, prints the result, and waits for another command.
</dd>
<dt><span id="redirect">redirect</span></dt>
<dd>To send a command’s output to a file rather than to the screen or another command, or equivalently to read a command’s input from a file.
</dd>
<dt><span id="regular-expression">regular expression</span></dt>
<dd>A pattern that specifies a set of character strings. REs are most often used to find sequences of characters in strings.
</dd>
<dt><span id="relative-path">relative path</span></dt>
<dd>A <a href="#path">path</a> that specifies the location of a file or directory with respect to the <a href="#current-working-directory">current working directory</a>. Any path that does not begin with a separator character (“/” or “\”) is a relative path. See also: <a href="#absolute-path">absolute path</a>.
</dd>
<dt><span id="root-directory">root directory</span></dt>
<dd>The top-most directory in a <a href="#file-system">file system</a>. Its name is “/” on Unix (including Linux and Mac OS X) and “\” on Microsoft Windows.
</dd>
<dt><span id="shell">shell</span></dt>
<dd>A <a href="#cli">command-line interface</a> such as Bash (the Bourne-Again Shell) or the Microsoft Windows DOS shell that allows a user to interact with the <a href="#operating-system">operating system</a>.
</dd>
<dt><span id="shell-script">shell script</span></dt>
<dd>A set of <a href="#shell">shell</a> commands stored in a file for re-use. A shell script is a program executed by the shell; the name “script” is used for historical reasons.
</dd>
<dt><span id="standard-input">standard input</span></dt>
<dd>A process’s default input stream. In interactive command-line applications, it is typically connected to the keyboard; in a <a href="#pipe">pipe</a>, it receives data from the <a href="#standard-output">standard output</a> of the preceding process.
</dd>
<dt><span id="standard-output">standard output</span></dt>
<dd>A process’s default output stream. In interactive command-line applications, data sent to standard output is displayed on the screen; in a <a href="#pipe">pipe</a>, it is passed to the <a href="#standard-input">standard input</a> of the next process.
</dd>
<dt><span id="sub-directory">sub-directory</span></dt>
<dd>A directory contained within another directory.
</dd>
<dt><span id="tab-completion">tab completion</span></dt>
<dd>A feature provided by many interactive systems in which pressing the Tab key triggers automatic completion of the current word or command.
</dd>
<dt><span id="variable">variable</span></dt>
<dd>A name in a program that is associated with a value or a collection of values.
</dd>
<dt><span id="while-loop">while loop</span></dt>
<dd>A loop that keeps executing as long as some condition is true. See also: <a href="#for-loop">for loop</a>.
</dd>
<dt><span id="wildcard">wildcard</span></dt>
<dd>A character used in pattern matching. In the Unix shell, the wildcard <code>*</code> matches zero or more characters, so that <code>*.txt</code> matches all files whose names end in <code>.txt</code>.
</dd>
</dl>
</div>
</div>
</article>
<div class="footer">
<a class="label swc-blue-bg" href="http://software-carpentry.org">Software Carpentry</a>
<a class="label swc-blue-bg" href="https://github.com/swcarpentry/shell-novice">Source</a>
<a class="label swc-blue-bg" href="mailto:[email protected]">Contact</a>
<a class="label swc-blue-bg" href="LICENSE.html">License</a>
</div>
</div>
<!-- Javascript placed at the end of the document so the pages load faster -->
<script src="http://software-carpentry.org/v5/js/jquery-1.9.1.min.js"></script>
<script src="css/bootstrap/bootstrap-js/bootstrap.js"></script>
<script src='https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML'></script>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-37305346-2', 'auto');
ga('send', 'pageview');
</script>
</body>
</html>