How to ignore an error in a bash script?

How to ignore an error in a bash script?

In bash script to stop execution on error. I have ~100 lines of script executing and I don’t want to check return code of every line in the script. But for one particular command, I want to ignore the error.

Why does Bash not run in compatibility mode?

/bin/sh is usually symlinked to either a minimal POSIX compliant shell or to a standard shell (e.g. bash). Even in the latter case, #!/bin/sh may fail because bash wil run in compatibility mode as explained in the manpage:

When to ignore error code of failed command?

Just in case if you want your script not to stop if a particular command fails and you also want to save error code of failed command:

What happens if cmd1 or Cmd2 fails?

The pseudo-code in the question does not correspond to the title of the question. cmd1 ; cmd2: This will run cmd1, and then it will run cmd2, independent of the failure or success of running cmd1. Petr Uzel is spot on but you can also play with the magic $?.

Is there such thing as a return value in Bash?

From the perspective of your program logic, there shouldn’t really be any difference. Shell commands are connected by pipes (aka streams), and not fundamental or user-defined data types, as in “real” programming languages. There is no such thing like a return value for a command, maybe mostly because there’s no real way to declare it.

Where is the exit code in the return statement in Bash?

The return statement sets the exit code of the function, much the same as exit will do for the entire script. The exit code for the last command is always available in the $? variable.

Can a variable be considered as a command in Bash?

Since Bash and other POSIX commandline interpreters does not consider variable assignments as a command, the present command’s return code is respected. Note: assignement with the typeset or declare keyword is considered as a command, so the evaluated return code in case is the assignement itself and not the command executed in the sub-shell: