Syntax error near unexpected token '(' in bash script when selecting files

Running this bash ./cleanup.bash script

 #!/bin/bash ## going to dir moving stuff rm -rf !(composer.json|.git) 

Gives an error:

cleanup.bash: line 10: syntax error near unexpected token '('
cleanup.bash: line 10: 'rm -rf! (composer.json | .git) '

But if I run into the terminal directly, no problem rm -rf !(composer.json|.git)

I tried to delete all other lines, still getting an error.

How to correctly enter it in a bash script?

+5
source share
1 answer

I assume your problem is that the extended shell extension option is not set when running from a script. When you claim that it works on the command line, you somehow set the extglob flag, which allows !() Globs.

Since a bash script whenever it starts with #!/bin/bash starts a new sub-shell, the advanced options set in the parent shell may not appear in the new shell. To make it effect, set it to script after it-bang

 #!/bin/bash shopt -s extglob ## going to dir moving stuff rm -rf !(composer.json|.git) 
+5
source

Source: https://habr.com/ru/post/1273392/


All Articles