Skip to main content
edited title
Link
Steve
  • 171
  • 7

Why do bash variable assignments affect the last exit code $? and where is this documented?

added 4 characters in body
Source Link
Steve
  • 171
  • 7

I was surprised to find out that setting an environment variable modified the last exist code variable $?:

$ false
$ echo $?
1
$ false
$ FOO=BAR
$ echo $?
0
$ 

The bash manual says only this about $?:

($?) Expands to the exit status of the most recently executed foreground pipeline.

Where is the exit code of a variable being set documented?Where is the exit code of a variable being set documented? Can it fail?

I was surprised to find out that setting an environment variable modified the last exist code variable $?:

$ false
$ echo $?
1
$ false
$ FOO=BAR
$ echo $?
0
$ 

The bash manual says only this about $?:

($?) Expands to the exit status of the most recently executed foreground pipeline.

Where is the exit code of a variable being set documented? Can it fail?

I was surprised to find out that setting an environment variable modified the last exist code variable $?:

$ false
$ echo $?
1
$ false
$ FOO=BAR
$ echo $?
0
$ 

The bash manual says only this about $?:

($?) Expands to the exit status of the most recently executed foreground pipeline.

Where is the exit code of a variable being set documented? Can it fail?

Source Link
Steve
  • 171
  • 7

Why do bash variable assignments affect the last exit code $?

I was surprised to find out that setting an environment variable modified the last exist code variable $?:

$ false
$ echo $?
1
$ false
$ FOO=BAR
$ echo $?
0
$ 

The bash manual says only this about $?:

($?) Expands to the exit status of the most recently executed foreground pipeline.

Where is the exit code of a variable being set documented? Can it fail?