www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - passing a string with the & character as an argument

reply jic <cabrera wrc.xerox.com> writes:
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
Feb 28 2012
next sibling parent reply James Miller <james aatch.net> writes:
On 29 February 2012 18:51, jic <cabrera wrc.xerox.com> wrote:
 Greetings!

 I have this program,

 import std.process : system;
 import std.stdio;
 int main(char[][] args)
 {
 =C2=A0char[] cmd;

 =C2=A0for (int i=3D1;i<args.length;i++)
 =C2=A0{
 =C2=A0 =C2=A0cmd ~=3D args[i] ~ " ";
 =C2=A0}
 =C2=A0writefln(cmd);
 =C2=A0return(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 &. =C2=A0How can I pass a & in a string? =C2=A0I tried escapin=

 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
Feb 28 2012
parent reply Jos van Uden <user domain.invalid> writes:
On 29-2-2012 7:06, James Miller wrote:
 On 29 February 2012 18:51, jic<cabrera 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


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

On windows the ampersand also has a special meaning. In that case try the carrot ^ to escape test 1! 2 3^& 4# Jos
Feb 28 2012
parent jic <cabrera wrc.xerox.com> writes:
James Miller Wrote:

 On 29 February 2012 20:21, Jos van Uden <user domain.invalid> wrote:
 On 29-2-2012 7:06, James Miller wrote:
 On 29 February 2012 18:51, jic<cabrera 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


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

On windows the ampersand also has a special meaning. In that case try the carrot ^ to escape test 1! 2 3^&  4# Jos

Today I Learned that windows has insane escaping.

Feb 29 2012
prev sibling next sibling parent James Miller <james aatch.net> writes:
On 29 February 2012 20:21, Jos van Uden <user domain.invalid> wrote:
 On 29-2-2012 7:06, James Miller wrote:
 On 29 February 2012 18:51, jic<cabrera wrc.xerox.com> =C2=A0wrote:
 Greetings!

 I have this program,

 import std.process : system;
 import std.stdio;
 int main(char[][] args)
 {
 =C2=A0char[] cmd;

 =C2=A0for (int i=3D1;i<args.length;i++)
 =C2=A0{
 =C2=A0 =C2=A0cmd ~=3D args[i] ~ " ";
 =C2=A0}
 =C2=A0writefln(cmd);
 =C2=A0return(1);
 }

 if I compile it and run it this way,

 test 1! 2  3& =C2=A04#

 the result is


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

On windows the ampersand also has a special meaning. In that case try the carrot ^ to escape test 1! 2 3^& =C2=A04# Jos

Today I Learned that windows has insane escaping. -- James Miller
Feb 28 2012
prev sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 2/29/12, James Miller <james aatch.net> wrote:
 Today I Learned that windows has insane escaping.

You won't have to worry about it for long: https://github.com/D-Programming-Language/phobos/pull/457
Feb 29 2012