By Carl Albing, JP Vossen, Cameron Newham
Book Price: $49.99 USD
£35.50 GBP
PDF Price: $39.99
Cover | Table of Contents | Colophon
jp@adams:~$
jp@adams:/tmp$
bash-2.03$ pwd /tmp bash-2.03$ export PS1='[\u@\h \w]$ ' [jp@solaris8 /tmp]$
-L displays your logical path and is the default.s displays your physical location, which may differ from your logical path if you have followed a symbolic link.bash-2.03$ pwd /tmp/dir2 bash-2.03$ pwd -L /tmp/dir2 bash-2.03$ pwd -P /tmp/dir1
$PATH. The bash built-in type command searches your environment (including aliases, keywords, functions, built-ins, and files in the $PATH) for executable commands matching its arguments and displays the type and location of any matches. It has several arguments, notably the -a flag, which causes it to print all matches instead of stopping at the first one. The which command is similar but only searches your $PATH (and csh aliases). It may vary from system to system (it's usually a csh shell script on BSD, but a binary on Linux), and usually has a -a flag like type. Use these commands when you know the name of a command and need to know exactly where it's located, or to see if it's on this computer. For example:$ type which which is hashed (/usr/bin/which) $ type ls ls is aliased to `ls -F -h' $ type -a ls ls is aliased to `ls -F -h' ls is /bin/ls $ which which /usr/bin/which
man ls will give you documentation about the ls command. Many programs also have a built-in help facility, accessed by providing a "help me" argument such as -h or --help. Some programs, especially on other operating systems, will give you help if you don't give them arguments. Some Unix commands will also do that, but a great many of them will not. This is due to the way that Unix commands fit together into something called pipelines, which we'll cover later. But what if you don't know or can't remember the name of the command you need? apropos searches manpage names and descriptions for regular expressions supplied as arguments. This is incredibly useful when you don't remember the name of the command you need. This is the same as man -k.$ apropos music cms (4) - Creative Music System device driver $ man -k music cms (4) - Creative Music System device driver
$ touch /tmp/sample_file $ ls /tmp/sample_file /tmp/sample_file $ ls -l /tmp/sample_file -rw-r--r-- 1 jp jp 0 Dec 18 15:03 /tmp/sample_file $ stat /tmp/sample_file File: "/tmp/sample_file" Size: 0 Blocks: 0 IO Block: 4096 Regular File Device: 303h/771d Inode: 2310201 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 501/ jp) Gid: ( 501/ jp) Access: Sun Dec 18 15:03:35 2005 Modify: Sun Dec 18 15:03:35 2005 Change: Sun Dec 18 15:03:42 2005 $ file /tmp/sample_file /tmp/sample_file: empty $ file -b /tmp/sample_file empty $ echo '#!/bin/bash -' > /tmp/sample_file $ file /tmp/sample_file /tmp/sample_file: Bourne-Again shell script text executable $ file -b /tmp/sample_file Bourne-Again shell script text executable
ls shows only filenames, while -l provides more details about each file. ls has many options; consult the manpage on your system for the ones it supports. Useful options include:ls -F a slash (/) indicates a directory, an asterisk (*) means the file is executable, an at sign (@) indicates a symbolic link, a percent sign (%) shows a whiteout, an equal sign (=) is a socket, and a pipe or vertical bar (|) is a FIFO.ls -l:$ ls -l /tmp/sample_file -rw-r--r-- 1 jp jp 14 Dec 18 15:04 /tmp/sample_file $ stat -c'%A %h %U %G %s %y %n' /tmp/sample_file -rw-r--r-- 1 jp jp 14 Sun Dec 18 15:04:12 2005 /tmp/sample_file $ find /tmp/ -name sample_file -printf '%m %n %u %g %t %p' 644 1 jp jp Sun Dec 18 15:04:12 2005 /tmp/sample_file
ls -a shows all files, including normally hidden ones, but that is often too noisy, and ls -a .* doesn't do what you think it will.ls-d along with whatever other criteria you have.ls -d .* ls -d .b* ls -d .[!.]*
$ grep -l 'PATH' ~/.[!.]* /home/jp/.bash_history /home/jp/.bash_profile
*.txt means any file ending in .txt, while *txt means any file ending in txt (no dot). f?o would match foo or fao but not fooo. So you'd think that .* would match any file beginning with a dot.-d and without, then try echo.*. The echo trick simply shows you what the shell expanded your .* to. Try echo.[!.]* also.$ echo A coffee is $5?! A coffee is ?! $ echo "A coffee is $5?!" -bash: !": event not found $ echo 'A coffee is $5?!' A coffee is $5?!
$5 is treated as a variable to expand, but since it doesn't exist it is set to null. In the second example, the same is true, but we never even get there because !" is treated as a history substitution, which fails in this case because it doesn't match anything in the history. The third example works as expected.$ echo 'A coffee is $5 for' "$USER" '?!' A coffee is $5 for jp ?! $ echo "A coffee is \$5 for $USER?\!" A coffee is $5 for jp?\! $ echo "A coffee is \$5 for $USER?! " A coffee is $5 for jp?!
# We'll get a continuation prompt since we now have unbalanced quotes $ echo '$USER won't pay $5 for coffee.' > ^C # WRONG $ echo "$USER won't pay $5 for coffee." jp won't pay for coffee. # Works $ echo "$USER won't pay \$5 for coffee." jp won't pay $5 for coffee. # Also works $ echo 'I won'\''t pay $5 for coffee.' I won't pay $5 for coffee.
$VAR syntax# type cd cd is a shell builtin # type awk awk is /bin/awk # which cd /usr/bin/which: no cd in (/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/ sbin:/usr/bin/X11:/usr/X11R6/bin:/root/bin) # which awk /bin/awk
enable -a will list all built-ins and their enabled or disabled status.-h or --help option to get usage reminders, and if a manpage exists it's often just a pointer to the large bash manpage. That's where the help command, which is itself a built-in, comes in handy. help displays help about shell built-ins.# 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.
case statement:#!/usr/bin/env bash # cookbook filename: interactive case "$-" in *i*) # Code for interactive shell here ;; *) # Code for non-interactive shell here ;; esac
i if the shell is interactive.if [ "$PS1" ]; then echo This shell is interactive else echo This shell is not interactive fi
case statement$ bash --version GNU bash, version 3.00.16(1)-release (i386-pc-solaris2.10) Copyright (C) 2004 Free Software Foundation, Inc.
chsh -l or cat /etc/shells may give you a list of valid shells on some systems. Otherwise, ask your system administrator where bash is, or if it can be installed.chsh -l provides a list of valid shells on Linux, but opens an editor and allows you to change settings on BSD. -l is not a valid option to chsh on Mac OS X, but just running chsh will open an editor to allow you to change settings, and chpass -s shell will change your shell.chsh -s command to change your default shell. For example, chsh -s /bin/bash. If for any reason that fails try chsh, passwd -e, passwd -l chpass,or usermod -s /usr/bin/bash. If you still can't change your shell ask your system administrator, who may need to edit the /etc/passwd file. On most systems, /etc/passwd will have lines of the form:cam:pK1Z9BCJbzCrBNrkjRUdUiTtFOh/:501:100:Cameron Newham:/home/cam:/bin/bash cc:kfDKDjfkeDJKJySFgJFWErrElpe/:502:100:Cheshire Cat:/home/cc:/bin/bash
Distribution | 2.x in base install | 2.x in updates | 3.x in base install | 3.x in updates |
|---|---|---|---|---|
Debian Woody | 2.05a | N/A | N/A | N/A |
Debian Sarge | 2.05b | 3.1dfsg-8 (testing & unstable) | 3.0-12(1)-release | 3.00.16(1)-release |
Fedora Core 1 | bash-2.05b-31.i386.rpm | bash-2.05b-34.i386.rpm | N/A | N/A |
Fedora Core 2 | bash-2.05b-38.i386.rpm | N/A | N/A | N/A |
Fedora Core3 | N/A | N/A | bash-3.0-17.i386.rpm | bash-3.0-18.i386.rpm |
Fedora Core 4 | N/A | N/A | bash-3.0-31.i386.rpm | N/A |
Fedora Core 5 | N/A | N/A | bash-3.1-6.2.i386.rpm | bash-3.1-9.fc5.1.i386.rpm |
Fedora Core 6 | N/A | N/A | bash-3.1-16.1.i386.rpm | N/A |
Knoppix 3.9 & 4.0.2 | N/A | N/A | 3.0-15 | N/A |
Mandrake 9.2 | bash-2.05b-14mdk.i586.rpm | N/A | N/A | N/A |
Mandrake 10.1 | bash-2.05b-22mdk.i586.rpm | N/A | N/A | N/A |
Mandrake 10.2 | N/A | N/A | bash-3.0-2mdk.i586.rpm | N/A |
Mandriva 2006.0 | N/A | N/A | bash-3.0-6mdk.i586.rpm | N/A |
Mandriva 2007.0 | N/A | N/A | bash-3.1-7mdv2007.0.i586.rpm | N/A |
OpenSUSE 10.0 | N/A | N/A | 3.00.16(1)-release | 3.0.17(1)-release |
OpenSUSE 10.1 | N/A | N/A | 3.1.16(1)-release | N/A |
OpenSUSE 10.2 | N/A | N/A | bash-3.1-55.i586.rpm | N/A |
SLED 10 RC3 | N/A | N/A | 3.1.17(1)-release | N/A |
RHEL 3.6, CentOS 3.6 | bash-2.05b.0(1) | N/A | N/A | N/A |
RHEL 4.4, CentOS 4.4 | N/A | N/A | 3.00.15(1)-release | N/A |
MEPIS 3.3.1 | N/A | N/A | 3.0-14 | N/A |
Ubuntu 5.10 | N/A | N/A | 3.0.16(1) | N/A |
Ubuntu 6.06 | N/A | N/A | 3.1.17(1)-release | N/A |
Ubuntu 6.10 | N/A | N/A | 3.1.17(1)-release | N/A |
pkg_add command. If you are an experienced BSD user, you may prefer using the ports collection, but we will not cover that here.pkg_add -vr bash
pkg_add -vu ftp://ftp.netbsd.org/pub/NetBSD/packages/pkgsrc-2005Q3/NetBSD-2.0/i386/ All/bash-3.0pl16nb3.tgz
pkg_add -vr command. You may have to adjust the FTP path for your version and architecture. Also, there may be a statically compiled version. For example: ftp://ftp.openbsd.org/pub/OpenBSD/3.8/packages/i386/bash-3.0.16p1-static.tgz.pkg_add -vr ftp://ftp.openbsd.org/pub/OpenBSD/3.8/packages/i386/bash-3.0.16p1.tgz
Solaris 2.x, Solaris 7, and Solaris 8 users can get a precompiled version of bash-3.0 from the Sunfreeware site. Sun ships bash-2.03 with Solaris 8 distributions, ships bash-2.05 as a supported part of Solaris 9, and ships bash-3.0 as a supported part of Solaris 10 (directly on the Solaris 10 CD).AIX users can get precompiled versions of older releases of bash for various versions of AIX from Groupe Bull, and sources and binaries of current releases for various AIX releases from UCLA. IBM makes bash-3.0 available for AIX 5L as part of the AIX tool-box for [GNU/]Linux applications. They use RPM format; you can get RPM for AIX from there, too.SGI users can get an installable version of bash-2.05b from the SGI Freeware page.HP-UX users can get bash-3.0 binaries and source code from the Software Porting and Archive Center for HP-UX.Tru64 Unix users can get sources and binaries for bash-2.05b from the HP/Compaq Tru64 Unix Open Source Software Collection.
What Is CygwinCygwin is a Linux-like environment for Windows. It consists of two parts:
A DLL (cygwin1.dll), which acts as a Linux API emulation layer providing substantial Linux API functionality. A collection of tools, which provide Linux look and feel.The Cygwin DLL works with all non-beta, non "release candidate," x86 32-bit versions of Windows since Windows 95, with the exception of Windows CE.What Isn't Cygwin Cygwin is not a way to run native Linux apps on Windows. You have to rebuild your application from source if you want to get it running on Windows. Cygwin is not a way to magically make native Windows apps aware of Unix functionality (e.g., signals, ptys). Again, you need to build your apps from source if you want to take advantage of Cygwin functionality.
polarhome.com is non commercial, educational effort for popularization of shell enabled operating systems and Internet services, offering shell accounts, mail and other online services on all available systems (currently on Linux, OpenVMS, Solaris, AIX, QNX, IRIX, HP-UX, Tru64, FreeBSD, OpenBSD, NetBSD and OPENSTEP).[…]Note: this site is continuously under construction and running on slow lines and low capacity servers that have been retired, therefore as a non commercial site user/visitor, nobody should have too high expectations in any meaning of the word. Even if polarhome.com does all to provide services on professional level, users should not expect more than "AS-IS".polarhome.com is a distributed site, but more than 90% of polarhome realm is located in Stockholm, Sweden.
echo Please wait.
Please wait.
$ echo Please wait. Please wait. $
$ echo this was very widely spaced this was very widely spaced $
$ echo "this was very widely spaced" this was very widely spaced $
$ echo 'this was very widely spaced' this was very widely spaced $
$ printf '%s = %d\n' Lines $LINES Lines = 24 $
$ printf '%-10.10s = %4.2f\n' 'GigaHerz' 1.92735 GigaHerz = 1.93 $
s or f in our example) provide additional formatting details. For the floating-point type (f), the first number (4 in the 4.2 specifier) is the width of the entire field. The second number (2) is how many digits should be printed to the right of the decimal point. Note that it rounds the answer.$ printf '%10.10s = %4.2f\n' 'GigaHerz' 1.92735 GigaHerz = 1.93 $