'command substitution calling a function with read inside not working

  • When I call a simple function with a read inside the code works as expected.
  • When I use command substitution and call the same function the code behave unexpectedly

CODE (TEST 1 and TEST 2)

#!/opt/homebrew/bin/bash

function readUserInput()
{
    echo -e "\n${1}\n" # This message must be displayed BEFORE the read command
    read -p "Enter a word: " selection
    echo -e "\nWord INSIDE the function: [${selection}]\n"
    lowercase=$(echo "${selection}" | tr '[:upper:]' '[:lower:]')
    case ${lowercase} in
        "house")    echo -e "\nUser selected HOUSE\n"
                    return 1
                    ;;
        "car")      echo -e "\nUser selected CAR\n"
                    return 2
                    ;;
        *)          echo -e "\nUser selected SOMETHING ELSE\n"
                    return 0
                    ;;
    esac
}

clear

# TEST 1 Calling the function...
readUserInput "KEY MESSAGE"

# TEST 2 Command substitution...
wordSelected="$(readUserInput "KEY MESSAGE")"
echo -e "Word OUTSIDE the function: [${wordSelected}]\n"

exit

OUTPUT TEST 1 # CORRECT

KEY MESSAGE
Enter a word: house
Word INSIDE the function: [house]
User selected HOUSE

OUTPUT TEST 2 # INCORRECT

Enter a word: car
Word OUTSIDE the function: [    # should be displayed last, not showing return value, spilling (close bracket missing)
KEY MESSAGE                     # should be displayed first
Word INSIDE the function: [car] # should be displayed after the read
User selected CAR]              # spilling, close bracket displayed here

CODE (TEST 3 & TEST 4)

modified this line...

echo -e "\n${1}\n" >&2 # TEST redirecting to stderr &2 instead of stdout &1


# TEST 3 Calling the function...
readUserInput "KEY MESSAGE"

# TEST 4 Command substitution...
wordSelected="$(readUserInput "KEY MESSAGE")"
echo -e "Word OUTSIDE the function: [${wordSelected}]\n"

OUTPUT TEST 3 # CORRECT

KEY MESSAGE
Enter a word: house
Word INSIDE the function: [house]
User selected HOUSE

OUTPUT TEST 4 # INCORRECT

KEY MESSAGE                         # CORRECT, however this may not be the right way to do this
Enter a word: car
Word OUTSIDE the function: [        # should be displayed last, not showing return value, spilling (close bracket missing)
Word INSIDE the function: [car]     # should be displayed after the read
User selected CAR]                  # spilling, close bracket displayed here

Any help would be greatly appreciated. The code is a simplification for ease; I do need to have the read command inside the function showing the string that I'm passing before the read prompts for a word. I made it work using global variables and avoiding the command substitution, however I'm more interested in resolving the challenge and learn how to do this properly.

I also have checked all options provided with read --help without luck.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source