-1

This is the code I'm using to check the current git branch,

branch=$(git branch | sed -n -e 's/^\* \(.*\)/\1/p')

I want to run the following code conditionally if the branch is master

cd build && aws s3 cp . s3://www.examle.com/ --recursive

How do I do this in bash?

2
  • As in most languages you would use an if statement. Look them up in your favorite bash language reference Commented Mar 14, 2019 at 21:30
  • BTW, I'd maybe make it (cd build && exec aws s3 ...) -- adding the exec balances out the performance impact of the subshell created by the parens (by consuming it), while scoping the cd to only change the directory where that one aws s3 command is run. Commented Mar 14, 2019 at 21:33

2 Answers 2

3
branch=$(git branch | sed -n -e 's/^\* \(.*\)/\1/p')
if [ "$branch" = master ]; then
   cd build && aws s3 cp . s3://www.examle.com/ --recursive
fi
Sign up to request clarification or add additional context in comments.

Comments

3

Try like below

[[ "$branch" == master ]] && cd build && aws s3 cp . s3://www.examle.com/ --recursive

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.