title: How do I use shell script to check if a bucket exists?
body: <p>I have <code>aws</code> cli installed. I'm just not sure how to do this in shell script. </p>
  
  <p>when I run command <code>aws s3 ls s3://bucket</code> it would give me something like this </p>
  
  <blockquote>
    <p>A client error (NoSuchBucket) occurred when calling the ListObjects operation: The specified bucket does not exist</p>
  </blockquote>
  
  <p>That means the bucket doesn't exist. So I want to run that from shell script and check if <code>grep</code> finds it. But my command doesn't work.</p>
  
  <pre><code>if [ $(aws s3 ls "s3://$S3_BUCKET" | grep 'NoSuchBucket' &amp;&gt; /dev/null) == 0 ] 
  then
      echo "$S3_BUCKET doesn\'t exist please check again"
      exit
  fi
  </code></pre>
  
  <p>It just gave me this</p>
  
  <blockquote>
    <p>backup.sh: 20: [: 0: unexpected operator</p>
  </blockquote>
  
  <p>Updated</p>
  
  <p>I changed the script to be</p>
  
  <pre><code>echo "S3_BUCKET=$S3_BUCKET"
  if aws s3 ls "s3://$S3_BUCKET" | grep -q 'AllAccessDisabled'    
  then
      echo "$S3_BUCKET doesn\'t exist please check again"
      exit
  fi
  </code></pre>
  
  <p>And this is the output I got</p>
  
  <pre><code>A client error (AllAccessDisabled) occurred when calling the ListObjects operation: All access to this object has been disabled
  </code></pre>
  
  <p>So the text contains <code>AllAccessDisabled</code> but I still don't the <code>echo</code> the next line.</p>