简体   繁体   中英

How to pass a command as a command line argument by Batch file

I want to pass a command as a command line argument from one batch file to another.

eg :

first.bat:

call test.bat "echo hello world" "echo welcome "

test.bat:

set initialcommand=%1

set maincommand=%2

%maincommand%

%initialcommand%

Here's what you need:

first.cmd:

@echo off
set maincommand=echo hello world!
call test.cmd %maincommand%

test.cmd:

@echo off
%*

In this case first.cmd passes the actual command (your example just passed the constant string "maincommand" rather than its value).

In addition, test.cmd executes a command made up of every parameter, not just the first.

When you create those two files and execute first.cmd , you get:

hello world!

as expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM