Command substitution in the context of the current shell

105 Views Asked by At

I made a function that prints something in the terminal in a certain position and I made other function that returns the new position to print. However, I am not able to get the function to return the right value, it always returns the initial value.

My question is: is there any way to make the code work by passing my function as an argument?

My code below:

function print_in_specified_position() {
  local X="$1"
  local Y="$2"
  local STRING="$3"
  tput cup "$X" "$Y"
  printf "$STRING"
}

function show_script_options() {
  COLUMS=$(tput cols)
  LINES=$(tput lines)
  export PRINT_X=$(( COLUMS / 2 - 77 ))
  export PRINT_Y=$(( LINES / 2 - 11 ))

  if (( PRINT_X < 0 )); then
    PRINT_X=0
  fi
  if (( PRINT_Y < 0 )); then
    PRINT_Y=0
  fi

  function get_new_line() {
    PRINT_Y=$(( PRINT_Y + 1 ))
    echo $PRINT_Y
  }

  print_in_specified_position "$PRINT_X" "$(get_new_line)" "\n →  What do you want to do?\n"
  print_in_specified_position "$PRINT_X" "$(get_new_line)" "\n    1 - Option 1"
  print_in_specified_position "$PRINT_X" "$(get_new_line)" "\n    2 - Option 2"
  print_in_specified_position "$PRINT_X" "$(get_new_line)" "\n    "
  print_in_specified_position "$PRINT_X" "$(get_new_line)" "\n    3 - Option 3"
  print_in_specified_position "$PRINT_X" "$(get_new_line)" "\n    4 - Option 4"
  print_in_specified_position "$PRINT_X" "$(get_new_line)" "\n    "
  print_in_specified_position "$PRINT_X" "$(get_new_line)" "\n    5 - Option 5"
  print_in_specified_position "$PRINT_X" "$(get_new_line)" "\n    "
  print_in_specified_position "$PRINT_X" "$(get_new_line)" "\n    6 - Option 6"
  print_in_specified_position "$PRINT_X" "$(get_new_line)" "\n    "
  print_in_specified_position "$PRINT_X" "$(get_new_line)" "\n    q - Quit\n"
}
  • I tried to call my function individually, and it worked.

Code:

get_new_line
get_new_line
get_new_line

Output:

14
15
16
  • I tried to call my function with command substitution, and it not worked.

Code:

echo "$(get_new_line)"
echo "$(get_new_line)"
echo "$(get_new_line)"

Output:

14
14
14
  • I also tried "$get_new_line" and "${get_new_line}", but both didn't worked.
0

There are 0 best solutions below