Skip to main content
alot -> a lot
Source Link
Jeff Schaller
  • 68.8k
  • 35
  • 122
  • 266

Cjm's answer is correct, but $? can be used in silly ways in shell scripts, and I'd like to warn against that. A lot of bad shell scripts have a repeated pattern of code:

run_some_command EXIT_STATUS=$? if [ "$EXIT_STATUS" -eq "0" ] then # Do work when command exists on success else # Do work for when command has a failure exit fi 

If at all possible (readability concerns sometimes intrude) you should code this situation differently:

if run_some_command then # Do work when command exists on success else # Do failure exit work fi 

This latter usage is faster, does not contaminate the shell's variable namespace with what amounts to temp variables, can often be alota lot more readable for humans and encourages the use of "positive logic", the practice of writing conditionals without negations, which has cognitive simplicity in most situations. It does away with the use of $? for the most part.

Cjm's answer is correct, but $? can be used in silly ways in shell scripts, and I'd like to warn against that. A lot of bad shell scripts have a repeated pattern of code:

run_some_command EXIT_STATUS=$? if [ "$EXIT_STATUS" -eq "0" ] then # Do work when command exists on success else # Do work for when command has a failure exit fi 

If at all possible (readability concerns sometimes intrude) you should code this situation differently:

if run_some_command then # Do work when command exists on success else # Do failure exit work fi 

This latter usage is faster, does not contaminate the shell's variable namespace with what amounts to temp variables, can often be alot more readable for humans and encourages the use of "positive logic", the practice of writing conditionals without negations, which has cognitive simplicity in most situations. It does away with the use of $? for the most part.

Cjm's answer is correct, but $? can be used in silly ways in shell scripts, and I'd like to warn against that. A lot of bad shell scripts have a repeated pattern of code:

run_some_command EXIT_STATUS=$? if [ "$EXIT_STATUS" -eq "0" ] then # Do work when command exists on success else # Do work for when command has a failure exit fi 

If at all possible (readability concerns sometimes intrude) you should code this situation differently:

if run_some_command then # Do work when command exists on success else # Do failure exit work fi 

This latter usage is faster, does not contaminate the shell's variable namespace with what amounts to temp variables, can often be a lot more readable for humans and encourages the use of "positive logic", the practice of writing conditionals without negations, which has cognitive simplicity in most situations. It does away with the use of $? for the most part.

replaced http://unix.stackexchange.com/ with https://unix.stackexchange.com/
Source Link

Cjm's answerCjm's answer is correct, but $? can be used in silly ways in shell scripts, and I'd like to warn against that. A lot of bad shell scripts have a repeated pattern of code:

run_some_command EXIT_STATUS=$? if [ "$EXIT_STATUS" -eq "0" ] then # Do work when command exists on success else # Do work for when command has a failure exit fi 

If at all possible (readability concerns sometimes intrude) you should code this situation differently:

if run_some_command then # Do work when command exists on success else # Do failure exit work fi 

This latter usage is faster, does not contaminate the shell's variable namespace with what amounts to temp variables, can often be alot more readable for humans and encourages the use of "positive logic", the practice of writing conditionals without negations, which has cognitive simplicity in most situations. It does away with the use of $? for the most part.

Cjm's answer is correct, but $? can be used in silly ways in shell scripts, and I'd like to warn against that. A lot of bad shell scripts have a repeated pattern of code:

run_some_command EXIT_STATUS=$? if [ "$EXIT_STATUS" -eq "0" ] then # Do work when command exists on success else # Do work for when command has a failure exit fi 

If at all possible (readability concerns sometimes intrude) you should code this situation differently:

if run_some_command then # Do work when command exists on success else # Do failure exit work fi 

This latter usage is faster, does not contaminate the shell's variable namespace with what amounts to temp variables, can often be alot more readable for humans and encourages the use of "positive logic", the practice of writing conditionals without negations, which has cognitive simplicity in most situations. It does away with the use of $? for the most part.

Cjm's answer is correct, but $? can be used in silly ways in shell scripts, and I'd like to warn against that. A lot of bad shell scripts have a repeated pattern of code:

run_some_command EXIT_STATUS=$? if [ "$EXIT_STATUS" -eq "0" ] then # Do work when command exists on success else # Do work for when command has a failure exit fi 

If at all possible (readability concerns sometimes intrude) you should code this situation differently:

if run_some_command then # Do work when command exists on success else # Do failure exit work fi 

This latter usage is faster, does not contaminate the shell's variable namespace with what amounts to temp variables, can often be alot more readable for humans and encourages the use of "positive logic", the practice of writing conditionals without negations, which has cognitive simplicity in most situations. It does away with the use of $? for the most part.

added 113 characters in body
Source Link
Caleb
  • 72k
  • 19
  • 203
  • 233

Cjm's answerCjm's answer is correct, but "$?"$? can be used in silly ways in shell scripts, and I'd like to warn against that. A lot of bad shell scripts have a repeated pattern of code:

run_some_command EXIT_STATUS=$? if [ "$EXIT_STATUS" -eq "0" ] then # Do work when command exists on success else # Do work for when command has a failure exit fi 

If at all possible (readability concerns sometimes intrude) you should code this situation differently:

if run_some_command then # Do work when command exists on success else # Do failure exit work fi 

This latter usage is faster, does not contaminate the shell's variable namespace with what amounts to temp variables, can often be alot more readable for humans and encourages the use of "positive logic", the practice of writing conditionals without negations, which has cognitive simplicity in most situations. It does away with the use of "$?"$? for the most part.

Cjm's answer is correct, but "$?" can be used in silly ways in shell scripts, and I'd like to warn against that. A lot of bad shell scripts have a repeated pattern of code:

run_some_command EXIT_STATUS=$? if [ "$EXIT_STATUS" -eq "0" ] then # Do work when command exists on success else # Do work for when command has a failure exit fi 

If at all possible (readability concerns sometimes intrude) you should code this situation differently:

if run_some_command then # Do work when command exists on success else # Do failure exit work fi 

This latter usage is faster, does not contaminate the shell's variable namespace with what amounts to temp variables, can often be alot more readable for humans and encourages the use of "positive logic", the practice of writing conditionals without negations, which has cognitive simplicity in most situations. It does away with the use of "$?" for the most part.

Cjm's answer is correct, but $? can be used in silly ways in shell scripts, and I'd like to warn against that. A lot of bad shell scripts have a repeated pattern of code:

run_some_command EXIT_STATUS=$? if [ "$EXIT_STATUS" -eq "0" ] then # Do work when command exists on success else # Do work for when command has a failure exit fi 

If at all possible (readability concerns sometimes intrude) you should code this situation differently:

if run_some_command then # Do work when command exists on success else # Do failure exit work fi 

This latter usage is faster, does not contaminate the shell's variable namespace with what amounts to temp variables, can often be alot more readable for humans and encourages the use of "positive logic", the practice of writing conditionals without negations, which has cognitive simplicity in most situations. It does away with the use of $? for the most part.

Source Link
user732
user732
Loading