0

In my Makefile I have something like this

BREW?=/opt/homebrew/bin/brew

What would the equivalent of this be in a standard shell command ? What the above does is checks if the environment variable BREW has been defined. If it is then it uses that value otherwise it uses the assigned value.

How would I translate the above in a standard shell statement I am doing this

  env | grep BREW > /dev/null
  if [ $? -eq 0 ];then
        export BREW=$BREW
  else
        export BREW=/opt/homebrew/bin/brew
  fi
  echo "The path of brew is : $BREW"

It does not print the last statement BREW

James Franco
  • 4,516
  • 10
  • 38
  • 80
  • 2
    set: `: ${BREW:=/opt/homebrew/bin/brew} ;` – jhnc May 02 '23 at 02:30
  • 1
    use without changing permanently: `... ${BREW:-/opt/homebrew/bin/brew} ...` – jhnc May 02 '23 at 02:33
  • 1
    leave out the `:` to change if unset but leave as-is if set to null – jhnc May 02 '23 at 02:35
  • 3
    Does this answer your question? [Assigning default values to shell variables with a single command in bash](https://stackoverflow.com/questions/2013547/assigning-default-values-to-shell-variables-with-a-single-command-in-bash) – Shuai May 02 '23 at 02:38
  • @jhncif If I do this `${BREW:=/opt/homebrew/bin/brew}` it actually executes the command. It does assign the value but I don't want it to execute the command. I Just want it in a variable. – James Franco May 02 '23 at 02:42
  • @JamesFranco : you need the leading `;` char as included in jhnc's first sample. The colon is a null command, but it allows other actions, which would set your brew cmd. try `: ${BREW:=/someplace} ; echo $BREW` . good luck. – shellter May 02 '23 at 03:40
  • 1
    Don't invent your own variations: You have to type the command exactly like jhnc suggests it in his comment: By using a lone colon `:`, which is the no-op command in a POSIX shell. – user1934428 May 02 '23 at 06:25

0 Answers0