1

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.

0

2 Answers 2

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
    ...
Sign up to request clarification or add additional context in comments.

1 Comment

Thank that works. But if I put these two functions in .bashrc then call function_1 from the terminal, the terminal gets closed.
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.