Find the answer to your Linux question:
Results 1 to 3 of 3
I have been learning linux for about a year now, and I thought I knew my way around the bash shell fairly well by now, until I deleted a whole ...
  1. #1
    Just Joined!
    Join Date
    Jan 2010
    Posts
    1

    The use of parentheses in the bash shell

    I have been learning linux for about a year now, and I thought I knew my way around the bash shell fairly well by now, until I deleted a whole folder worth of files on accident. Here's how it happened: I have a homework folder to which I had downloaded a good deal of files, and also had saved a bunch of my homework there. Over the course of the semester, I had inadvertently downloaded some files twice, and firefox placed a "(2)" at the end of duplicate files. I wanted to delete all of the duplicates, so I tried the following:
    Code:
     # rm *(2)*
    It did not work as expected, it deleted everything. I now know that i should have used:
    Code:
    # rm *"(2)"*
    or
    Code:
    # rm *\(2\)*
    but it's too late for that. I have since been trying to figure out exactly what the parentheses do. I am baffled because I am getting some pretty unexpected results as I play around with them. for example:
    Code:
    # echo *(2)
    *()
    # echo *(2)*
    emacs-23.1.tar.bz2 final_report_merged.pdf HW%202(2).doc HW%202.doc HW%203.doc site-lisp verilog-mode.el.gz
    Can someone help me understand what is happening? What do the parentheses do? Any links to sites that explain in detail would be appreciated as well.

    Thanks in advance!

  2. #2
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    This has to do with extended globbing, an extension of the usual meta-character set:
    Code:
    * ? [ ... ]
    Please look at the section of man bash that describes shopt and extglob:
    Code:
           If the extglob shell option is enabled using the shopt builtin, several
           extended pattern matching operators are recognized ...
    
    -- excerpt from man bash, searching for extglob
    a few experiments along with that reference should explain things ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  3. #3
    Just Joined! regexorcist's Avatar
    Join Date
    Jan 2010
    Location
    ~/
    Posts
    12
    That's cool drl, I didn't know that... thanks

    Parentheses are primarily used to define functions

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...