pahan
pahan

Reputation: 2453

Brace Expansion not working bash

I am trying to use brace expansion in a bash script as follows.

#!/bin/bash
document_root="/var/www/www.example.com"
`chmod -R g+w $document_root/{../captcha,../files}`

this gives me the error

chmod: cannot access `/var/www/www.example.com/{../captcha,../files}': No such file or directory

but when I run this in a terminal it works just fine.

Upvotes: 2

Views: 2310

Answers (3)

Thomas Urban
Thomas Urban

Reputation: 5061

Stumbled over this issue myself. In my case I was using set -eu command at some point of my script resulting in this particular issue. Fix was to add -B option like set -euB re-enabling brace expansion for it has been disabled for some reason though manual states it is enabled by default.

Upvotes: 1

Maurício Sousa
Maurício Sousa

Reputation: 36

Have you tried this way?

#!/bin/bash
document_root="/var/www/www.example.com"
chmod -R g+w $document_root/{"../captcha","../files"}

Upvotes: 1

SiegeX
SiegeX

Reputation: 140227

#!/bin/bash
document_root="/var/www/www.example.com"
chmod -R g+w $document_root/{../captcha,../files}
  1. Don't prefix a variable with $ when you are assigning to a variable, only when expanding
  2. You don't need the backticks around chmod, doing so treats the whole thing as a command

Upvotes: 4

Related Questions