I am trying to set a variable before calling a command in bash (on Mac):
BRANCH=test echo "$BRANCH"
But I get an empty echo.
printenv also has no other variable with the same name:
$ printenv | grep BRANCH
$
What am I doing wrong?
This is correct way:
BRANCH='test' bash -c 'echo "$BRANCH"'
test
To execute echo command you'll need bash -c to execute it after assignment.
BRANCH='test' bash -c 'echo "$BRANCH"' and BRANCH='test' echo "$BRANCH"?( BRANCH='test' && echo "$BRANCH" ) I guess in first case assignment isn't processed unless bash -c forks a new sub-process.$BRANCH is expanded before echo starts, before echo could look in its environment. Second, echo ignores its environment anyway. There's no need or ability to use an environment variable here, so just use echo test.