Penguin
Annotated edit history of BashNotes version 60, including all changes. View license author blame.
Rev Author # Line
50 BenStaz 1 !! What's true and what's false?
2
3 0 is true, anything non-zero is false. You can test the return value of any command by examining the special <tt>$?</tt> EnvironmentVariable.
4
5 <pre>
6 __$ ls mbox ; echo $?__
7 mbox
8 0
9 __$ ls mboxxx ; echo $?__
10 ls: mboxxx: No such file or directory
11 1
12 </pre>
13
14 There are also special commands called true(1) and false(1) that can be used in tests:
15
16 <pre>
17 __$ true ; echo $?__
18 0
19 __$ false ; echo $?__
20 1
21 </pre>
22
23 !! How do I stop bash beeping at me all the time?
24
25 It's probably not bash(1) beeping, but readline(3) sending a bell. You can disable this by putting:
26
27 <verbatim>
28 set audible-bell none
29 </verbatim>
30
31 in your <tt>~~/.inputrc</tt> file. This will remove the bell for all readline(3) enabled programs. To make it only do this for bash(1), see the readline(3) section of the bash(1) manpage.
32
33 If you are in a graphical environment, you can tell your [XServer] to disable all beeps for X programs (including xterm(1) and other terminals) by setting the bell volume to zero:
34
35 <verbatim>
36 xset b 0
37 </verbatim>
38
39 If you use gnome, then that annoying beeping can be disabled by going to System > Preferences > Sound
40
41 Find the "System Beep" tab and uncheck "Enable System Beep".
42
43 !! How do I do arithmetic in bash?
44
45 Surround the expression with $(( )), eg:
46
47 <verbatim>
48 ANSWER=$((6*9))
49 </verbatim>
50
51 This is equivalent to using the expr(1) program, but you will need to escape any shell special characters:
52
53 <verbatim>
54 ANSWER=`expr 6 \* 9`
55 </verbatim>
56
57 !! How do I wait until a specified time?
58
59 You could use at(1) but it's more fun to use something like this:
60
61 <verbatim>
62 sleep $(($(date -d '6pm tomorrow' +%s)-$(date +%s)))
63 </verbatim>
64
65 !! What's << doing in all my files?
66
67 See HereDocuments.
68
69 !! How can I easily do a batch rename of files with a subtle change (ie . to -)?
70
71 Easy:
72
73 <verbatim>
74 for i in *.files.foo; do mv $i ${i/files.foo/files-foo}; done
75 </verbatim>
76
77 This lets you use sed(1)-like regexp syntax in the command line. You might want to read up on "Parameter Expansion" in bash(1).
78
79 To remove a suffix:
80
81 <verbatim>
82 for i in *.files.foo; do mv $i ${i%.foo}; done
83 </verbatim>
84
85 To remove a prefix:
86
87 <verbatim>
88 for i in files-foo*; do mv $i ${i#files-}; done
89 </verbatim>
90
91 !! How do I get a sequence of numbers
92
93 For small ranges you can just write the numbers out:
94
95 <verbatim>
96 for i in 2 3 4 5 6; do echo $i; done
97 </verbatim>
98
99 On [GNU] systems, use the <tt>seq(1)</tt> program:
100
101 <verbatim>
102 for i in `seq 50 1000`; do echo $i; done
103 </verbatim>
104
105 [FreeBSD] has a similar program called <tt>jot</tt>.
106
107 !! How do I insert a Tab character when <tt>\t</tt> won't work?
108
109 Type Ctrl-V so the next character will be interpreted literally, then tap Tab to insert a literal Tab character.
110
111 !! How does redirection work?
112
113 You can redirect a program's stdout and/or stderr to places other than the terminal – eg to files on the disk, or as another program's stdin. The order that redirections happen can be important. Some examples.
114
115 Make stderr go to stdout, so you can view both of them together in less(1) or more(1):
116
117 <verbatim>
118 somecommand 2>&1 | less
119 </verbatim>
120
121 Get rid of stdout, and move stderr to stdout so that you can pipe it to less:
122
123 <verbatim>
124 somecommand 2>&1 >/dev/null | less
125 </verbatim>
126
127 The following first sends stdout to <tt>/dev/null</tt>, and then sends stderr to where stdout is now pointing (ie sends both to <tt>/dev/null</tt>, which is probably not what you want for redirection, but it's excellent for getting silent output from a command):
128
129 <verbatim>
130 somecommand >/dev/null 2>&1
60 AlastairPorter 131 </verbatim>
132
133 As an alternative, to send both stdout and stderr to the same place you can use the <tt>&></tt> redirector:
134
135 <verbatim>
136 somecommand &>/dev/null
50 BenStaz 137 </verbatim>
138
139 See CshProgrammingConsideredHarmful for some trickier examples.
140
141 !! How does redirection work from inside a script?
142
143 A useful method to debug bash scripts is to run then with <tt>bash -x</tt>; printing each line as it goes, interspersed with its output. However, you can't start a script with
144
145 <verbatim>
146 #!/bin/bash -x > foo
147 </verbatim>
148
149 You can, however, do this:
150
151 <verbatim>
152 #!/bin/bash -x
153 exec 1> /tmp/foo
154 exec 2>&1
155 </verbatim>
156
157 !! How can I get the output from a command into a program that acts on files, not stdin?
158
159 Using process substitution (see [bash(1)Part4]) -
160
161 <verbatim>
162 gedit <(groff-2-wiki.pl bash)
163 </verbatim>
164
165 This opens up the bash(1) wikified page in gedit (as a temp file like <tt>/dev/fd/63</tt>), ready to manually overview and paste into the wiki. See the file archive for the groff-2-wiki script.
166
167 !! Bash isn't using the full width of the terminal after I resize it
168
169 When you resize a terminal, it sends a [SIGWINCH] signal to the process running inside it. If the current foreground process is not bash(1), but another process (eg less(1), tail(1), or whatever), then only that process receives the signal. The shell is then left with outdated size information.
170
171 The best way to fix this is to set the <tt>checkwinsize</tt> shell option:
172
173 <verbatim>
174 shopt -s checkwinsize
175 </verbatim>
176
177 Bash will now check the size every time it displays a prompt. Other shells (such as zsh(1)) seem to do this automatically, and don't have a corresponding option.
178
179 A one-off workaround is to send [SIGWINCH] to the shell yourself:
180
181 <verbatim>
182 kill -WINCH $$
183 </verbatim>
184
185 (<tt>$$</tt> is a variable containing the shell's own [PID].)
186
187 !! How do I change the title of a terminal?
188
189 From [faqs.org|http://www.faqs.org/docs/Linux-mini/Xterm-Title.html#ss4.3]:
190
191 Window and icon titles may be changed in a running xterm(1) (and most derivatives, such as gnome-terminal) by using XTerm escape sequences. The following sequences are useful in this respect:
192
193 * <tt>''ESC'']0;string''BEL''</tt> – Set icon name and window title to string
194 * <tt>''ESC'']1;string''BEL''</tt> – Set icon name to string
195 * <tt>''ESC'']2;string''BEL''</tt> – Set window title to string
196
197 where ESC is the escape character (\033), and BEL is the bell character (\007).
198
199 <verbatim>
200 echo -ne "\033]0;''title here''\007"
201 </verbatim>
202
203 In bash(1), to have the title automatically update based on who and where you are, set <tt>PROMPT_COMMAND</tt> like so:
204
205 <verbatim>
206 ROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD}\007"'
207 </verbatim>
208
209 !!How do I enable case-insensitive tab completion?
210
211 * bind "set completion-ignore-case on"
212
213 That should configure the shell completion to be case-insensitive for
214 the remainder of the session.
215
216 Make permanent:
217 Create a file called .inputrc in your home directory and put this line in it:
218 *set completion-ignore-case on
219
220 !! Help, bash is listing my files weird!
221
222 Well, bash is listing nothing. It's ls(1) and its locale support that matter here, it has nothing to do with the [Shell]. But this is where you'd expect to find this tidbit.
223
224 I swear ls(1) used to list files in [ASCII] order. Then one day it stopped. And I didn't like it. Much swearing and cursing ensued.
225
226 <verbatim>
227 $ ls -la
228 total 3612
229 drwxr-xr-x 15 573 573 4096 2005-05-14 14:53 .
230 drwxrwsr-x 4 root src 4096 2005-06-15 01:59 ..
231 drwxr-xr-x 20 573 573 4096 2003-08-25 23:44 arch
232 -rw-r--r-- 1 root root 17644 2005-05-14 14:44 .config
233 -rw-r--r-- 1 root root 4376 2005-05-14 14:46 .depend
234 -rw-r--r-- 1 573 573 18691 2002-08-03 12:39 COPYING
235 drwxr-xr-x 32 573 573 4096 2005-04-04 13:42 Documentation
236 drwxr-xr-x 40 573 573 4096 2005-05-14 14:45 drivers
237 -rw-rw-r-- 1 573 573 19095 2005-05-14 14:44 Makefile
238 drwxr-xr-x 2 573 573 4096 2005-05-14 14:49 mm
239 -rw-r--r-- 1 573 573 14287 2003-08-25 23:44 README
240 -rw-r--r-- 1 root root 505139 2005-05-14 14:53 System.map
241 -rw-r--r-- 1 root root 2 2005-05-14 14:46 .version
242 -rwxr-xr-x 1 root root 2646164 2005-05-14 14:53 vmlinux
243 $ export LANG=C LC_ALL=C
244 $ ls -la
245 total 3612
246 drwxr-xr-x 15 573 573 4096 May 14 14:53 .
247 drwxrwsr-x 4 root src 4096 Jun 15 01:59 ..
248 -rw-r--r-- 1 root root 17644 May 14 14:44 .config
249 -rw-r--r-- 1 root root 4376 May 14 14:46 .depend
250 -rw-r--r-- 1 root root 2 May 14 14:46 .version
251 -rw-r--r-- 1 573 573 18691 Aug 3 2002 COPYING
252 drwxr-xr-x 32 573 573 4096 Apr 4 13:42 Documentation
253 -rw-rw-r-- 1 573 573 19095 May 14 14:44 Makefile
254 -rw-r--r-- 1 573 573 14287 Aug 25 2003 README
255 -rw-r--r-- 1 root root 505139 May 14 14:53 System.map
256 drwxr-xr-x 20 573 573 4096 Aug 25 2003 arch
257 drwxr-xr-x 40 573 573 4096 May 14 14:45 drivers
258 drwxr-xr-x 2 573 573 4096 May 14 14:49 mm
259 -rwxr-xr-x 1 root root 2646164 May 14 14:53 vmlinux
260 </verbatim>
261
262 !! Dealing with argument lists in wrappers
263
264 If you have a shell script that passes arguments on to another program while acting as a wrapper watch carefully how you deal with arguments or you will end up accidentally eating whitespace. See the example below for a demonstration.
265
266 <tt>test.sh</tt>:
267
268 <verbatim>
269 #!/bin/bash
270 # Print number of arguments
271 echo $#
272 </verbatim>
273
274 <tt>wrapper.sh</tt>:
275
276 <verbatim>
277 #!/bin/bash
278
279 # Do some stuff here
280
281 # Call another program
282 ./test.sh $@
283 </verbatim>
284
285 <tt>safe_wrapper.sh</tt>:
286
287 <verbatim>
288 #!/bin/bash
289
290 # Do some stuff here
291
292 # Call another program
293 ./test.sh "$@"
294 </verbatim>
295
296 <tt>Output</tt>:
297
298 <verbatim>
299 matt@argon:/tmp$ ./wrapper.sh
300 0
301 matt@argon:/tmp$ ./safe_wrapper.sh
302 0
303 matt@argon:/tmp$ ./wrapper.sh a
304 1
305 matt@argon:/tmp$ ./safe_wrapper.sh b
306 1
307 matt@argon:/tmp$ ./wrapper.sh a ""
308 1
309 matt@argon:/tmp$ ./safe_wrapper.sh b ""
310 2
311 </verbatim>
312
313 Note that the 'empty' argument is only preserved when $@ is wrapped in quotes (eg in safe_wrapper.sh).
314
315 !! Why can't I hit ''Del''? Why does it show a tilde (~~) character ?
316
317 readline(3) isn't correctly interpreting the escape code that delete generates. You can work around it in your terminal, but the easiest fix is to add the line
318
319 <verbatim>
320 "\e[3~": delete-char
321 </verbatim>
322
323 to <tt>~~/.inputrc</tt>.
324
325 Garret ~LeSage has posted a [super useful inputrc|http://linuxart.com/log/archives/2005/10/13/super-useful-inputrc/], which fixes all sorts of keys (such as ctrl-arrow).
326
327 Here's another cool self-documented tip:
328
329 <verbatim>
330 # By default up/down are bound to previous-history
331 # and next-history respectively. The following does the
332 # same but gives the extra functionality where if you
333 # type any text (or more accurately, if there is any text
334 # between the start of the line and the cursor),
335 # the subset of the history starting with that text
336 # is searched (like 4dos for e.g.).
337 # Note to get rid of a line just hit Ctrl-C.
338 "\e[B": history-search-forward
339 "\e[A": history-search-backward
340 </verbatim>
341
342 !! How do I feed quote characters to the other end of an [SSH] session?
343
344 I have a bunch of remote machines with a config file that has some <tt>OPTIONS=something</tt>, and I want to set them all across-the-board to <tt>OPTIONS="foo"</tt>. Locally, I would do it this way:
345
346 <verbatim>
347 sed -i -e 's/OPTIONS=.*/OPTIONS="foo"/' file
348 </verbatim>
349
350 This is hard to send over [SSH] however, as bash(1) locally likes to eat all your quotes, so they never reach the other end.
351
352 PerryLorier's most bodacious answer:
353
354 <verbatim>
355 ssh site sed -i -e \''s/OPTIONS=.*/OPTIONS="foo"/'\' file
356 </verbatim>
357
358 If anyone else has an answer, please add it, rather than replacing this one; I can at least understand what happens here. One set of brackets is eaten locally, and another set is sent to the other end, so the sed command ends up with 's when run on the remote host. —CraigBox
359
360 Another option:
361
362 <verbatim>
363 ssh site <<'END_SCRIPT'
364 sed -i -e 's/OPTIONS=.*/OPTIONS="foo"/' file
365 END_SCRIPT
366 </verbatim>
367
368 —AristotlePagaltzis
52 LawrenceDoliveiro 369
370 LawrenceDoliveiro's so-simple-you'll-kick-yourself-for-not-thinking-about-it answer:
371
372 <verbatim>
373 ssh site sed -i -e $(printf %q 's/OPTIONS=.*/OPTIONS="foo"/') file
374 </verbatim>
375
376 using Bash's <tt>printf</tt> builtin.
51 BenStaz 377
378 !How can I see what my command will look like after bash expands it?
379
380 After typing the command, press ''Esc'' then press ''C-e''
381
53 BenStaz 382 !Bash C-style for loop construct
383
384 for ((i=1; i<100;i++));do echo $i; done;
50 BenStaz 385
386 !How can I do Division in bash using floating point numbers?
387
388 Use ''bc''
389
390 *echo "scale=10;5/4" | bc"
391
392 will result in : 1.2500000000
393 The value of the scale function is the number of digits after the decimal point in the expression. (In this case 10)
394
395 !How Can I use ''and'' / ''or'' in bash if statements?
396
397
398 *You can do boolean OR in BASH by using the -o operator.
399 *You can do boolean AND in BASH by using the -a operator.
400
401 Try intermixing the two also.
402
403 <verbatim>
404 if [ $bla -eq 1 -o $cars -eq 3 -o $monkeys -eq 4 ]; then
405 echo "Westside"
406 fi
407 </verbatim>
54 BenStaz 408
409 !How can I supply filenames with a prefix of '-' '--' as arguments to a program?
410
411 Say you have a file called '--test'.
412 If you wanted to use vim to edit this file then you would do:
413
59 AdrianHo 414 <verbatim>
415 vim -- --test
416 </verbatim>
54 BenStaz 417
418 The '--' tells the command that what follows is an argument, NOT to interpret them as options.
59 AdrianHo 419
420 AdrianHo: Note that not all programs accept '--' as an "end of options" marker. A more reliable method is prefixing the filename with "./", as in:
421
422 <verbatim>
423 vim ./--test
424 </verbatim>
55 BenStaz 425
426 !How to directly interpret a shell script by the current shell. (Without forking a subshell)
427
428 *. <myscript>
429
430 If the above script makes a change to the environment, it is the environment of this shell that is changed.
431 So if inside the script we change the directory, then when the script has finished being interpreted we will find ourselves in the new directory.
432
433 Note : This can be handy when you want to reset the shell environment variables.
434
435 Just do:
436
437 *. ~~/.bashrc
56 BenStaz 438
439 !Set vi mode in bash
440
441 Vi mode allows for the use of vi like commands when at the bash prompt. When set to this mode initially you will be in insert mode (be able to type at the prompt unlike when you enter vi). Hitting the escape key takes you into command mode. To enable this do:
442
443 *set -o vi
444
445 One thing I do like about vi mode, is how easy it is to edit really long commands. (Of course some will argue that I should just use a bash script instead)
446
447 When in ''vi mode'' press 'esc' to enter ''command mode''. Now press 'v'.
448 This will start up the default editor (as defined by the EDITOR environment variable) and your command will be displayed.
449 Edit it to your liking (feel free to use multiple lines for loops/if statements etc), save and exit.
450 Your command will be executed.
451
452 To escape out of ''vi mode'' simply type:
453
454 *set +o vi
455
50 BenStaz 456 !! See also
457
458 * CommonErrors, under "Your shell hangs"
459 * BashOneLiners
460 * SpacesInPathNames
461 * PortabilityNotes
58 IanMcDonald 462 * BashHistory
52 LawrenceDoliveiro 463 * [bash reference manual|http://www.gnu.org/software/bash/manual/bashref.html]
58 IanMcDonald 464 ----
465 CategoryNotes

PHP Warning

lib/blame.php:177: Warning: Invalid argument supplied for foreach()