zebediah49
zebediah49

Reputation: 7611

Bash Array Feature or Bug?

While attempting to debug a job submission script, I ended up narrowing down the bug to this:

    [testuser@bes ~]$ var=( 1 foo1*bar4 echo 1*4=4 )
    [testuser@bes ~]$ echo "${var[@]}"
    1 foo1*bar4 echo 1*4=4
    [testuser@bes ~]$ cd /data/testuser/jobs/example/a16162/
    [testuser@bes a16162]$ var=( 1 foo1*bar4 echo 1*4=4 )
    [testuser@bes a16162]$ echo "${var[@]}"
    1 foo1-bar4 foo1*bar4 echo 1*4=4
    [testuser@bes a16162]$

That is an uncut transcript of a fresh bash session. Anyone have any idea how that one works? Is this some archaic feature of bash that I've never heard of before, or just a really weird bug?

Versions (yes I know it's out dated):

GNU bash, version 3.2.25(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2005 Free Software Foundation, Inc.

Linux bes 2.6.18-194.11.3.el5 #1 SMP Mon Aug 30 16:19:16 EDT 2010 x86_64 x86_64 x86_64 GNU/Linux

EDIT: This is for something that needs to process a user-passed array, and I'd rather use this method than a triplet of rather awkward awk hacks. They're trivial "extract element 2" sorts of things, which is why using the array seems nicer.

Upvotes: 0

Views: 129

Answers (2)

glenn jackman
glenn jackman

Reputation: 247200

What does ls /data/testuser/jobs/example/a16162/foo1* reveal?

You can disable filename globbing with set -f and re-enable it with set +f

Upvotes: 1

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799420

Globs are still globbed when the array is formed. If you don't want this then you need to quote or escape them.

$ var=( 1 "foo1*bar4" echo "1*4=4" )

Upvotes: 2

Related Questions