Adjusting Shell Behavior Using set, shopt, and Environment Variables

This section combines Table A-5, set Options, and shopt options and provides a quick way to look for what you can configure and which of the three mechanisms you use to configure it. The options are loosely grouped according to function or purpose, but it’s worthwhile to scan the entire table to get an overall sense of what you can configure.

The “Set option” column contains the options that can be turned on with the set -arg command. All are initially off except where noted. Items in the “Set full name” column, where listed, are arguments to set that can be used with set -o. The full names braceexpand, histexpand, history, keyword, and onecmd are not available in versions of bash prior to 2.0. Also, in those versions, hashing is switched with -d.

The “Shopt option” column shows the options set with shopt -s arg and unset with shopt -u arg. Versions of bash prior to 2.0 had environment variables to perform some of these settings. Setting them equated to shopt -s. The variables (and corresponding shopt options) were: allow_null_glob_expansion (nullglob), cdable_vars (cdable_vars), command_oriented_history (cmdhist), glob_dot_filenames (dotglob), no_exit_on_failed_exec (execfail). These variables no longer exist.

The options extdebug, failglob, force_fignore, and gnu_errfmt are not available in versions of bash prior to 3.0.

The “Environment variable” column lists environment variables that affect bash configuration and operation. The letters in the Type column of the table have the following meanings: A = Array, L = colon-separated list, R = read-only, U = unsetting it causes it to lose its special meaning.

Note that the variables beginning BASH_ and beginning COMP, as well as the variables DIRSTACK, FUNCNAME, GLOBIGNORE, GROUPS, HISTIGNORE, HOSTNAME, HISTTIMEFORMAT, LANG, LC_ALL, LC_COLLATE, LC_MESSAGE, MACHTYPE, PIPESTATUS, SHELLOPTS, and TIMEFORMAT are not available in versions prior to 2.0. BASH_ENV replaces ENV found in earlier versions.

Table A-8. Adjusting shell behavior using set, shopt, and environment variables

Set option

Set full name

Shopt option

Environment variable

Env. var. type

Description

   

COMP_CWORD

 

An index into ${COMPWORDS} of the word containing the current cursor position. This variable is available only in shell functions invoked by the programmable completion facilities.

   

COMP_LINE

 

The current command line. This variable is available only in shell functions and external commands invoked by the programmable completion facilities.

  

COMP_POINT

 

The index of the current cursor position relative to the beginning of the current command. If the current cursor position is at the end of the current command, the value of this variable is equal to ${#COMPLINE}. This variable is available only in shell functions and external commands invoked by the programmable completion facilities.

   

COMP_WORDBREAKS

U

The set of characters that the Readline library treats as word separators when performing word completion. If COMP_WORDBREAKS is unset, it loses its special properties, even if it is subsequently reset.

   

COMP_WORDS

A

An array of the individual words in the current command line. This variable is available only in shell functions invoked by the programmable completion facilities.

   

COMPREPLY

A

The possible completions generated by a shell function invoked by the programmable completion facility.

   

FIGNORE

L

A list of names to ignore when doing filename completion.

  

force_fignore

  

The suffixes specified by the FIGNORE shell variable cause words to be ignored when performing word completion even if the ignored words are the only possible completions.

  

hostcomplete

  

If readline is being used, an attempt will be made to perform hostname completion when a word beginning with @ is being completed.

   

HOSTFILE

 

The file to be used for hostname completion.

  

no_empty_cmd_completion

  

If readline is being used, no attempt will be made to search the PATH for possible completions when completion is attempted on an empty line.

  

progcomp

  

Programmable completion facilities are enabled. Default is on.

   

INPUTRC

 

The readline startup file.

-C

noclobber

   

Don’t allow redirection to overwrite existing files.

-t

onecmd

   

Exit after reading and executing one command.

-P

physical

   

Do not follow symbolic links on commands that change the current directory. Use the physical directory.

  

restricted_shell

  

Set if the shell is started in restricted mode. The value cannot be changed.

   

SHELLOPTS

LR

A list of enabled shell options.

  

sourcepath

  

The source built-in uses the value of $PATH to find the directory containing the file supplied as an argument.

   

BASH_ARGC

A

An array of values, which are the number of parameters in each frame of the current bash execution call stack. The number of parameters to the current subroutine (shell function or script executed with . or source) is at the top of the stack.

   

BASH_ARGV

A

All of the parameters in the current bash execution call stack. The final parameter of the last subroutine call is at the top of the stack; the first parameter of the initial call is at the bottom.

   

BASH_COMMAND

 

The command currently being executed or about to be executed, unless the shell is executing a command as the result of a trap, in which case it is the command executing at the time of the trap.

   

BASH_LINENO

A

An array whose members are the line numbers in source files corresponding to each member of @var{FUNCNAME}. ${BASHLINENO[$i]} is the line number in the source file where ${FUNCNAME[$i + 1]} was called. The corresponding source filename is ${BASHSOURCE[$i + 1]}.

   

BASH_SOURCE

A

An array containing the source filenames corresponding to the elements in the $FUNCNAME array variable.

-E

errtrace

   

Any trap on ERR is inherited by shell functions, command substitutions, and commands executed in a subshell environment.

  

extdebug

  

Behavior intended for use by debuggers is enabled. This includes: the -F option of declare displays the source filename and line number corresponding to each function name supplied as an argument; if the command run by the DEBUG trap returns a nonzero value, the next command is skipped and not executed; and if the command run by the DEBUG trap returns a value of 2, and the shell is executing in a subroutine, a call to return is simulated.

   

FUNCNAME

ARU

An array containing the names of all shell functions currently in the execution call stack. The element with index 0 is the name of any currently-executing shell function. The bottom-most element is “main.” This variable exists only when a shell function is executing.

-T

functrace

   

Any trap on DEBUG is inherited by shell functions, command substitutions, and commands executed in a subshell environment.

   

LINENO

U

The number of the line that just ran in a script or function.

-n

noexec

   

Read commands and check syntax but do not execute them. Ignored for interactive shells.

-v

verbose

   

Print shell input lines before running them.

-x

xtrace

   

Print commands (after expansions) before running them.

   

BASH_SUBSHELL

 

Incremented by 1 each time a subshell or subshell environment is spawned. The initial value is 0. A subshell is a forked copy of the parent shell and shares it’s environment.

  

SHLVL

 

Incremented by 1 each time a new instance (not a subshell) of bash is invoked. This is intended to be a count of how deeply your bash shells are nested.

-a

allexport

   

Export all subsequently defined or modified variables.

   

BASH_ENV

 

The name of a file to run as the environment file when the shell is invoked.

   

BASH_EXECUTION_STRING

 

The command argument to the -c invocation option.

   

BASH_VERSINFO

AR

Version information for this instance of bash. Each element of the array holds parts of the version number.

   

BASH_VERSION

 

The version number of this instance of bash.

   

-

R

Options given to the shell on invocation.

-

    

Signals the end of options. All remaining arguments are assigned to the positional parameters. -x and -v are turned off. If there are no remaining arguments to set, the positional arguments remain unchanged.

  

gnu_errfmt

  

Shell error messages are written in the standard GNU error message format.

   

HOME

 

The home (login) directory.

   

HOSTNAME

 

The name of the current host.

   

HOSTTYPE

 

The type of machine bash is running on.

  

huponexit

  

bash will send SIGHUP to all jobs when an interactive login shell exits.

--

    

With no arguments following, unset the positional parameters. Otherwise, the positional parameters are set to the following arguments (even if they begin with -).

   

IFS

 

The Internal Field Separator: a list of characters that act as word separators. Normally set to space, tab, and newline.

-k

keyword

   

Place keyword arguments in the environment for a command.

   

LANG

 

Used to determine the locale category for any category not specifically selected with a variable starting with LC_.

   

LC_ALL

 

Overrides the value of $LANG and any other LC_ variable specifying a locale category.

   

LC_COLLATE

 

Determines the collation order used when sorting the results of pathname expansion.

   

LC_CTYPE

 

Determines the interpretation of characters and the behavior of character classes within pathname expansion and pattern matching.

   

LC_MESSAGES

 

This variable determines the locale used to translate double-quoted strings preceded by a $.

   

LC_NUMERIC

 

Determines the locale category used for number formatting.

  

login_shell

  

If bash is started as a login shell. This is a read-only value.

   

MACHTYPE

 

A string describing the system on which bash is executing.

   

PATH

L

The search path for commands.

   

SECONDS

U

The number of seconds since the shell was invoked.

-B

braceexpand

   

The shell performs brace expansion. This is on by default.

  

dotglob

  

Filenames beginning with a . are included in pathname expansion.

  

expand_aliases

  

Aliases are expanded.

  

extglob

  

Extended pattern matching features are enabled.

  

failglob

  

Patterns that fail to match filenames during pathname expansion result in an expansion error.

   

GLOBIGNORE

L

A list of patterns defining filenames to ignore during pathname expansion.

 

nocaseglob

  

bash matches filenames in a caseinsensitive fashion when performing pathname expansion.

-f

noglob

   

Disable pathname expansion.

  

nullglob

  

Cause patterns that match no files to expand to null strings rather than to themselves.

  

checkhash

  

Commands found in the hash table are checked for existence before being executed, and nonexistence forces a $PATH search.

-h

hashall

   

Disable the hashing of commands.

  

cmdhist

  

Attempt to save all lines of a multiline command in a single history entry.

  

histappend

  

The history list is appended to the file named by the value of the variable $HISTFILE when the shell exits, rather than overwriting the file.

   

histchars

 

Specifies what to use as the history control characters. Normally set to the string !^#.

   

HISTCMD

U

The history number of the current command.

   

HISTCONTROL

 

A list of patterns, separated by colons (:), which can have the following values. ignorespace: lines beginning with a space are not entered into the history list. ignoredups: lines matching the last history line are not entered. erasedups: all previous lines matching the current line to are removed from the history list before the line is saved. ignoreboth: enables both ignorespace and ignoredups.

-H

histexpand

   

Enable ! style history substitution. On by default in an interactive shell.

   

HISTFILE

 

The name of the command history file.

   

HISTFILESIZE

 

The maximum number of lines kept in the history file.

   

HISTIGNORE

 

A list of patterns to decide what should be retained in the history list.

history

   

Enable command history. On by default in interactive shells.

  

histreedit

  

If readline is being used, the opportunity is given for re-editing a failed history substitution.

   

HISTSIZE

 

The number of lines kept in the command history.

   

HISTTIMEFOR MAT

 

If set and not null, its value is used as a format string for strftime(3) to print the timestamp associated with each history entry displayed by the history built-in. If this variable is set, timestamps are written to the history file so they may be preserved across shell sessions.

  

histverify

  

If readline is being used, the results of history substitution are not immediately passed to the shell parser. Instead, the resulting line is loaded into the readline editing buffer, allowing further modification.

  

lithist

  

If the cmdhist option is enabled, multiline commands are saved to the history with embedded newlines rather than using semicolon separators where possible.

   

IGNOREEOF

 

The number of EOF characters received before exiting an interactive shell.

 

ignoreeof

   

Disallow Ctrl-D to exit the shell.

  

cdable_vars

  

An argument to cd that is not a directory is assumed to be the name of a variable whose value is the directory to change to.

   

CDPATH

L

A list of directories for the cd command to search.

  

cdspell

  

Minor errors in the spelling of a directory supplied to the cd command will be corrected if there is a suitable match. This correction includes missing letters, incorrect letters, and letter transposition. It works for interactive shells only.

 

checkwinsize

  

Checks the window size after each command and, if it has changed, updates the variables $LINES and $COLUMNS accordingly.

   

DIRSTACK

ARU

The current contents of the directory stack.

 

emacs

   

Use Emacs-style command-line editing.

   

FCEDIT

 

The default editor for the fc command.

  

interactive_comments

  

Allows a word beginning with # and all subsequent characters on the line to be ignored in an interactive shell.

   

OLDPWD

 

The previous working directory.

   

PROMPT_COMMAND

 

The value is executed as a command before the primary prompt is issued.

  

promptvars

  

Prompt strings undergo variable and parameter expansion after being expanded.

   

PS1

 

The primary command prompt string.

   

PS2

 

The prompt string for line continuations.

   

PS3

 

The prompt string for the select command.

   

PS4

 

The prompt string for the xtrace option.

   

PWD

 

The current working directory.

  

shift_verbose

  

The shift built-in prints an error if it has shifted past the last positional parameter.

   

TIMEFORMAT

 

Specifies the format for the output from using the time reserved word on a command pipeline.

   

TMOUT

 

If set to a positive integer, the number of seconds after which the shell automatically terminates if no input is received.

   

-

R

Last argument to the previous command.

 

vi

   

Use vi-style command-line editing.

  

auto_resume

 

Controls how job control works (values are exact, substring, or something other than those keywords).

-m

monitor

   

Enable job control (on by default in interactive shells).

-b

notify

   

Report the status of terminating background jobs immediately.

   

MAIL

 

The name of the file to check for new mail.

   

MAILCHECK

 

How often (in seconds) to check for new mail.

   

MAILPATH

L

A list of file names to check for new mail, if $MAIL is not set.

  

mailwarn

  

If the file being checked for mail has been accessed since the last time it was checked, the message “The mail in mailfile has been read” is displayed.

 

pipefail

   

The return value of a pipeline is the value of the last (rightmost) command to exit with a nonzero status, or zero if all commands in the pipeline exit successfully. This option is disabled by default.

   

PIPESTATUS

A

An array variable containing a list of exit status values from the processes in the most recently executed foreground pipeline.

 

posix

   

Change the default behavior to that of POSIX 1003.2 where it differs from the standard.

   

POSIXLY_CORRECT

 

If in the environment when bash starts, the shell enters posix mode before reading the startup files, as if the --posix invocation option had been supplied. If it is set while the shell is running, bash enables posix mode, as if the command set -o posix had been executed.

  

xpg_echo

  

echo expands backslash-escape sequences by default.

  

BASH_REMATCH

AR

An array whose members are assigned by the =~ binary operator to the [[ conditional command. The element with index 0 is the portion of the string matching the entire regular expression. The element with index n is the portion of the string matching the nth parenthesized subexpression.

   

0

R

Name of the shell or shell script.

   

*

R

A single string containing the positional parameters given to the current script or function, separated by the first character of $IFS (e.g., arg1 arg2 arg3).

   

@

R

Each of the positional parameters given to the current script or function, given as a list of double-quoted strings (e.g., "arg1" "arg2" "arg3").

   

BASH

 

The full pathname used to invoke this instance of bash.

   

$

R

Process ID of the shell process.

-e

errexit

   

Exit the shell when a simple command exits with nonzero status. A simple command is a command not part of a while, until, or if; nor part of a && or || list; nor a command whose return value is inverted by !.

   

EUID

R

The effective user ID of the current user.

   

!

R

Process ID of the last background command.

  

execfail

  

A noninteractive shell will not exit if it cannot execute the argument to an exec. Interactive shells do not exit if exec fails.

   

GROUPS

AR

An array containing a list of groups of which the current user is a member.

-u

nounset

   

Treat undefined variables as errors, not as null.

   

OPTARG

 

The value of the last option argument processed by getopts.

   

OPTERR

 

If set to 1, display error messages from getopts.

  

OPTIND

 

The number of the first argument after options.

   

OSTYPE

 

The operating system on which bash is executing.

   

#

R

The number of arguments given to the current script or function.

   

PPID

R

The process ID of the parent process.

-p

privileged

   

Script is running in suid mode.

   

?

R

Exit status of the previous command.

   

RANDOM

U

A random number between 0 and 32767 (2^15 - 1).

   

REPLY

 

The user’s response to the select command; result of the read command if no variable names are given.

   

SHELL

 

The full pathname of the shell.

   

UID

R

The user ID of the current user.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.184.90