Skip to main content
Improved punctuation and grammar; and you can't trap KILL.
Source Link

This is explained some in the trap(1P) man page and more in the Advanced Bash Scripting Guide, which explains that the -- is a Bash built-in, used to denote the "end of options" for a command. Since scripts using sh are designed to be portable between other systems with it, bash behaves as if it were executed with --posix, which changes some shell behavior to match the POSIX specification.

When a signal is "trapped", (e.g., EXIT), it binds the first argument after the command to the signal, eval-ing it when the signal is issued, and ignoring its normal behavior (except for EXIT). So when trap 'echo something' EXIT is ranrun, then exit, the shell will eval 'echo something' prior to exiting. This would also work with a different signal, such as KILLTERM, which could be bound to, for example, a graceful exit function in a script. When trap -- EXIT is ranrun, the -- is interpreted as an "end of arguments", indicating to traptrap that the signal is to be bound to null ('') (since there were no flags prior to or after --), indicating to the shell to ignore the signal (however, this doesn't work with EXIT, but works with other signals). Running trap -- 'echo something' EXIT, however, will still eval 'echo something' and exit upon exit. As per the specification of trap, the command - by itself indicates that the shell will reset any traps for the signals specified, which is why it works in both sh and Bash.

This is explained some in the trap(1P) man page and more in the Advanced Bash Scripting Guide, which explains that the -- is a Bash built-in, used to denote the "end of options" for a command. Since scripts using sh are designed to be portable between other systems with it, bash behaves as if it were executed with --posix, which changes some shell behavior to match the POSIX specification.

When a signal is "trapped", (e.g. EXIT), it binds the first argument after the command to the signal, eval-ing it when the signal is issued, and ignoring its normal behavior (except for EXIT). So when trap 'echo something' EXIT is ran, then exit, the shell will eval 'echo something' prior to exiting. This would also work with a different signal, such as KILL, which could be bound to, for example, a graceful exit function in a script. When trap -- EXIT is ran, the -- is interpreted as an "end of arguments", indicating to trap that the signal is to be bound to null ('') (since there were no flags prior to or after --), indicating to the shell to ignore the signal (however, this doesn't work with EXIT, but works with other signals). Running trap -- 'echo something' EXIT, however, will still eval 'echo something' and exit upon exit. As per the specification of trap, the command - by itself indicates that the shell will reset any traps for the signals specified, which is why it works in both sh and Bash.

This is explained some in the trap(1P) man page and more in the Advanced Bash Scripting Guide, which explains that the -- is a Bash built-in, used to denote the "end of options" for a command. Since scripts using sh are designed to be portable between other systems with it, bash behaves as if it were executed with --posix, which changes some shell behavior to match the POSIX specification.

When a signal is "trapped", (e.g., EXIT), it binds the first argument after the command to the signal, eval-ing it when the signal is issued, and ignoring its normal behavior (except for EXIT). So when trap 'echo something' EXIT is run, then exit, the shell will eval 'echo something' prior to exiting. This would also work with a different signal, such as TERM, which could be bound to, for example, a graceful exit function in a script. When trap -- EXIT is run, the -- is interpreted as an "end of arguments", indicating to trap that the signal is to be bound to null ('') (since there were no flags prior to or after --), indicating to the shell to ignore the signal (however, this doesn't work with EXIT, but works with other signals). Running trap -- 'echo something' EXIT, however, will still eval 'echo something' and exit upon exit. As per the specification of trap, the command - by itself indicates that the shell will reset any traps for the signals specified, which is why it works in both sh and Bash.

bash as sh does not disable all shell built-ins
Source Link
jordanm
  • 43.6k
  • 10
  • 121
  • 115

This is explained some in the trap(1P) man page and more in the Advanced Bash Scripting Guide, which explains that the -- is a Bash built-in, used to denote the "end of options" for a command. Since scripts using sh are designed to be portable between other systems with it, bash behaves as if it doesn't include any Bash built-inswere executed with --posix, which aren't part ofchanges some shell behavior to match the officialPOSIX specification which would break portability.

When a signal is "trapped", (e.g. EXIT), it binds the first argument after the command to the signal, eval-ing it when the signal is issued, and ignoring its normal behavior (except for EXIT). So when trap 'echo something' EXIT is ran, then exit, the shell will eval 'echo something' prior to exiting. This would also work with a different signal, such as KILL, which could be bound to, for example, a graceful exit function in a script. When trap -- EXIT is ran, the -- is interpreted as an "end of arguments", indicating to trap that the signal is to be bound to null ('') (since there were no flags prior to or after --), indicating to the shell to ignore the signal (however, this doesn't work with EXIT, but works with other signals). Running trap -- 'echo something' EXIT, however, will still eval 'echo something' and exit upon exit. As per the specification of trap, the command - by itself indicates that the shell will reset any traps for the signals specified, which is why it works in both sh and Bash.

This is explained some in the trap(1P) man page and more in the Advanced Bash Scripting Guide, which explains that the -- is a Bash built-in, used to denote the "end of options" for a command. Since scripts using sh are designed to be portable between other systems with it, it doesn't include any Bash built-ins which aren't part of the official specification which would break portability.

When a signal is "trapped", (e.g. EXIT), it binds the first argument after the command to the signal, eval-ing it when the signal is issued, and ignoring its normal behavior (except for EXIT). So when trap 'echo something' EXIT is ran, then exit, the shell will eval 'echo something' prior to exiting. This would also work with a different signal, such as KILL, which could be bound to, for example, a graceful exit function in a script. When trap -- EXIT is ran, the -- is interpreted as an "end of arguments", indicating to trap that the signal is to be bound to null ('') (since there were no flags prior to or after --), indicating to the shell to ignore the signal (however, this doesn't work with EXIT, but works with other signals). Running trap -- 'echo something' EXIT, however, will still eval 'echo something' and exit upon exit. As per the specification of trap, the command - by itself indicates that the shell will reset any traps for the signals specified, which is why it works in both sh and Bash.

This is explained some in the trap(1P) man page and more in the Advanced Bash Scripting Guide, which explains that the -- is a Bash built-in, used to denote the "end of options" for a command. Since scripts using sh are designed to be portable between other systems with it, bash behaves as if it were executed with --posix, which changes some shell behavior to match the POSIX specification.

When a signal is "trapped", (e.g. EXIT), it binds the first argument after the command to the signal, eval-ing it when the signal is issued, and ignoring its normal behavior (except for EXIT). So when trap 'echo something' EXIT is ran, then exit, the shell will eval 'echo something' prior to exiting. This would also work with a different signal, such as KILL, which could be bound to, for example, a graceful exit function in a script. When trap -- EXIT is ran, the -- is interpreted as an "end of arguments", indicating to trap that the signal is to be bound to null ('') (since there were no flags prior to or after --), indicating to the shell to ignore the signal (however, this doesn't work with EXIT, but works with other signals). Running trap -- 'echo something' EXIT, however, will still eval 'echo something' and exit upon exit. As per the specification of trap, the command - by itself indicates that the shell will reset any traps for the signals specified, which is why it works in both sh and Bash.

added 201 characters in body
Source Link

This is explained some in the trap(1P) man page and more in the Advanced Bash Scripting Guide, which explains that the -- is a Bash built-in, used to denote the "end of options" for a command. Since scripts using sh are designed to be portable between other systems with it, it doesn't include any Bash built-ins which aren't part of the official specification which would break portability.

When a signal is "trapped", (e.g. EXIT), it binds the first argument after the command to the signal, eval-ing it when the signal is issued, and ignoring its normal behavior (except for EXIT). So when trap 'echo something' EXIT is ran, then exit, the shell will eval 'echo something' prior to exiting. This would also work with a different signal, such as KILL, which could be bound to, for example, a graceful exit function in a script. When trap -- EXIT is ran, the -- is interpreted as an "end of arguments", indicating to trap that the signal is to be bound to null ('') (since there were no flags prior to or after --), indicating to the shell to ignore the signal (however, this doesn't work with EXIT, but works with other signals). Running trap -- 'echo something' EXIT, however, will still eval 'echo something' and exit upon exit. As per the implementationspecification of trap, the command - by itself indicates that the shell will reset any traps for the signals specified, which is why it works in both sh and Bash.

This is explained some in the trap(1P) man page and more in the Advanced Bash Scripting Guide, which explains that the -- is a Bash built-in, used to denote the "end of options" for a command. Since scripts using sh are designed to be portable between other systems with it, it doesn't include any Bash built-ins which aren't part of the official specification which would break portability.

When a signal is "trapped", (e.g. EXIT), it binds the first argument after the command to the signal, eval-ing it when the signal is issued, and ignoring its normal behavior (except for EXIT). So when trap 'echo something' EXIT is ran, then exit, the shell will eval 'echo something' prior to exiting. This would also work with a different signal, such as KILL, which could be bound to, for example, a graceful exit function in a script. When trap -- EXIT is ran, the -- is interpreted as an "end of arguments", indicating to trap that the signal is to be bound to null (''), indicating to the shell to ignore the signal (however, this doesn't work with EXIT, but works with other signals). As per the implementation of trap, the command - by itself indicates that the shell will reset any traps for the signals specified.

This is explained some in the trap(1P) man page and more in the Advanced Bash Scripting Guide, which explains that the -- is a Bash built-in, used to denote the "end of options" for a command. Since scripts using sh are designed to be portable between other systems with it, it doesn't include any Bash built-ins which aren't part of the official specification which would break portability.

When a signal is "trapped", (e.g. EXIT), it binds the first argument after the command to the signal, eval-ing it when the signal is issued, and ignoring its normal behavior (except for EXIT). So when trap 'echo something' EXIT is ran, then exit, the shell will eval 'echo something' prior to exiting. This would also work with a different signal, such as KILL, which could be bound to, for example, a graceful exit function in a script. When trap -- EXIT is ran, the -- is interpreted as an "end of arguments", indicating to trap that the signal is to be bound to null ('') (since there were no flags prior to or after --), indicating to the shell to ignore the signal (however, this doesn't work with EXIT, but works with other signals). Running trap -- 'echo something' EXIT, however, will still eval 'echo something' and exit upon exit. As per the specification of trap, the command - by itself indicates that the shell will reset any traps for the signals specified, which is why it works in both sh and Bash.

Source Link
Loading