Filters
Zo zoek je
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • "twee woorden" vindt precies die zin.
  • --naam of -x vindt een optie.
  • #woord zoekt alleen in commando's.
  • @grep vindt lessen en labs die grep gebruiken.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Zet ! voor een woord om het weg te laten. OR tussen woorden betekent één ervan.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken

set

Turn shell options on or off (like -x trace, -e stop on error), set the positional parameters, or list all variables.

Niveau
Dagelijks Linux
Groep
Shell en hulp
Root nodig
Nee
Pakket
bash (built-in)

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

Wat het doet

set -OPTION turns an option on, set +OPTION turns it off. In scripts the most used line is set -euo pipefail (stop on errors, error on unset variables, fail on broken pipes). set -- a b c sets $1 $2 $3. Without arguments, set prints all shell variables and functions.

Zo schrijf je het

set [-abefhkmnptuvxBCEHPT] [-o OPTION] [--] [ARGUMENT]...

Opties 23

Meest gebruikt

-e, -o errexit
Exit when a command fails.
-u, -o nounset
Error when an unset variable is used.
-x, -o xtrace
Print commands before running them (trace).
-o pipefail
A pipe fails if any command in it fails.
-v, -o verbose
Print lines as they are read.
-n, -o noexec
Read commands but do not run them.
-f, -o noglob
No wildcard expansion.
-C, -o noclobber
> does not overwrite files (use >| to force).
-a, -o allexport
Export all new variables.
-b, -o notify
Report finished jobs at once.
-h, -o hashall
Remember command locations.
-k, -o keyword
All assignments go to the environment.
-m, -o monitor
Job control.
-p, -o privileged
Privileged mode.
-t, -o onecmd
Exit after one command.
-B, -o braceexpand
Brace expansion (on by default).
-E, -o errtrace
ERR traps in functions.
-H, -o histexpand
! history expansion.
-P, -o physical
No symlinks for cd and pwd.
-T, -o functrace
DEBUG/RETURN traps in functions.
-o vi, -o emacs
Editing mode of the command line.
-o
List all options and their state.
--
Set the positional parameters (or clear them).

Voorbeelden 10

$ als gewone gebruiker# als root (of met sudo)

  1. 1Strict mode for scripts

    head -2 /usr/local/bin/backup.sh
    #!/bin/bash
    set -euo pipefail

    Many hidden errors become visible.

  2. 2Trace part of a script

    set -x; cp a b; set +x
    + cp a b

    Only these lines are traced.

  3. 3Stop on errors

    bash -c 'set -e; false; echo "not reached"'

    The script ends at false.

  4. 4Unset variable error

    bash -c 'set -u; echo "$TYPO"'
    bash: line 1: TYPO: unbound variable

    Catches typos like rm -rf "$DIR/" with an empty DIR.

  5. 5Pipe failures

    bash -c 'set -o pipefail; false | true; echo $?'
    1

    Without pipefail the result would be 0.

  6. 6Do not overwrite files

    set -C; echo x > existing.txt
    bash: existing.txt: cannot overwrite existing file

    Protects against a wrong >.

  7. 7vi mode on the command line

    set -o vi

    Esc, then vi keys to edit.

  8. 8Show all options

    set -o | head

    on/off for each.

  9. 9Set positional parameters

    set -- one two three; echo "$2 / $#"
    two / 3

    Useful in tests and functions.

  10. 10List variables and functions

    set | grep ^HIST

    All HIST variables.

Exitcodes

0 :: Done. 1 :: Wrong option.

Volledige handleiding

Deze pagina legt het commando uit in eenvoudige woorden. De originele handleiding heeft elk detail.

In je terminal: man set