Score:0

How to source and call a function within another function?

mp flag

To my use case, I'm trying to accomplish calling on a function inside another function sourced from a different script. As you can see below, script1.sh is only called upon and wouldn't work if not called on function_main. Instead, I'm trying to call the functions within it from another script and I believe I'm missing something.

script1.sh:

function_main() {

echo "universe is "

  function_2nd() {

  echo "huge"

  }

  function_3rd() {

  echo "small"

  }

}

script2.sh:

read -p "What do you think about the universe? [B/S]" answer </dev/tty

case $answer in
b|B) source ~/script1.sh; function_main; function_2nd ;;
s|S) source ~/script1.sh; function_main; function_3rd ;;
*) ;;
esac

I'm grateful for any help!

tm flag
What's the problem? The scripts work. If you remove `function_main` and declare the other functions in the top level, they will still work.
bac0n avatar
cn flag
this sounds like a [XY problem](https://xyproblem.info/)
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.