:: Re: [DNG] [OT] bash / quote weirdne…
Inizio della pagina
Delete this message
Reply to this message
Autore: Martial Bornet (gmail)
Data:  
To: dng
Oggetto: Re: [DNG] [OT] bash / quote weirdness
Hi everyone,

to better understand how the shell interprets quotes, you should compile
and use the following small C program to test some expressions :

$ cat args.c

#include    <stdio.h>
#include    <string.h>

int main(int argc, char *argv[])
{
    int         _i;

    printf("ARGC = %d\n", argc);

    for (_i = 1; _i < argc; _i++) {
        printf("ARGV[%3d : length = %3d] = [%s]\n",
               _i, strlen(argv[_i]), argv[_i]);
    }
    return 0;
}

Now, test your expressions :

$ xcmd="unrar x"

$ args xcmd
ARGC = 2
ARGV[  1 : length =   4] = [xcmd]

$ args $xcmd
ARGC = 3
ARGV[  1 : length =   5] = [unrar]
ARGV[  2 : length =   1] = [x]

$ args "$xcmd"
ARGC = 2
ARGV[  1 : length =   7] = [unrar x]

$ args '$xcmd'
ARGC = 2
ARGV[  1 : length =   5] = [$xcmd]

$ args "cat /etc/fstab"
ARGC = 2
ARGV[  1 : length =  14] = [cat /etc/fstab]

Regards,

Martial