Beruflich Dokumente
Kultur Dokumente
ftp://ftp.cwru.edu/pub/bash/FAQ
----------
Contents:
C1) How does bash differ from sh, the Bourne shell?
C2) How does bash differ from the Korn shell, version ksh88?
C3) Which new features in ksh-93 are not in bash, and which are?
Section D: Why does bash do some things differently than other Unix shells?
E1) Why is the bash builtin `test' slightly different from /bin/test?
E2) Why does bash sometimes say `Broken pipe'?
E3) When I have terminal escape sequences in my prompt, why does bash
wrap lines at the wrong column?
E4) If I pipe the output of a command into `read variable', why doesn't
the output show up in $variable when the read command finishes?
E5) I have a bunch of shell scripts that use backslash-escaped characters
in arguments to `echo'. Bash doesn't interpret these characters. Why
not, and how can I make it understand them?
E6) Why doesn't a while or for loop get suspended when I type ^Z?
E7) What about empty for loops in Makefiles?
E8) Why does the arithmetic evaluation code complain about `08'?
E9) Why does the pattern matching expression [A-Z]* match files beginning
with every letter except `z'?
E10) Why does `cd //' leave $PWD as `//'?
E11) If I resize my xterm while another program is running, why doesn't bash
notice the change?
E12) Why don't negative offsets in substring expansion work like I expect?
E13) Why does filename completion misbehave if a colon appears in the filename?
E14) Why does quoting the pattern argument to the regular expression matching
conditional operator (=~) cause matching to stop working?
E15) Tell me more about the shell compatibility level.
G1) How can I get bash to read and display eight-bit characters?
G2) How do I write a function `x' to replace builtin command `x', but
still invoke the command from within the function?
G3) How can I find the value of a shell variable whose name is the value
of another shell variable?
G4) How can I make the bash `time' reserved word print timing output that
looks like the output from my system's /usr/bin/time?
G5) How do I get the current directory into my prompt?
G6) How can I rename "*.foo" to "*.bar"?
G7) How can I translate a filename from uppercase to lowercase?
G8) How can I write a filename expansion (globbing) pattern that will match
all files in the current directory except "." and ".."?
----------
Section A: The Basics
ftp://ftp.gnu.org/pub/gnu/bash/bash-4.3.tar.gz
ftp://ftp.cwru.edu/pub/bash/bash-4.3.tar.gz
ftp://ftp.gnu.org/pub/gnu/bash/bash-doc-4.3.tar.gz
ftp://ftp.cwru.edu/pub/bash/bash-doc-4.3.tar.gz
Any patches for the current version are available with the URL:
ftp://ftp.cwru.edu/pub/bash/bash-4.3-patches/
Bash has been ported to nearly every version of Unix. All you
should have to do to build it on a machine for which a port
exists is to type `configure' and then `make'. The build process
will attempt to discover the version of Unix you have and tailor
itself accordingly, using a script created by GNU autoconf.
More information appears in the file `INSTALL' in the distribution.
Cygnus originally ported bash-1.14.7, and that port was part of their
early GNU-Win32 (the original name) releases. Cygnus has also done
ports of bash-3.2 and bash-4.0 to the CYGWIN environment, and both
are available as part of their current release.
http://www.delorie.com/djgpp/
I have been told that the original DJGPP port was done by Daisuke Aoyama.
ftp://ftp.simtel.net/pub/simtelnet/gnu/djgpp/v2gnu/bsh204b.zip binary
ftp://ftp.simtel.net/pub/simtelnet/gnu/djgpp/v2gnu/bsh204d.zip documentation
ftp://ftp.simtel.net/pub/simtelnet/gnu/djgpp/v2gnu/bsh204s.zip source
Mark began to work with bash-2.05, but I don't know the current status.
Some machines let you use `chsh' to change your login shell. Other
systems use `passwd -s' or `passwd -e'. If one of these works for
you, that's all you need. Note that many systems require the full
pathname to a shell to appear in /etc/shells before you can make it
your login shell. For this, you may need the assistance of your
friendly local system administrator.
If you cannot do this, you can still use bash as your login shell, but
you need to perform some tricks. The basic idea is to add a command
to your login shell's startup file to replace your login shell with
bash.
For example, if your login shell is csh or tcsh, and you have installed
bash in /usr/gnu/bin/bash, add the following line to ~/.login:
It's not a good idea to put this command into ~/.cshrc, because every
csh you run without the `-f' option, even ones started to run csh scripts,
reads that file. If you must put the command in ~/.cshrc, use something
like
This will cause login shells to replace themselves with bash running as
a login shell. Once you have this working, you can copy your initialization
code from ~/.profile to ~/.bash_profile.
I have received word that the recipe supplied above is insufficient for
machines running CDE. CDE has a maze of twisty little startup files, all
slightly different.
If you cannot change your login shell in the password file to bash, you
will have to (apparently) live with CDE using the shell in the password
file to run its startup scripts. If you have changed your shell to bash,
there is code in the CDE startup files (on Solaris, at least) that attempts
to do the right thing. It is, however, often broken, and may require that
you use the $BASH_ENV trick described below.
You can use the above `exec' recipe to start bash when not logging in with
CDE by testing the value of the DT variable:
if [ -n "$DT" ]; then
[ -f /usr/gnu/bin/bash ] && exec /usr/gnu/bin/bash --login
fi
If CDE starts its shells non-interactively during login, the login shell
startup files (~/.profile, ~/.bash_profile) will not be sourced at login.
To get around this problem, append a line similar to the following to your
~/.dtprofile:
unset BASH_ENV
A8) I just changed my login shell to bash, and now I can't FTP into my
machine. Why not?
You must add the full pathname to bash to the file /etc/shells. As
noted in the answer to the previous question, many systems require
this before you can make bash your login shell.
http://www.UNIX-systems.org/version3/
Bash-4.3 contains the following new features (see the manual page for
complete descriptions and the CHANGES and NEWS files in the bash-4.3
distribution):
o The `helptopic' completion action now maps to all the help topics, not just
the shell builtins.
o The shell can be compiled to not display a message about processes that
terminate due to SIGTERM.
o In Posix mode, the `command' builtin does not change whether or not a
builtin it shadows is treated as an assignment builtin.
o The `return' and `exit' builtins accept negative exit status arguments.
o The history expansion character (!) does not cause history expansion when
followed by the closing quote in a double-quoted string.
o `complete' and its siblings compgen/compopt now takes a new `-o noquote'
option to inhibit quoting of the completions.
o Setting HISTSIZE to a value less than zero causes the history list to be
unlimited (setting it 0 zero disables the history list).
o Setting HISTFILESIZE to a value less than zero causes the history file size
to be unlimited (setting it to 0 causes the history file to be truncated
to zero size).
o There is a new `bind -X' option to print all key sequences bound to Unix
commands.
o The build process has a new mechanism for constructing separate help files
that better reflects the current set of compilation options.
o The -nt and -ot options to test now work with files with nanosecond
timestamp resolution.
o The shell saves the command history in any shell for which history is
enabled and HISTFILE is set, not just interactive shells.
o The shell has `nameref' variables and new -n(/+n) options to declare and
unset to use them, and a `test -R' option to test for them.
o The {x}<word redirection feature now allows words like {array[ind]} and
can use variables with special meanings to the shell (e.g., BASH_XTRACEFD).
o There is a new CHILD_MAX special shell variable; its value controls the
number of exited child statues the shell remembers.
o Bash does not do anything special to ensure that the file descriptor
assigned to X in {x}<foo remains open after the block containing it
completes.
o The `wait' builtin has a new `-n' option to wait for the next child to
change status.
o The `printf' %(...)T format specifier now uses the current time if no
argument is supplied.
o Shells started to run process substitutions now run any trap set on EXIT.
o `trap -p' will now show signals ignored at shell startup, though their
disposition still cannot be modified.
o $'...', echo, and printf understand \uXXXX and \UXXXXXXXX escape sequences.
o A new FUNCNEST variable to allow the user to control the maximum shell
function nesting (recursive execution) level.
o The mapfile builtin now supplies a third argument to the callback command:
the line about to be assigned to the supplied array index.
o The printf builtin has as new %(fmt)T specifier, which allows time values
to use strftime-like formatting.
o There is a new `lastpipe' shell option that runs the last command of a
pipeline in the current shell context. The lastpipe option has no
effect if job control is enabled.
o Posix-mode shells use the argument passed to `.' as-is if a $PATH search
fails, effectively searching the current directory. Posix-2008 change.
o Bash's file status checks (executable, readable, etc.) now take file
system ACLs into account on file systems that support them.
o Bash now passes environment variables with names that are not valid
shell variable names through into the environment passed to child
processes.
o New `complete -E' and `compopt -E' options that work on the "empty"
completion: completion attempted on an empty command line.
o The [[ and (( commands are now subject to the setting of `set -e' and the
ERR trap.
o The source/. builtin now removes NUL bytes from the file before attempting
to parse commands.
o New variable $BASH_XTRACEFD; when set to an integer bash will write xtrace
output to that file descriptor.
o The < and > operators to the [[ conditional command now do string
comparison according to the current locale.
o Programmable completion now uses the completion for `b' instead of `a'
when completion is attempted on a line like: a $(b c.
o The read builtin has a new `-N nchars' option, which reads exactly NCHARS
characters, ignoring delimiters like newline.
o The < and > operators to [[ do string comparisons using the current locale
only if the compatibility level is greater than 40 (set to 41 by default).
o When in readline vi command mode, repeatedly hitting ESC now does nothing,
even when ESC introduces a bound key sequence. This is closer to how
historical vi behaves.
o There is a new `autocd' option that, when enabled, causes bash to attempt
to `cd' to a directory name that is supplied as the first word of a
simple command.
o There is a new `checkjobs' option that causes the shell to check for and
report any running or stopped jobs at exit.
o The programmable completion code now uses the same set of characters as
readline when breaking the command line into a list of words.
o The block multiplier for the ulimit -c and -f options is now 512 when in
Posix mode, as Posix specifies.
o Changed the behavior of the read builtin to save any partial input received
in the specified variable when the read builtin times out. This also
results in variables specified as arguments to read to be set to the empty
string when there is no input available. When the read builtin times out,
it returns an exit status greater than 128.
o The `ulimit' builtin now has new -b (socket buffer size) and -T (number
of threads) options.
o The `read' builtin has a new -i option which inserts text into the reply
buffer when using readline.
o A new `-E' option to the complete builtin allows control of the default
behavior for completion on an empty line.
o There is now limited support for completing command name words containing
globbing characters.
o The `help' builtin now has a new -d option, to display a short description,
and a -m option, to print help information in a man page-like format.
o There is a new shell option: `globstar'. When enabled, the globbing code
treats `**' specially -- it matches all directories (and files within
them, when appropriate) recursively.
o The `-t' option to the `read' builtin now supports fractional timeout
values.
o The command assigned to a key sequence with `bind -x' now sets two new
variables in the environment of the executed command: READLINE_LINE_BUFFER
and READLINE_POINT. The command can change the current readline line
and cursor position by modifying READLINE_LINE_BUFFER and READLINE_POINT,
respectively.
o There is a new >>& redirection operator, which appends the standard output
and standard error to the named file.
o The parser now understands `|&' as a synonym for `2>&1 |', which redirects
the standard error for a command through a pipe.
o The new `;&' case statement action list terminator causes execution to
continue with the action associated with the next pattern in the
statement rather than terminating the command.
o The new `;;&' case statement action list terminator causes the shell to
test the next set of patterns after completing execution of the current
action, rather than terminating the command.
o The `declare' builtin now has new -l (convert value to lowercase upon
assignment) and -u (convert value to uppercase upon assignment) options.
There is an optionally-configurable -c option to capitalize a value at
assignment.
o Bash-3.2 now checks shell scripts for NUL characters rather than non-printing
characters when deciding whether or not a script is a binary file.
o Bash-3.1 may now be configured and built in a mode that enforces strict
POSIX compliance.
o Features to support the bash debugger have been implemented, and there
is a new `extdebug' option to turn the non-default options on
o Brace expansion has been extended with a new {x..y} form, producing
sequences of digits or characters
o Timestamps are now kept with history entries, with an option to save
and restore them from the history file; there is a new HISTTIMEFORMAT
variable describing how to display the timestamps when listing history
entries
o The `[[' command can now perform extended regular expression (egrep-like)
matching, with matched subexpressions placed in the BASH_REMATCH array
variable
o The `jobs', `kill', and `wait' builtins now accept job control notation
in their arguments even if job control is not enabled
o The `gettext' package and libintl have been integrated, and the shell
messages may be translated into other languages
o support for multibyte characters has been added to both bash and readline
o the DEBUG trap is now run *before* simple commands, ((...)) commands,
[[...]] conditional commands, and for ((...)) loops
o the shell now performs arithmetic in the largest integer size the machine
supports (intmax_t)
o `read' has a new `-u fd' option to read from a specified file descriptor
o the bash debugger in examples/bashdb has been modified to work with the
new DEBUG trap semantics, the command set has been made more gdb-like,
and the changes to $LINENO make debugging functions work better
o New `-A group/-g' option to complete and compgen; goes group name
completion
o New [+-]O invocation option to set and unset `shopt' options at startup
o `for' loops now allow empty word lists after the `in' reserved word
o Readline can be configured to place the user at the same point on the line
when retrieving commands from the history list
o This version has once again reverted to using locales and strcoll(3) when
processing pattern matching bracket expressions, as POSIX requires.
o Added a new `--init-file' invocation argument as a synonym for `--rcfile',
per the new GNU coding standards.
o The /dev/tcp and /dev/udp redirections now accept service names as well as
port numbers.
o `complete' and `compgen' now take a `-o value' option, which controls some
of the aspects of that compspec. Valid values are:
o Programmable word completion with the new `complete' and `compgen' builtins;
examples are provided in examples/complete/complete-examples
o `history' has a new `-d' option to delete a history entry
o `bind' has a new `-x' option to bind key sequences to shell commands
o The prompt expansion code has new `\j' and `\l' escape sequences
o The `no_empty_cmd_completion' shell option, if enabled, inhibits
command completion when TAB is typed on an empty line
o `help' has a new `-s' option to print a usage synopsis
o New arithmetic operators: var++, var--, ++var, --var, expr1,expr2 (comma)
o New ksh93-style arithmetic for command:
for ((expr1 ; expr2; expr3 )); do list; done
o `read' has new options: `-t', `-n', `-d', `-s'
o The redirection code handles several filenames specially: /dev/fd/N,
/dev/stdin, /dev/stdout, /dev/stderr
o The redirection code now recognizes /dev/tcp/HOST/PORT and
/dev/udp/HOST/PORT and tries to open a TCP or UDP socket, respectively,
to the specified port on the specified host
o The ${!prefix*} expansion has been implemented
o A new FUNCNAME variable, which expands to the name of a currently-executing
function
o The GROUPS variable is no longer readonly
o A new shopt `xpg_echo' variable, to control the behavior of echo with
respect to backslash-escape sequences at runtime
o The NON_INTERACTIVE_LOGIN_SHELLS #define has returned
Bash-2.03 had very few new features, in keeping with the convention
that odd-numbered releases provide mainly bug fixes. A number of new
features were added to Readline, mostly at the request of the Cygnus
folks.
There were lots of changes to the version of the Readline library released
along with Bash-2.03. For a complete list of the changes, read the file
CHANGES in the Bash-2.03 distribution.
a new version of malloc (based on the old GNU malloc code in previous
bash versions) that is more page-oriented, more conservative
with memory usage, does not `orphan' large blocks when they
are freed, is usable on 64-bit machines, and has allocation
checking turned on unconditionally
POSIX.2-style globbing character classes ([:alpha:], [:alnum:], etc.)
POSIX.2-style globbing equivalence classes
POSIX.2-style globbing collating symbols
the ksh [[...]] extended conditional command
the ksh egrep-style extended pattern matching operators
a new `printf' builtin
the ksh-like $(<filename) command substitution, which is equivalent to
$(cat filename)
new tilde prefixes that expand to directories from the directory stack
new `**' arithmetic operator to do exponentiation
case-insensitive globbing (filename expansion)
menu completion a la tcsh
`magic-space' history expansion function like tcsh
the readline inputrc `language' has a new file inclusion directive ($include)
new `GROUPS' builtin array variable containing the user's group list
new bindable readline commands: history-and-alias-expand-line and
alias-expand-line
implementation stuff:
autoconf-based configuration
nearly all of the bugs reported since version 1.14 have been fixed
most builtins converted to use builtin `getopt' for consistency
most builtins use -p option to display output in a reusable form
(for consistency)
grammar tighter and smaller (66 reduce-reduce conflicts gone)
lots of code now smaller and faster
test suite greatly expanded
C1) How does bash differ from sh, the Bourne shell?
Implementation differences:
redirection to/from compound commands causes sh to create a subshell
bash does not allow unbalanced quotes; sh silently inserts them at EOF
bash does not mess with signal 11
sh sets (euid, egid) to (uid, gid) if -p not supplied and uid < 100
bash splits only the results of expansions on IFS, using POSIX.2
field splitting rules; sh splits all words on IFS
sh does not allow MAILCHECK to be unset (?)
sh does not allow traps on SIGALRM or SIGCHLD
bash allows multiple option arguments when invoked (e.g. -x -v);
sh allows only a single option argument (`sh -x -v' attempts
to open a file named `-v', and, on SunOS 4.1.4, dumps core.
On Solaris 2.4 and earlier versions, sh goes into an infinite
loop.)
sh exits a script if any builtin fails; bash exits only if one of
the POSIX.2 `special' builtins fails
C2) How does bash differ from the Korn shell, version ksh88?
C3) Which new features in ksh-93 are not in bash, and which are?
Section D: Why does bash do some things differently than other Unix shells?
The csh script version reads the csh startup files from your
home directory and uses those to determine which `command' will
be invoked. Since bash doesn't use any of those startup files,
there's a good chance that your bash environment differs from
your csh environment. The bash `type' builtin does everything
`which' does, and will report correct results for the running
shell. If you're really wedded to the name `which', try adding
the following function definition to your .bashrc:
which()
{
builtin type "$@"
}
If you're moving from tcsh and would like to bring `where' along
as well, use this function:
where()
{
builtin type -a "$@"
}
D2) Why doesn't bash treat brace expansions exactly like csh?
The only difference between bash and csh brace expansion is that
bash requires a brace expression to contain at least one unquoted
comma if it is to be expanded. Any brace-surrounded word not
containing an unquoted comma is left unchanged by the brace
expansion code. This affords the greatest degree of sh
compatibility.
Bash, ksh, zsh, and pd-ksh all implement brace expansion this way.
${parameter%word}
Remove smallest suffix pattern. The WORD is expanded to produce
a pattern. It then expands to the value of PARAMETER, with the
smallest portion of the suffix matched by the pattern deleted.
x=file.c
echo ${x%.c}.o
-->file.o
${parameter%%word}
x=posix/src/std
echo ${x%%/*}
-->posix
${parameter#word}
Remove smallest prefix pattern. The WORD is expanded to produce
a pattern. It then expands to the value of PARAMETER, with the
smallest portion of the prefix matched by the pattern deleted.
x=$HOME/src/cmd
echo ${x#$HOME}
-->/src/cmd
${parameter##word}
Remove largest prefix pattern. The WORD is expanded to produce
a pattern. It then expands to the value of PARAMETER, with the
largest portion of the prefix matched by the pattern deleted.
x=/one/two/three
echo ${x##*/}
-->three
Given
a=/a/b/c/d
b=b.xxx
D4) How can I make my csh aliases work when I convert to bash?
D5) How can I pipe standard output and standard error from one command to
another, like csh does with `|&'?
Use
command 2>&1 | command2
D6) Now that I've converted from ksh to bash, are there equivalents to
ksh features like autoloaded functions and the `whence' command?
There are features in ksh-88 and ksh-93 that do not have direct bash
equivalents. Most, however, can be emulated with very little trouble.
Section E: How can I get bash to do certain things, and why does bash do
things the way it does?
E1) Why is the bash builtin `test' slightly different from /bin/test?
0 Args: False
1 Arg: True iff argument is not null.
2 Args: If first arg is !, True iff second argument is null.
If first argument is unary, then true if unary test is true
Otherwise error.
3 Args: If second argument is a binary operator, do binary test of $1 $3
If first argument is !, negate two argument test of $2 $3
If first argument is `(' and third argument is `)', do the
one-argument test of the second argument.
Otherwise error.
4 Args: If first argument is !, negate three argument test of $2 $3 $4.
Otherwise unspecified
5 or more Args: unspecified. (Historical shells would use their
current algorithm).
The operators -a and -o are considered binary operators for the purpose
of the 3 Arg case.
As you can see, the test becomes (not (x or x)), which is false.
ps -aux | head
`head' can finish before `ps' writes all of its output, and ps
will try to write on a pipe without a reader. In that case, bash
will print `Broken pipe' to stderr when ps is killed by a
SIGPIPE.
E3) When I have terminal escape sequences in my prompt, why does bash
wrap lines at the wrong column?
Readline, the line editing library that bash uses, does not know
that the terminal escape sequences do not take up space on the
screen. The redisplay code assumes, unless told otherwise, that
each character in the prompt is a `printable' character that
takes up one character position on the screen.
You can use the bash prompt expansion facility (see the PROMPTING
section in the manual page) to tell readline that sequences of
characters in the prompt strings take up no screen space.
E4) If I pipe the output of a command into `read variable', why doesn't
the output show up in $variable when the read command finishes?
#! /bin/sh
host `hostname` | awk '/address/ {print $NF}'
Instead of using
/usr/local/bin/ipaddr | read A B C D
OIFS="$IFS"
IFS=.
set -- $(/usr/local/bin/ipaddr)
IFS="$OIFS"
A="$1" B="$2" C="$3" D="$4"
This is the general approach -- in most cases you will not need to
set $IFS to a different value.
E6) Why doesn't a while or for loop get suspended when I type ^Z?
When you run a while or for loop, the only thing that the shell forks
and executes are any commands in the while loop test and commands in
the loop bodies. These, therefore, are the only things that can be
suspended when you type ^Z.
If you want to be able to stop the entire loop, you need to put it
within parentheses, which will force the loop into a subshell that
may be stopped (and subsequently restarted) as a single unit.
SUBDIRS = @SUBDIRS@
...
subdirs-clean:
for d in ${SUBDIRS}; do \
( cd $$d && ${MAKE} ${MFLAGS} clean ) \
done
When SUBDIRS is empty, this results in a command like this being passed to
bash:
for d in ; do
( cd $d && ${MAKE} ${MFLAGS} clean )
done
In versions of bash before bash-2.05a, this was a syntax error. If the
reserved word `in' was present, a word must follow it before the semicolon
or newline. The language in the manual page referring to the list of words
being empty referred to the list after it is expanded. These versions of
bash required that there be at least one word following the `in' when the
construct was parsed.
SUBDIRS = @SUBDIRS@
subdirs-clean:
subdirs=$SUBDIRS ; for d in $$subdirs; do \
( cd $$d && ${MAKE} ${MFLAGS} clean ) \
done
The latest updated POSIX standard has changed this: the word list
is no longer required. Bash versions 2.05a and later accept the
new syntax.
E8) Why does the arithmetic evaluation code complain about `08'?
The bash arithmetic evaluation code (used for `let', $(()), (()), and in
other places), interprets a leading `0' in numeric constants as denoting
an octal number, and a leading `0x' as denoting hexadecimal. This is
in accordance with the POSIX.2 spec, section 2.9.2.1, which states that
arithmetic constants should be handled as signed long integers as defined
by the ANSI/ISO C standard.
http://www.pasc.org/interps/unofficial/db/p1003.2/pasc-1003.2-173.html
E9) Why does the pattern matching expression [A-Z]* match files beginning
with every letter except `z'?
Bash-2.03, Bash-2.05 and later versions honor the current locale setting
when processing ranges within pattern matching bracket expressions ([A-Z]).
This is what POSIX.2 and SUSv3/XPG6 specify.
The behavior of the matcher in bash-2.05 and later versions depends on the
current LC_COLLATE setting. Setting this variable to `C' or `POSIX' will
result in the traditional behavior ([A-Z] matches all uppercase ASCII
characters). Many other locales, including the en_US locale (the default
on many US versions of Linux) collate the upper and lower case letters like
this:
AaBb...Zz
which means that [A-Z] matches every letter except `z'. Others collate like
aAbBcC...zZ
My advice is to put
export LC_COLLATE=C
into /etc/profile and inspect any shell scripts run from cron for
constructs like [A-Z]. This will prevent things like
rm [A-Z]*
from removing every file in the current directory except those beginning
with `z' and still allow individual users to change the collation order.
Users may put the above command into their own profiles as well, of course.
E11) If I resize my xterm while another program is running, why doesn't bash
notice the change?
When bash is running with job control enabled, each pipeline (which may be
a single command) is run in its own process group, different from bash's
process group. This foreground process group receives the SIGWINCH; bash
does not. Bash has no way of knowing that the terminal has been resized.
E12) Why don't negative offsets in substring expansion work like I expect?
When a negative `offset' begins with a minus sign, however, unexpected things
can happen. Consider
a=12345678
echo ${a:-4}
intending to print the last four characters of $a. The problem is that
${param:-word} already has a well-defined meaning: expand to word if the
expanded value of param is unset or null, and $param otherwise.
To use negative offsets that begin with a minus sign, separate the
minus sign and the colon with a space.
E13) Why does filename completion misbehave if a colon appears in the filename?
COMP_WORDBREAKS=${COMP_WORDBREAKS//:}
You can also quote the colon with a backslash to achieve the same result
temporarily.
E14) Why does quoting the pattern argument to the regular expression matching
conditional operator (=~) cause regexp matching to stop working?
Since the treatment of quoted string arguments was changed, several issues
have arisen, chief among them the problem of white space in pattern arguments
and the differing treatment of quoted strings between bash-3.1 and bash-3.2.
Both problems may be solved by using a shell variable to hold the pattern.
Since word splitting is not performed when expanding shell variables in all
operands of the [[ command, this allows users to quote patterns as they wish
when assigning the variable, then expand the values to a single string that
may contain whitespace. The first problem may be solved by using backslashes
or any other quoting mechanism to escape the white space in the patterns.
compat31 set
- the < and > operators to the [[ command do not consider the current
locale when comparing strings
- quoting the rhs of the regexp matching operator (=~) has no
special effect
compat32 set
- the < and > operators to the [[ command do not consider the current
locale when comparing strings
compat40 set
- the < and > operators to the [[ command do not consider the current
locale when comparing strings
- interrupting a command list such as "a ; b ; c" causes the execution
of the entire list to be aborted (in versions before bash-4.0,
interrupting one command in a list caused the next to be executed)
compat41 set
- interrupting a command list such as "a ; b ; c" causes the execution
of the entire list to be aborted (in versions before bash-4.1,
interrupting one command in a list caused the next to be executed)
- when in posix mode, single quotes in the `word' portion of a
double-quoted parameter expansion define a new quoting context and
are treated specially
compat42 set
- the replacement string in double-quoted pattern substitution is not
run through quote removal, as in previous versions
The problem is `cmdtool' and bash fighting over the input. When
scrolling is enabled in a cmdtool window, cmdtool puts the tty in
`raw mode' to permit command-line editing using the mouse for
applications that cannot do it themselves. As a result, bash and
cmdtool each try to read keyboard input immediately, with neither
getting enough of it to be useful.
`xterm' is a better choice, and gets along with bash much more
smoothly.
If you must use cmdtool, you can use the termcap description in
examples/suncmd.termcap. Set the TERMCAP variable to the terminal
description contained in that file, i.e.
TERMCAP='Mu|sun-cmd:am:bs:km:pt:li#34:co#80:cl=^L:ce=\E[K:cd=\E[J:rs=\E[s:'
Then export TERMCAP and start a new cmdtool window from that shell.
The bash command-line editing should behave better in the new
cmdtool. If this works, you can put the assignment to TERMCAP
in your bashrc file.
If you have installed the Sun C compiler, you may also need to
put /usr/ccs/bin and /opt/SUNWspro/bin into your $PATH before
/usr/ucb.
F3) Why does bash dump core after I interrupt username completion or
`~user' tilde expansion on a machine running NIS?
The YP library code keeps static state -- a pointer into the data
returned from the server. When YP initializes itself (setpwent),
it looks at this pointer and calls free on it if it's non-null.
So far, so good.
F4) I'm running SVR4.2. Why is the line erased every time I type `@'?
stty kill ^U
F5) Why does bash report syntax errors when my C News scripts use a
redirection before a subshell command?
F6) Why can't I use vi-mode editing on Red Hat Linux 6.1?
The long answer is that they shipped an /etc/inputrc that only works
for emacs mode editing, and then screwed all the vi users by setting
INPUTRC to /etc/inputrc in /etc/profile.
$if mode=emacs
[...]
$endif
The easiest fix, until HP fixes things up, is to edit the generated
config.h and #undef the HAVE_LONG_DOUBLE line. After doing that,
the compilation should complete successfully.
G1) How can I get bash to read and display eight-bit characters?
First, you must ensure that the `physical' data path is a full eight
bits. For xterms, for example, the `vt100' resources `eightBitInput'
and `eightBitOutput' should be set to `true'.
Once you have set up an eight-bit path, you must tell the kernel and
tty driver to leave the eighth bit of characters alone when processing
keyboard input. Use `stty' to do this:
stty pass8
Finally, you need to tell readline that you will be inputting and
displaying eight-bit characters. You use readline variables to do
this. convert-meta says what to do if you read a character with its
eighth bit set. input-meta says whether to permit characters with the
eighth bit at all. output-meta determines how to display characters
with the eighth bit set: if on, they are output directly; if it is off,
such characters are displayed as a meta-prefixed escape sequence.
The `set' commands between the single quotes may also be placed
in ~/.inputrc.
G2) How do I write a function `x' to replace builtin command `x', but
still invoke the command from within the function?
cd()
{
builtin cd "$@" && xtitle "$HOST: $PWD"
}
G3) How can I find the value of a shell variable whose name is the value
of another shell variable?
Versions of Bash newer than Bash-2.0 support this directly. You can use
${!var}
var1=var2
var2=z
echo ${!var1}
For example, this expression prints the value of the last positional
parameter:
echo ${!#}
does the same thing.
This is not the same thing as ksh93 `nameref' variables, though the syntax
is similar. Namerefs are available bash version 4.3, and work as in ksh93.
G4) How can I make the bash `time' reserved word print timing output that
looks like the output from my system's /usr/bin/time?
The bash command timing code looks for a variable `TIMEFORMAT' and
uses its value as a format string to decide how to display the
timing statistics.
TIMEFORMAT=$'\nreal\t%3lR\nuser\t%3lU\nsys\t%3lS'
TIMEFORMAT=$'\nreal\t%1R\nuser\t%1U\nsys\t%1S'
TIMEFORMAT=$'\nreal\t%2lR\nuser\t%2lU\nsys\t%2lS'
The \w expansion gives the full pathname of the current directory, with
a tilde (`~') substituted for the current value of $HOME. The \W
expansion gives the basename of the current directory. To put the full
pathname of the current directory into the path without any tilde
subsitution, use $PWD. Here are some examples:
The single quotes are important in the final example to prevent $PWD from
being expanded when the assignment to PS1 is performed.
Use the pattern removal functionality described in D3. The following `for'
loop will do the trick:
for f in *.foo; do
mv $f ${f%foo}bar
done
G8) How can I write a filename expansion (globbing) pattern that will match
all files in the current directory except "." and ".."?
You must have set the `extglob' shell option using `shopt -s extglob' to use
this:
echo .!(.|) *
A solution that works without extended globbing is given in the Unix Shell
FAQ, posted periodically to comp.unix.shell. It's a variant of
(The ..?* catches files with names of three or more characters beginning
with `..')
H1) How do I report bugs in bash, and where should I look for fixes and
advice?
Cameron Newham and Bill Rosenblatt have written a book on bash, published
by O'Reilly and Associates. The book is based on Bill Rosenblatt's Korn
Shell book. The title is ``Learning the Bash Shell'', and the ISBN number
of the third edition, published in March, 2005, is 0-596-00965-8. Look for
it in fine bookstores near you. This edition of the book has been updated
to cover bash-3.0.
The GNU Bash Reference Manual has been published as a printed book by
Network Theory Ltd (Paperback, ISBN: 0-9541617-7-7, Nov. 2006). It covers
bash-3.2 and is available from most online bookstores (see
http://www.network-theory.co.uk/bash/manual/ for details). The publisher
will donate $1 to the Free Software Foundation for each copy sold.
Arnold Robbins and Nelson Beebe have written ``Classic Shell Scripting'',
published by O'Reilly. The first edition, with ISBN number 0-596-00595-4,
was published in May, 2005.
These are features that may or may not appear in a future version of bash.
The next version will appear sometime in 2015. Never make predictions.