2

I'm trying to read shell script which I haven't worked with before... What does this code do?

# Setup some command defaults (can be overriden by the config)
MYSQL=${MYSQL:-`which mysql`}
MYSQLDUMP=${MYSQLDUMP:-`which mysqldump`}
PHP=${PHP:-`which php`}

I have a feeling it determines the location of php, mysql and mysqldump if the variable is not already defined. Is that correct?

jørgensen
  • 10,149
  • 2
  • 20
  • 27
Ben
  • 60,438
  • 111
  • 314
  • 488

1 Answers1

2

If variable is undefined or is the empty string, it replaces it with the result of the which command so that it serves as a default value.

Side note, you could make it more robust by using type -P mysql or hash mysql instead of which mysql since implementations of which may differ.

see http://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html and Check if a program exists from a Bash script

Community
  • 1
  • 1
Gregory Pakosz
  • 69,011
  • 20
  • 139
  • 164
  • 1
    The first sentence should begin "If the variable is undefined or is the empty string" since the operator is ':-' rather than '-'. – William Pursell Feb 25 '12 at 17:34