7

Let's imagine that on my server, i have the following files :

foo@bar:/var/log/foo$ ls
fooFile1 fooFile2 logFile logFile.1 logFile.2

I wanted to create an archive containing every file but logFile.*. So I came up with the command :

foo@bar:~$ tar -czf foo.tar.gz /var/log/foo/!(logFile.*)

And it works fine ! However, when trying to create a script which does the same job :

#!/bin/bash

tar -czf foo.tar.gz /var/log/foo/!(logFile.*)

I came across the following error :

./test.sh: line 3: syntax error near unexpected token '('
./test.sh: line 3: 'tar -czf foo.tar.gz /var/log/foo/!(logFile.*)'

The problem is not tar related (I met the same issue using ls instead of tar)

I don't understand where this error is coming from. Could anyone tell me where i am wrong ?

Met on Ubuntu 12.04

1 Answer 1

7

Place this line just below shebang:

shopt -s extglob

to enable extended glob option in your script.

!(logFile.*) is extended glob pattern that requires enabling extglob

Sign up to request clarification or add additional context in comments.

2 Comments

It is worth pointing out explicitly that this option is enabled by default in an interactive shell, which is the cause of the difference in behaviour.
I will need to look more into it. Thanks to both of you !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.