Pass parameters that contain whitespaces via shell variable
<p>I've got a program that I want to call by passing parameters from a shell variable. Throughout this question, I am going to assume that it is given by</p>

<pre><code>#!/bin/sh
echo $#
</code></pre>

<p>i.e. that it prints out the number of arguments that are passed to it. Let's call it <code>count-args</code>.</p>

<p>I call my program like this:</p>

<pre><code>X="arg1 arg2"
count-args $X
</code></pre>

<p>This works quite well. But now one of my arguments has a whitespace in it and I can't find a way to escape it, e.g. the following things do not work:</p>

<pre><code>X="Hello\ World"
X="Hello\\ World"
X="'Hello World'"
</code></pre>

<p>In all of the cases, my program <code>count-args</code> prints out <code>2</code>. I want to find a way so I can pass the string <code>Hello World</code> and that it returns <code>1</code> instead. How?</p>

<p>Just for clarification: I do not want to pass all parameters as a single string, e.g.</p>

<pre><code>X="Hello World"
count-args $X
</code></pre>

<p>should print out <code>2</code>. I want a way to pass parameters that contain whitespaces.</p>