I do the following:
$ ./input ""
Output:
argc 2
argv[1][0] 0
But if I want to pass (several) empty quotes in program based manner:
$ python -c 'print "\"\""'
""
./input $(python -c 'print "\"\""')
gives:
argc 2
argv[1][0] 22 - (22 hex value for ")
So how can I generate something like:
$ ./input "" "" "" ""
and get result same as in example 1 ?
Best Answer
in
Because,
$(cmd)
is unquoted, that's a split+glob operator. The shell retrieves the output ofcmd
, removes all the trailing newline characters, then splits that based on the value of the$IFS
special parameter, and then performs filename generation (for instance turns*.txt
into the list of non-hidden txt files in the current directory) on the resulting words (that latter part not withzsh
) and in the case ofksh
also performs brace expansion (turnsa{b,c}
intoab
andac
for instance).The default value of
$IFS
contains the SPC, TAB and NL characters (also NUL inzsh
, other shells either remove the NULs or choke on them). Those (not NUL) also happen to be IFS-whitespace characters¹, which are treated specially when it comes to IFS-splitting.If the output of
cmd
is" a b\nc \n"
, that split+glob operator will generate a"a"
,"b"
and"c"
arguments to./input
. With IFS-white-space characters, it's impossible forsplit+glob
to generate an empty argument because sequences of one or more IFS-whitespace characters are treated as one delimiter. To generate an empty argument, you'd need to choose a separator that is not an IFS-whitespace character. Actually, any non-whitespace character will do (best to also avoid multi-byte characters which are not supported by all shells here).So for instance if you do:
And if
cmd
outputsa::b\n
, then that split+glob operator will result in"a"
,""
and"b"
arguments (note that the"
s are not part of the value, I'm just using them here to help show the values).With
a:b:\n
, depending on the shell, that will result in"a"
and"b"
or"a"
,"b"
and""
. You can make it consistent across all shells with(which also means that for an empty output of
cmd
(or an output consisting only of newline characters),./input
will receive one empty argument as opposed to no argument at all).Example:
gives:
Also note that when you do:
Those
"
are part of the shell syntax, they are shell quoting operators. Those"
characters are not passed toinput
.¹ IFS whitespace characters, per POSIX being the characters classified as
[:space:]
in the locale and that happen to be in$IFS
though in ksh88 (on which the POSIX specification is based) and in most shells, that's still limited to SPC, TAB and NL. The only POSIX compliant shell in that regard I found wasyash
.ksh93
andbash
(since 5.0) also include other whitespace (such as CR, FF, VT...), but limited to the single-byte ones (beware on some systems like Solaris, that includes the non-breaking-space which is single byte in some locales)