Friday, 6 February 2026

subsehll - Example-1

 [root@oel01db Shell-Scripting]# cat test_sh.sh

#!/bin/bash

echo "1. This line 1."

# NO parentheses here!

if ! return 2>/dev/null; then

    echo "2. This line 2."

fi

echo "3. his line 3."

[root@oel01db Shell-Scripting]#

[root@oel01db Shell-Scripting]# chmod 777 test_sh.sh

[root@oel01db Shell-Scripting]#

Execute the script.

[root@oel01db Shell-Scripting]# ./test_sh.sh

1. This line 1.

2. This line 2.

3. his line 3.

[root@oel01db Shell-Scripting]# 


Here when script run as standalone script , return is invalid and retrun fails and ! return becomes true and 2nd and 3rd echo will be executed .

Now lets source it. 

[root@oel01db Shell-Scripting]# source test_sh.sh

1. This line 1.

[root@oel01db Shell-Scripting]#


Here the script is sourced, it runs inside the current shell and return is valid and it means “Stop executing this sourced script immediately and return control to the caller.”

You might think , “I’m just checking return’s status in if. I’m not actually returning.” and as return is valid , ! return becomes false and it should just ignore the echo 2 but echo 3 should be succeeded .

But in bash, 

There is no such thing as “checking return without executing it.”

The moment Bash parses and evaluates this:

if ! return 2>/dev/null; then

👉 return is executed immediately.


What happens in your sourced script

if ! return 2>/dev/null; then

Step-by-step:

1️⃣ Bash executes return

  • Since the script is sourced → valid

  • return immediately exits the sourced file

2️⃣ Control jumps back to the caller shell

⛔ Bash never gets a chance to:

  • negate it with !

  • enter then

  • reach echo "2"

  • reach echo "3"

let's put the if statement in subshell and see the difference.


[root@oel01db Shell-Scripting]# cat ./test_sh.sh
#!/bin/bash

echo "1. This line 1."

# NO parentheses here!
if ! (return 2>/dev/null); then
    echo "2. This line 2."
fi

echo "3. his line 3."
[root@oel01db Shell-Scripting]#
[root@oel01db Shell-Scripting]# ./test_sh.sh
1. This line 1.
2. This line 2.
3. his line 3.
[root@oel01db Shell-Scripting]# source ./test_sh.sh
1. This line 1.
3. his line 3.
[root@oel01db Shell-Scripting]#


Let's look at why Line 3 printed when you ran source ./test_sh.sh:

The Logic Breakdown (Sourcing)

  1. echo "1. This line 1.": Executes normally.

  2. if ! (return 2>/dev/null);:

    • The shell creates a subshell (the parentheses).

    • Inside that subshell, it runs return.

    • Because you are sourcing, return is valid. The subshell exits with code 0 (Success).

    • The ! operator flips that 0 to False.

    • Since the condition is False, the shell skips Line 2.

    • Because of the parentheses, the "return" command only killed the subshell, not the main script execution.

  3. echo "3. his line 3.": Since the main script is still alive, it continues to the next line and prints it.


No comments:

Post a Comment

JFrog Artifactory - How to install

JFrog Artifactory OSS Installation Guide CentOS 9 + PostgreSQL 17 This guide provides a structured workflow to install JFrog Artifactory OSS...