1SET(P)                     POSIX Programmer's Manual                    SET(P)
2
3
4

NAME

6       set - set or unset options and positional parameters
7

SYNOPSIS

9       set [-abCefmnuvx][-h][-o option][argument...]
10
11       set [+abCefmnuvx][+h][+o option][argument...]
12
13       set -- [argument...]
14
15       set -o
16
17       set +o
18
19

DESCRIPTION

21       If no options or arguments are specified, set shall write the names and
22       values of all shell variables in the collation sequence of the  current
23       locale. Each name shall start on a separate line, using the format:
24
25
26              "%s=%s\n", <name>, <value>
27
28       The  value  string  shall  be written with appropriate quoting; see the
29       description of shell quoting in Quoting . The output shall be  suitable
30       for reinput to the shell, setting or resetting, as far as possible, the
31       variables that are currently set; read-only variables cannot be reset.
32
33       When options are specified, they shall set or unset attributes  of  the
34       shell,  as  described  below.  When arguments are specified, they cause
35       positional parameters to be set or unset, as described  below.  Setting
36       or  unsetting  attributes and positional parameters are not necessarily
37       related actions, but they can be combined in  a  single  invocation  of
38       set.
39
40       The  set  special built-in shall support the Base Definitions volume of
41       IEEE Std 1003.1-2001, Section 12.2, Utility  Syntax  Guidelines  except
42       that  options  can  be  specified with either a leading hyphen (meaning
43       enable the option) or plus sign (meaning disable it)  unless  otherwise
44       specified.
45
46       Implementations shall support the options in the following list in both
47       their hyphen and plus-sign forms. These options can also  be  specified
48       as options to sh.
49
50       -a     When  this  option  is on, the export attribute shall be set for
51              each variable to which an assignment is performed; see the  Base
52              Definitions  volume of IEEE Std 1003.1-2001, Section 4.21, Vari‐
53              able Assignment. If the assignment precedes a utility name in  a
54              command,  the  export attribute shall not persist in the current
55              execution environment after  the  utility  completes,  with  the
56              exception  that  preceding one of the special built-in utilities
57              causes the export attribute to persist after  the  built-in  has
58              completed.  If the assignment does not precede a utility name in
59              the command, or if the assignment is a result of  the  operation
60              of  the  getopts  or  read utilities, the export attribute shall
61              persist until the variable is unset.
62
63       -b     This option shall be supported if  the  implementation  supports
64              the  User Portability Utilities option. It shall cause the shell
65              to notify the user asynchronously of background job completions.
66              The following message is written to standard error:
67
68
69              "[%d]%c %s%s\n", <job-number>, <current>, <status>, <job-name>
70
71       where the fields shall be as follows:
72
73       <current>
74              The  character  '+'  identifies  the job that would be used as a
75              default for the fg or bg utilities; this job can also be  speci‐
76              fied  using  the job_id "%+" or "%%" . The character '-' identi‐
77              fies the job that  would  become  the  default  if  the  current
78              default  job  were to exit; this job can also be specified using
79              the job_id "%-" . For other jobs, this field is  a  <space>.  At
80              most  one job can be identified with '+' and at most one job can
81              be identified with '-' . If there is any suspended job, then the
82              current  job shall be a suspended job. If there are at least two
83              suspended jobs, then the previous job also shall be a  suspended
84              job.
85
86       <job-number>
87              A  number  that can be used to identify the process group to the
88              wait, fg, bg, and kill utilities. Using these utilities, the job
89              can be identified by prefixing the job number with '%' .
90
91       <status>
92              Unspecified.
93
94       <job-name>
95              Unspecified.
96
97
98       When  the  shell  notifies  the  user  a job has been completed, it may
99       remove the job's process ID from the list of those known in the current
100       shell  execution  environment;  see  Asynchronous  Lists . Asynchronous
101       notification shall not be enabled by default.
102
103       -C     (Uppercase C.) Prevent existing files from being overwritten  by
104              the  shell's '>' redirection operator (see Redirecting Output );
105              the ">|" redirection  operator  shall  override  this  noclobber
106              option for an individual file.
107
108       -e     When this option is on, if a simple command fails for any of the
109              reasons listed in Consequences of Shell  Errors  or  returns  an
110              exit  status value >0, and is not part of the compound list fol‐
111              lowing a while, until, or if keyword, and is not a  part  of  an
112              AND or OR list, and is not a pipeline preceded by the ! reserved
113              word, then the shell shall immediately exit.
114
115       -f     The shell shall disable pathname expansion.
116
117       -h     Locate and remember utilities  invoked  by  functions  as  those
118              functions  are  defined (the utilities are normally located when
119              the function is executed).
120
121       -m     This option shall be supported if  the  implementation  supports
122              the  User Portability Utilities option. All jobs shall be run in
123              their own process groups. Immediately before the shell issues  a
124              prompt after completion of the background job, a message report‐
125              ing the exit status of the background job shall  be  written  to
126              standard error. If a foreground job stops, the shell shall write
127              a message  to  standard  error  to  that  effect,  formatted  as
128              described  by  the  jobs utility.  In addition, if a job changes
129              status other than exiting (for example, if it stops for input or
130              output or is stopped by a SIGSTOP signal), the shell shall write
131              a similar message immediately prior to writing the next  prompt.
132              This option is enabled by default for interactive shells.
133
134       -n     The  shell  shall  read commands but does not execute them; this
135              can be used to check for shell script syntax errors. An interac‐
136              tive shell may ignore this option.
137
138       -o     Write  the current settings of the options to standard output in
139              an unspecified format.
140
141       +o     Write the current option settings to standard output in a format
142              that  is  suitable  for  reinput  to  the shell as commands that
143              achieve the same options settings.
144
145       -o  option
146
147              This option is supported if the system supports the User  Porta‐
148              bility  Utilities  option. It shall set various options, many of
149              which shall be equivalent to the single option letters. The fol‐
150              lowing values of option shall be supported:
151
152       allexport
153              Equivalent to -a.
154
155       errexit
156              Equivalent to -e.
157
158       ignoreeof
159              Prevent  an  interactive shell from exiting on end-of-file. This
160              setting prevents accidental logouts when <control>-D is entered.
161              A user shall explicitly exit to leave the interactive shell.
162
163       monitor
164              Equivalent  to  -m.  This option is supported if the system sup‐
165              ports the User Portability Utilities option.
166
167       noclobber
168              Equivalent to -C (uppercase C).
169
170       noglob
171              Equivalent to -f.
172
173       noexec
174              Equivalent to -n.
175
176       nolog
177              Prevent the entry of function definitions into the command  his‐
178              tory; see Command History List .
179
180       notify
181              Equivalent to -b.
182
183       nounset
184              Equivalent to -u.
185
186       verbose
187              Equivalent to -v.
188
189       vi
190              Allow  shell  command line editing using the built-in vi editor.
191              Enabling vi mode shall disable any other  command  line  editing
192              mode provided as an implementation extension.
193
194              It need not be possible to set vi mode on for certain block-mode
195              terminals.
196
197       xtrace
198              Equivalent to -x.
199
200
201       -u     The shell shall write a message to standard error when it  tries
202              to  expand  a  variable that is not set and immediately exit. An
203              interactive shell shall not exit.
204
205       -v     The shell shall write its input to standard error as it is read.
206
207       -x     The shell shall write to standard error a trace for each command
208              after  it  expands  the command and before it executes it. It is
209              unspecified whether  the  command  that  turns  tracing  off  is
210              traced.
211
212
213       The  default  for  all these options shall be off (unset) unless stated
214       otherwise in the description of the option  or  unless  the  shell  was
215       invoked with them on; see sh.
216
217       The  remaining  arguments  shall be assigned in order to the positional
218       parameters. The special parameter '#' shall be set to reflect the  num‐
219       ber  of positional parameters. All positional parameters shall be unset
220       before any new values are assigned.
221
222       The special argument "--" immediately following the  set  command  name
223       can  be used to delimit the arguments if the first argument begins with
224       '+' or '-' , or to prevent inadvertent listing of all  shell  variables
225       when  there are no arguments. The command set -- without argument shall
226       unset all positional parameters and set the special  parameter  '#'  to
227       zero.
228

OPTIONS

230       See the DESCRIPTION.
231

OPERANDS

233       See the DESCRIPTION.
234

STDIN

236       Not used.
237

INPUT FILES

239       None.
240

ENVIRONMENT VARIABLES

242       None.
243

ASYNCHRONOUS EVENTS

245       Default.
246

STDOUT

248       See the DESCRIPTION.
249

STDERR

251       The standard error shall be used only for diagnostic messages.
252

OUTPUT FILES

254       None.
255

EXTENDED DESCRIPTION

257       None.
258

EXIT STATUS

260       Zero.
261

CONSEQUENCES OF ERRORS

263       Default.
264
265       The following sections are informative.
266

APPLICATION USAGE

268       None.
269

EXAMPLES

271       Write out all variables and their values:
272
273
274              set
275
276       Set $1, $2, and $3 and set "$#" to 3:
277
278
279              set c a b
280
281       Turn on the -x and -v options:
282
283
284              set -xv
285
286       Unset all positional parameters:
287
288
289              set --
290
291       Set $1 to the value of x, even if it begins with '-' or '+' :
292
293
294              set -- "$x"
295
296       Set  the positional parameters to the expansion of x, even if x expands
297       with a leading '-' or '+' :
298
299
300              set -- $x
301

RATIONALE

303       The set -- form is listed specifically in the SYNOPSIS even though this
304       usage  is  implied by the Utility Syntax Guidelines. The explanation of
305       this feature removes any ambiguity about whether the set -- form  might
306       be  misinterpreted  as  being  equivalent to set without any options or
307       arguments. The functionality of this form has  been  adopted  from  the
308       KornShell.  In  System  V, set -- only unsets parameters if there is at
309       least one argument; the only way to unset  all  parameters  is  to  use
310       shift.  Using  the KornShell version should not affect System V scripts
311       because there should be no reason to issue it without arguments  delib‐
312       erately; if it were issued as, for example:
313
314
315              set -- "$@"
316
317       and there were in fact no arguments resulting from "$@" , unsetting the
318       parameters would have no result.
319
320       The set + form in early proposals was omitted as being  an  unnecessary
321       duplication of set alone and not widespread historical practice.
322
323       The  noclobber option was changed to allow set -C as well as the set -o
324       noclobber option. The single-letter version was added so that the  his‐
325       torical "$-" paradigm would not be broken; see Special Parameters .
326
327       The  -h flag is related to command name hashing and is only required on
328       XSI-conformant systems.
329
330       The following set flags were omitted intentionally with  the  following
331       rationale:
332
333       -k     The  -k  flag  was  originally added by the author of the Bourne
334              shell to make it easier for users of pre-release versions of the
335              shell.  In  early versions of the Bourne shell the construct set
336              name= value had to be used to assign values to shell  variables.
337              The problem with -k is that the behavior affects parsing, virtu‐
338              ally precluding writing any compilers. To explain  the  behavior
339              of  -k, it is necessary to describe the parsing algorithm, which
340              is implementation-defined. For example:
341
342
343              set -k; echo name=value
344
345       and:
346
347
348              set -k
349              echo name=value
350
351       behave differently. The interaction with functions is  even  more  com‐
352       plex.   What  is  more,  the -k flag is never needed, since the command
353       line could have been reordered.
354
355       -t     The -t flag is hard to specify and almost never used.  The  only
356              known  use  could  be  done  with  here-documents. Moreover, the
357              behavior with ksh and sh differs. The reference page  says  that
358              it  exits  after  reading and executing one command. What is one
359              command? If the input is date; date, sh executes both date  com‐
360              mands while ksh does only the first.
361
362
363       Consideration was given to rewriting set to simplify its confusing syn‐
364       tax. A specific suggestion was that the unset utility should be used to
365       unset options instead of using the non- getopt() -able + option syntax.
366       However, the conclusion was reached that  the  historical  practice  of
367       using + option was satisfactory and that there was no compelling reason
368       to modify such widespread historical practice.
369
370       The -o option was adopted from the KornShell to address user needs.  In
371       addition  to  its generally friendly interface, -o is needed to provide
372       the vi command line editing mode, for which historical practice  yields
373       no  single-letter option name. (Although it might have been possible to
374       invent such a letter, it was recognized that other editing modes  would
375       be  developed  and  -o  provides  ample  name space for describing such
376       extensions.)
377
378       Historical implementations are inconsistent in the format used  for  -o
379       option  status  reporting. The +o format without an option-argument was
380       added to allow portable access to the options that  can  be  saved  and
381       then later restored using, for instance, a dot script.
382
383       Historically, sh did trace the command set +x, but ksh did not.
384
385       The  ignoreeof setting prevents accidental logouts when the end-of-file
386       character (typically <control>-D) is entered. A user  shall  explicitly
387       exit to leave the interactive shell.
388
389       The set -m option was added to apply only to the UPE because it applies
390       primarily to interactive use, not shell script applications.
391
392       The ability to do asynchronous notification  became  available  in  the
393       1988  version of the KornShell. To have it occur, the user had to issue
394       the command:
395
396
397              trap "jobs -n" CLD
398
399       The C shell provides two different levels of an asynchronous  notifica‐
400       tion  capability.  The environment variable notify is analogous to what
401       is done in set -b or set -o notify. When  set,  it  notifies  the  user
402       immediately  of background job completions. When unset, this capability
403       is turned off.
404
405       The other notification  ability  comes  through  the  built-in  utility
406       notify. The syntax is:
407
408
409              notify [%job ... ]
410
411       By issuing notify with no operands, it causes the C shell to notify the
412       user asynchronously when the state of the current job changes. If given
413       operands,  notify  asynchronously  informs  the  user of changes in the
414       states of the specified jobs.
415
416       To add asynchronous notification to the POSIX shell, neither the  Korn‐
417       Shell  extensions  to trap, nor the C shell notify environment variable
418       seemed appropriate ( notify is not a proper POSIX environment  variable
419       name).
420
421       The set -b option was selected as a compromise.
422
423       The  notify built-in was considered to have more functionality than was
424       required for simple asynchronous notification.
425

FUTURE DIRECTIONS

427       None.
428

SEE ALSO

430       Special Built-In Utilities
431
433       Portions of this text are reprinted and reproduced in  electronic  form
434       from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
435       -- Portable Operating System Interface (POSIX),  The  Open  Group  Base
436       Specifications  Issue  6,  Copyright  (C) 2001-2003 by the Institute of
437       Electrical and Electronics Engineers, Inc and The Open  Group.  In  the
438       event of any discrepancy between this version and the original IEEE and
439       The Open Group Standard, the original IEEE and The Open Group  Standard
440       is  the  referee document. The original Standard can be obtained online
441       at http://www.opengroup.org/unix/online.html .
442
443
444
445IEEE/The Open Group                  2003                               SET(P)
Impressum