passing a string with the & character as an argument

James Miller james at aatch.net
Tue Feb 28 22:06:37 PST 2012


On 29 February 2012 18:51, jic <cabrera at wrc.xerox.com> wrote:
>
> Greetings!
>
> I have this program,
>
> import std.process : system;
> import std.stdio;
> int main(char[][] args)
> {
>  char[] cmd;
>
>  for (int i=1;i<args.length;i++)
>  {
>    cmd ~= args[i] ~ " ";
>  }
>  writefln(cmd);
>  return(1);
> }
>
> if I compile it and run it this way,
>
> test 1! 2@ 3& 4#
>
> the result is
>
> 1! 2@ 3
>
> So, if I pass a string with an &, the argument array stops right
> before the &.  How can I pass a & in a string?  I tried escaping it,
> but it does not work either.
>
> thanks for the help.
>
> jic

This is more a shell problem than a D one. Assuming that you are using
a *nix shell (so csh, tcsh, bash or zsh) you need escape the & with a
backslash, like so: \&. You should be getting an error on your shell,
saying that it cannot find the command 4#.

Its because '&' is a special character used to fork a process into the
background, useful for gui programs and the like.

I have tried your code, using a *nix shell, and using 3\& works.

If you are on Windows, then I don't know why this is happening.

--
James Miller


More information about the Digitalmars-d-learn mailing list