Capture bash function's result and allow it to exit

lonix

This function should exit the calling script:

crash() {
  echo error
  exit 1
}

This works as expected:

echo before
crash
echo after         # execution never reaches here

But this does not:

echo before
x=$(crash)         # nothing is printed, and execution continues
echo after         # this is printed

How do I capture the result of a function, as well as allow it to exit?

xenoid

This is because $(crash) executes crash in a subshell, so the exit applies to the subshell and not to your script.

What is the point of capturing the output in a variable if you won't use it because the script exited anyway?

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Bash else/if capture exit status

From Dev

Capture result of bash anonymous FIFO

From Java

How to exit a function in bash

From Dev

Exit the bash function, not the terminal

From Java

Pipe output and capture exit status in Bash

From Dev

Capture the result of an IPython magic function

From Dev

Capture exit status from function in shell script

From Dev

Capture exit status from function in shell script

From Dev

Compiler does not allow to use exit() function in C

From Dev

Run command in background and capture result later (Bash)

From Dev

Connect to serial, issue a command, read result, capture it and exit

From Dev

bash shell - ssh remote script capture output and exit code?

From Dev

command to capture exit status of last background process in bash

From Dev

bash shell - ssh remote script capture output and exit code?

From Dev

bash - Capture exit code from remote ssh command

From Dev

Different result from a bash function

From Dev

Unable to trap ctrl-c to exit function exit bash script

From Dev

How to exit a bash function that expects a return

From Dev

How to capture "*" as argument to bash function and use in a comparison

From Dev

How to capture "*" as argument to bash function and use in a comparison

From Dev

How to implement a more general reduce function to allow early exit?

From Dev

In C, can a function exit() or should it allow main() to handle that?

From Dev

Function parameters are not destroyed at that function's exit

From Dev

Bash pipeline's exit status differs in script

From Dev

How do I capture a MySQL result set in a bash array?

From Dev

How to pass the result of a function as argument into a Bash function?

From Dev

Capture exit code of exit command

From Dev

how can we capture exit code of bash script while running the bash script in python

From Dev

Capture multi-line result with mysql_fetch_array() function

Related Related

  1. 1

    Bash else/if capture exit status

  2. 2

    Capture result of bash anonymous FIFO

  3. 3

    How to exit a function in bash

  4. 4

    Exit the bash function, not the terminal

  5. 5

    Pipe output and capture exit status in Bash

  6. 6

    Capture the result of an IPython magic function

  7. 7

    Capture exit status from function in shell script

  8. 8

    Capture exit status from function in shell script

  9. 9

    Compiler does not allow to use exit() function in C

  10. 10

    Run command in background and capture result later (Bash)

  11. 11

    Connect to serial, issue a command, read result, capture it and exit

  12. 12

    bash shell - ssh remote script capture output and exit code?

  13. 13

    command to capture exit status of last background process in bash

  14. 14

    bash shell - ssh remote script capture output and exit code?

  15. 15

    bash - Capture exit code from remote ssh command

  16. 16

    Different result from a bash function

  17. 17

    Unable to trap ctrl-c to exit function exit bash script

  18. 18

    How to exit a bash function that expects a return

  19. 19

    How to capture "*" as argument to bash function and use in a comparison

  20. 20

    How to capture "*" as argument to bash function and use in a comparison

  21. 21

    How to implement a more general reduce function to allow early exit?

  22. 22

    In C, can a function exit() or should it allow main() to handle that?

  23. 23

    Function parameters are not destroyed at that function's exit

  24. 24

    Bash pipeline's exit status differs in script

  25. 25

    How do I capture a MySQL result set in a bash array?

  26. 26

    How to pass the result of a function as argument into a Bash function?

  27. 27

    Capture exit code of exit command

  28. 28

    how can we capture exit code of bash script while running the bash script in python

  29. 29

    Capture multi-line result with mysql_fetch_array() function

HotTag

Archive