How to cache the variable used inside the Bash Completion Script for the current session

Inside my Bash Completion file, I view the final lines with an external script, which takes some time (1-2 seconds). Since these lines basically remain unchanged the rest of the current shell, I want to cache them, and when the Bash termination starts next time, it should use a cached line instead of an expensive search, so that it ends immediately when it is executed a second time.

To get an idea of ​​the completion file, here is an important part of the completion file:

getdeployablefiles()
{
  # How can i cache the result of 'pbt getdeployablefiles'
  # for the time the current shell runs? 
  echo `pbt getdeployablefiles`
}

have pbt &&
_pbt_complete()
{
  local cur goals

  COMPREPLY=()
  cur=${COMP_WORDS[COMP_CWORD]}
  goals=$(getdeployablefiles)
  COMPREPLY=( $(compgen -W "${goals}" -- $cur) )
  return 0
} &&
complete -F _pbt_complete pbt

How can I cache the getdeployablefiles output for the rest of the shell session? I need some kind of global variable here or some other trick.

Decision:

goals , . script:

getdeployablefiles()
{
  echo `pbt getdeployablefiles`
}

have pbt &&
_pbt_complete()
{
  local cur 
  if [ -z "$_pbt_complete_goals" ]; then
    _pbt_complete_goals=$(getdeployablefiles)
  fi

  _pbt_complete_goals=$(getdeployablefiles)

  COMPREPLY=()
  cur=${COMP_WORDS[COMP_CWORD]}
  COMPREPLY=( $(compgen -W "${_pbt_complete_goals}" -- $cur) )
  return 0
} &&
complete -F _pbt_complete pbt
+5
2

goals local , , _pbt_complete_goals? , , .

+7

PID , , PID. , , .

+1

All Articles