linuxbashshellbrace-expansion

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.


Solution

  • #!/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