Benny Yu
Benny Yu

Reputation: 1

How many arguments are passed to the command by the shell:

I want to find out how many arguments are passed to the command by the shell:

echo "It's "'funny how'" it's done."  

It is 1 argument, because first ' turn off the " after s , and the ' after turn off following " , so first " matches the last ".

For

"<bar bar -b "-a" '-r' >bar bar bar"

I don't understand why 5 argument are passed to the command by shell

pig pig pig

enter image description here

6   arguments passed to command by shell

Upvotes: 0

Views: 89

Answers (1)

dash-o
dash-o

Reputation: 14452

Bash converts its input to tokens based on the sequence

  • Quoting
  • Expansion (brace expansion, tilde expansion, parameter and variable expansion, command substitution, arithmetic expansion, word splitting, and pathname expansion)

Applying the above to the: echo "It's "'funny how'" it's done.":

  1. de-quoting will result in 3 replacements ('*' indicate quoted space)
    1. double quoted It's*
    2. Single quoted funny*how
    3. Single quoted *it's*done.
  2. The word splitting looks for unquoted spaces to separate into arguments. Given no (unquoted) spaces, all the above are combined into one argument. It's*funny*how*it's*done.

Note that quoting are not nested, as implied by the question (e.g., single quote within double quotes does NOT have special meaning).

Following on <pig pig -x " " -z -r" " >pig pig pig ('*' is quoted space, '_' is unquoted space).

  1. de-quoting will result in 2 replacements ('*' indicate quoted space)

    1. Unquoted <pig_pig_-x_ 2, Double quoted *
    2. Unquoted _-z_-r
    3. Double quoted *
    4. Unquoted '_>pig_pig_pig`
  2. Word splitting will process the combined <pig_pig_-x_*_-z_-r*_>pig_pig_pig. Splitting on unquoted spaces: "pig", "pig", "pig". The 'pig' will be processed by the shell: redirect input and output. Resulting in 7 parameter.

Upvotes: 1

Related Questions