0

I simplified my bash script to the scope of the problem. I was wondering how come when I do this...

#!/bin/bash                                
                                            
read -p "Please enter your name: " name    
                                            
function test()                            
{                                          
    while true                             
    do                                     
        echo $name                         
    done                                   
}                                          
                                            
echo $(test)

the echo command doesn't loop the name in the terminal. However, if I were to remove the function and have the while loop by itself like so....

#!/bin/bash                                
                                           
read -p "Please enter your name: " name    
                                                 
while true                             
do                                     
   echo $name                         
done                                   

It would work. Or if I do this, it will also work

#!/bin/bash
                            
read -p "Please enter your name: " name    
        
function test()                            
{    
    echo $name                                                           
}                                          
                                            
echo $(test)

What's causing the echo command to not to not display the name. This only happens when the echo command is inside a while loop whilst being inside a function.

1

1 Answer 1

4

What's causing the echo command to not to not display the name

The parent shell is waiting for the subshell to exit before expanding the command subsitution to it's contents. Because the subshell never exits (as it's in an endless loop), the echo command never gets executed.

echo $(test)
     ^^    ^  - shell tries to expand command substitution
                so it runs a subshell with redirected output
                and waitpid()s on it.
       ^^^^   - subshell executes `test` and never exits
                cause its an endless loop.
                Parent shell will endlessly wait on subshell to exit.

Note that test is already a very standard shell builtin for testing expressions. Defining such function which will cause to overwrite the builtin may cause unexpected problems.

For some reading I could recommend bash guide functions.

Sign up to request clarification or add additional context in comments.

3 Comments

I'm new to bash, so what would be the best way to call the function and have it execute in the way I expected. Also I only used test for the example. My function was named something else in my actual program haha
just type the name of the function... Doing echo $(something) is a useless use of echo, it's like echo $(echo $(echo $(echo $(something)))). Just something. On a side note, results from unquoted expansions undergo word splitting expansion - check your scripts with shellcheck.net and preferably quote the expansions - research when to use double quotes in bash.
My apologies...I was reading someone elses script and saw that they were calling functions that way. So I thought that was the way you were suppose to call functions. Thank you for letting me know, I learned something new today

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.