How to? Passing arguments with spaces

Mar 23, 2018
4
0
#1
In CMD.EXE the spaces are used as separators for arguments. And thus the following BAT-script will print only those characters that appear between the first and the second space in the Windows Command Prompt.

echo %1

In some shells (like, Cygwin-bash) I can use single quotes to pass an argument with spaces as a single argument. Of cause I have to rewrite the script.

echo $1

But I can run it like this.

my_script.bat 'one two'

And it will print "one two", not just "one".

Ii it possible in TCC/LE to do the same?
 

Charles Dye

Super Moderator
Staff member
May 20, 2008
3,611
46
Albuquerque, NM
prospero.unm.edu
#2
There are at least three ways to do this:
  • Using regular ASCII quotes: my_script "foo bar" The script will receive "foo bar", with the quotes, as a single argument. This is often the best way, as arguments with spaces generally need quotes anyway.
  • Using grave accents ("backquotes"): my_script `foo bar` The script will receive foo bar as a single argument; the grave accents will be stripped off.
  • You can also escape the space: my_script foo^ bar The script will see foo bar as a single argument; the escape will be lost.
 
Mar 23, 2018
4
0
#3
Charles Dye, I need the grave accents to be stripped off. The script in my original message was just a sample. But in fact I need something like this.

perl -e 'print "hi"'

It works in Cygwin-bash, but when I rewrote it like this.

perl -e `print "hi"`

It does not work in TCC/LE. I get the following error "Can't open hi: No such file or directory.". What am I doing wrong? Maybe that's because TCC/LE does some additional parsing. I mean it does not pass the characters between the grave accents as a single argument to perl.exe. But how do I suppress this parsing and make TCC/LE to pass it literally (as is).

Please, note that I am not using Cygwin-perl for this. The perl.exe (Active Perl or Strawberry) is in the PATH variable and when I try the Perl-scrpts in files they do work.
 
Last edited:

Charles Dye

Super Moderator
Staff member
May 20, 2008
3,611
46
Albuquerque, NM
prospero.unm.edu
#7
Can't find string terminator "'" anywhere before EOF at -e line 1.
Okay; I've downloaded ActiveState perl:
Code:
C:\>which perl
perl is an external : C:\Perl\bin\perl.exe

C:\>perl --version

This is perl 5, version 22, subversion 4 (v5.22.4) built for MSWin32-x86-multi-thread-64int
(with 1 registered patch, see perl -V for more detail)

Copyright 1987-2017, Larry Wall

Binary build 2205 [403863] provided by ActiveState http://www.ActiveState.com
Built Aug 24 2017 23:14:56

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl".  If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.


C:\>
I see the same behavior that you do, in both TCC/LE and CMD.EXE. No surprise there. perl does its own arg parsing; the shell has nothing to do with it. A little googling brought me to this page. I tried a few of the suggested solutions; this works for me:

Code:
perl -e "print 'hi'"
Again, this works in both TCC/LE and CMD.EXE. Which I guess makes sense; double quotes are significant to the shell, whether TCC/LE or CMD.EXE, but single quotes generally aren't.

As for backquotes, they are useful for grouping args to batch files, but not for programs called from the command line -- such as perl.exe. TCC/LE strips them off before calling the external, so perl never sees them.
 
Mar 23, 2018
4
0
#9
Charles Dye, thank you for quick and very useful answers. Also I apologize for not reading "perlwin32.html" before asking the question.

Reversing the quotation marks is a good workaround for this use-case. But I hope that in the future "TCC/LE" will have some INI-file option to switch parsing behavior to bash-mode. I mean two options: one for the quoting rules and another for wildcard expansion of command-line arguments.

Besides, I have not tried reversing the quotation marks before asking the question because I just assumed that it will not work. I came to this conclusion because I made some experiments with "echo" and noticed that the double quotes are not striped off by "CMD.EXE" and "TCC/LE".

Well, anyway, thank you for your time and good luck.