View mode: basic / threaded / horizontal-split · Log in · Help
February 29, 2012
passing a string with the & character as an argument
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
February 29, 2012
Re: passing a string with the & character as an argument
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
>
> 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
February 29, 2012
Re: passing a string with the & character as an argument
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
February 29, 2012
Re: passing a string with the & character as an argument
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.

--
James Miller
February 29, 2012
Re: passing a string with the & character as an argument
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
February 29, 2012
Re: passing a string with the & character as an argument
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.
> 
Me too.  I tried escaping it with the wonder-working \, but that didn't work.  This does work.  Weird stuff...  Thanks all.
Top | Discussion index | About this forum | D home