0

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
Benny Yu
  • 1
  • 2
  • Possible duplicate of [What are the special dollar sign shell variables?](https://stackoverflow.com/q/5163144/608639), [What do $? $0 $1 $2 mean in shell script?](https://stackoverflow.com/q/29258603/608639) and friends. – jww Oct 26 '19 at 00:27
  • Also see [Markdown help](https://stackoverflow.com/editing-help) in the [Help Center](https://stackoverflow.com/help). – jww Oct 26 '19 at 00:32
  • 1
    You say "I don't understand why 5 argument are passed to the command", then you show "6 arguments passed to command by shell". Why do you say 5 but show 6? – that other guy Oct 26 '19 at 00:45
  • not the dfference between `set -- "It's "'funny how'" it's done."; echo $# ; set -- "It's " 'funny how' " it's done." ; echo $#`. ($# is the number of arguments visible to the shell, `set --` puts whatever follows as the arguments to the current shell process, so you can also do `echo $3` for instance. Good luck. – shellter Oct 26 '19 at 02:19
  • **DO NOT post images of code, data, error messages, etc.** - copy or type the text into the question. https://stackoverflow.com/help/how-to-ask – Rob Oct 26 '19 at 10:46

1 Answers1

1

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.

oguz ismail
  • 1
  • 16
  • 47
  • 69
dash-o
  • 13,723
  • 1
  • 10
  • 37