0

I have this script:

#!/bin/bash

function_1() {
    function_2
    printf world
}

function_2() {
    printf "hello "
    return 1
    printf "not printed"
}

function_1

which prints hello world. I would like to stop the execution of the script in function_2 after the first printf. So the script should only print hello. I tried return 1 but this only returns back to function_1. I also tried to send a kill signal as proposed in earlier answer but I got the same result as with return 1.

builder-7000
  • 7,131
  • 3
  • 19
  • 43

2 Answers2

0

I believe you want the exit built-in. From the Bash manpage:

exit [n]

Cause the shell to exit with a status of n. If n is omitted, the exit status is that of the last command executed. A trap on EXIT is executed before the shell terminates.

Daniel H
  • 7,223
  • 2
  • 26
  • 41
0

you can add an exit 0 to exit the script with a 0 exit status at the desired point :

function_2() {
    printf "hello "
    exit 0
    ...
nullPointer
  • 4,419
  • 1
  • 15
  • 27
  • Thank that works. But if I put these two functions in `.bashrc` then call `function_1` from the terminal, the terminal gets closed. – builder-7000 Apr 17 '19 at 06:51