Find the answer to your Linux question:
Results 1 to 4 of 4
Hi everyone! I try to get an option in bash and send it to a c program, like this: # progName -a "getPacket !MY_IP -> any" but when I run ...
  1. #1
    Bah
    Bah is offline
    Just Joined!
    Join Date
    Jul 2007
    Posts
    2

    Cool strange behavior of "!" symbol

    Hi everyone!
    I try to get an option in bash and send it to a c program,
    like this:
    # progName -a "getPacket !MY_IP -> any"
    but when I run it, the "!" symbol replace with some strings
    that I don't know where are they come from! and this error occurs:
    -bash: event not found

    then I check this format:
    # progName -a "getPacket \!MY_IP -> any"
    but I see that "\" pass to my program, too!

    I try to do it with double quote option ("!"MY_IP), but it's not
    successfully.

    Please tell me how can I pass the "!" symbol to my prog?!
    (you can run these commands in bash and see one of strange behavior of "!":
    pwd !ls
    !pwd ls
    It seems that "!" points to a temp memory.)

    Thank you in advanced.

  2. #2
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    If you want the output of this command as a parameter for your program:

    Code:
    getPacket !MY_IP -> any
    try something like:

    Code:
    progName -a `getPacket !MY_IP -> any`
    or:

    Code:
    progName -a $(getPacket !MY_IP -> any)
    Regards

  3. #3
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    Use single quotes:
    Code:
    alex@danu ~ $ echo !hi
    bash: !hi: event not found
    alex@danu ~ $ echo "!hi"
    bash: !hi: event not found
    alex@danu ~ $ echo '!hi'
    !hi
    What '!' does is it looks for the last command you executed starting with whatever follows '!'. For instance:
    Code:
    alex@danu ~ $ echo "Hello there"
    Hello there
    alex@danu ~ $ !ec
    echo "Hello there"
    Hello there
    You can also use '!!' to repeat the last command:
    Code:
    alex@danu ~ $ echo "Hello there"
    Hello there
    alex@danu ~ $ !!
    echo "Hello there"
    Hello there
    I personally use '!' a lot when dealing with mounting:
    Code:
    mount /mnt/usb
    # do stuff, not using commands that start with 'mo'
    u!mo
    That last command will end up as 'umount /mnt/usb'. A much faster way to do that sort of stuff.

    '!' has some other cool powers. For instance, '!$' is the last argument of the last command:
    Code:
    alex@danu ~ $ vi testfile1 testfile2
    2 files to edit
    alex@danu ~ $ echo !$ 
    echo testfile2
    testfile2
    There's a lot of cool stuff that it does.

    Sorry for digressing .
    DISTRO=Arch
    Registered Linux User #388732

  4. #4
    Bah
    Bah is offline
    Just Joined!
    Join Date
    Jul 2007
    Posts
    2
    OK! it works correctly! Thanks a lot!

Posting Permissions

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