3

I have a glob pattern {,**/}*.* for recursing through all files in current directory plus children. In the terminal if I run echo {,**/}*.* it outputs all of the files in the current directory plus the nested directories. When I run a shell script which contains this line it only does one directory deep.

I understand that terminal has different behaviour than the shell: adding shopt -s extglob made no difference.

#!/bin/bash

shopt -s extglob
echo {,**/}*.*

I am on MacOSX with Bash 4 terminal and shopt -s globstar enabled.

Community
  • 1
  • 1
mummybot
  • 2,668
  • 2
  • 28
  • 31
  • But did you enable `globstar` in the script? – Ignacio Vazquez-Abrams Dec 22 '16 at 14:40
  • 1
    If I add `shopt -s globstar` it errors saying: "./show-glob.sh: line 3: shopt: globstar: invalid shell option name" – mummybot Dec 22 '16 at 14:42
  • Running `shopt globstar` in terminal outputs `globstar on` – mummybot Dec 22 '16 at 14:43
  • 1
    Are you sure you're running it with bash 4? – Ignacio Vazquez-Abrams Dec 22 '16 at 14:43
  • `$bash --version GNU bash, version 4.4.5(1)-release (x86_64-apple-darwin16.3.0) Copyright (C) 2016 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later This is free software; you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.` – mummybot Dec 22 '16 at 14:44
  • 2
    `#!/bin/bash` on OSX points to BASH 3.2 not BASH 4 – anubhava Dec 22 '16 at 14:44
  • 3
    You probably want `#!/usr/local/bin/bash` at the start of your script – anubhava Dec 22 '16 at 14:45
  • There is no inherent difference between the terminal and a script; it depends on which shell each is running. – chepner Dec 22 '16 at 14:50
  • I changed start of script to `#!/usr/local/bin/bash` t, and added `bash --version`. Shows that Bash 4 is running, still doesn't work as expected: `$./show-glob.sh GNU bash, version 4.4.5(1)-release (x86_64-apple-darwin16.3.0)` – mummybot Dec 22 '16 at 14:53
  • 1
    Do you still get the `"./show-glob.sh: line 3: shopt: globstar: invalid shell option name" ` or another error ? – Aserre Dec 22 '16 at 14:57
  • Boom, you are right. The following works: `#!/usr/local/bin/bash shopt -s globstar echo {,**/}*.*` – mummybot Dec 22 '16 at 15:00

2 Answers2

2

Thanks @Aserre and @anubhava, it was indeed the combination of bash path and making sure globstar was enabled (for MacOSX). Full script is:

#!/usr/local/bin/bash

shopt -s globstar

echo {,**/}*.*

And yes ./** would suffice but that wasn't my problem :)

mummybot
  • 2,668
  • 2
  • 28
  • 31
0

{,**/}*.* will not give you all files in current directory plus children. Instead it would give you results of the format

directory/file.ext

You might have used ./** instead

If you globstar, ** will expand to everything in the folder. ie do

shopt -s globstar
echo ./** # this would suffice

Apart from that, borrowing [ this ] comment, you need to use a shell version that supports globstar. This is evident from you getting

shopt: globstar: invalid shell option name

Then,

#!/bin/bash -> #!/usr/local/bin/bash

should do the job

Community
  • 1
  • 1
sjsam
  • 21,411
  • 5
  • 55
  • 102
  • 1
    According to the comments in the Question, OP has enabled globstar in his interactive shell and receives an error when enabling it in his script. His issue is related to his shebang. – Aserre Dec 22 '16 at 14:46
  • @Aserre : My emphasis was on the glob pattern. `for recursing through all files in current directory plus children`, `{,**/}*.*`seems overly complicated. – sjsam Dec 22 '16 at 15:00
  • 1
    Actually, I want to include the current directory, and I don't want folders, so my glob is based on the answer at http://stackoverflow.com/a/1691202/158815 – mummybot Dec 22 '16 at 15:12
  • @mummybot : I see, then what you did is the perfect way to do it. – sjsam Dec 22 '16 at 15:18