In bash, with
exec >file
all output from stdout will be redirected.
Is there an equivalence in windows cmd.exe?
== P.S. ==
Why need it?
In our application, which needed to support embedded shell(cmd.exe on windows), to achieve better text search support, we decided to decouple command execution from output display.
Under bash, we have used exec >file to achieve that, which works seamlessly. But my cmd expertise is insufficient to find an equivalent command under windows.
== EDIT ==
to clarify what exec >file do in bash
after exec >file in bash, file descriptor 1, i.e., its stdout being redirected to file. which is different from mycommand >file.
In former case, stdout of shell itself being redirected, stdout of any command running inside it has not being technically redirected, but since it's child process of shell, the stdout is inherited, thus run ls after exec >file, ls's output will write to `file.
In latter case, stdout of shell itself has unchanged, only child process's stdout being redirected (by shell, its parent process).
In summary, what I ask is how to redirect cmd.exe itself's stdout, then use cmd.exe interactively just as normal, except cmd.exe don't write anything to console, all output write to file.
Thus, below approaches is not what i pursuit
dir >file
wrap_several_command_to_batch_file >file
wrap_several_command_to_batch_file_and_redirect_inside_then_exit
There is no equivalent to the exec command.
But it can be emulated by restarting the own batch file.
This works only for all commands in the batch file itself, but ends when the batch ends.
#echo off
REM *** Trampoline jump for function calls of the form ex. "C:\:function:\..\ownBatchFile.bat"
FOR /F "tokens=3 delims=:" %%L in ("%~0") DO goto :%%L
REM *** Emulated exec
REM *** Restart this batch file with the function name inside %0
REM *** Parameters are appended, but works only for simple parameters
call "%~d0\:exec:\..\%~pnx0" %* > file
exit /b
:exec
echo This will be redirected
If you want to redirect also from the command line, there are two different possibilities.
Start cmd with a redirection.
cmd /k > file
Disturb the file handles, by redirection restore failures.
The redirection works, but can't be restored later (only by exit the cmd instance).
Read more at More fun with redirection and file handles
echo dummy >NUL 2> nul 3> file
The second example works, because when there are multiple redirections at a time, then the restoring of the file handles fails, because the order of restoring is done in the wrong order by cmd.exe.
Related
Is it possible to write a single script file which executes in both Windows (treated as .bat) and Linux (via Bash)?
I know the basic syntax of both, but didn't figure out. It could probably exploit some Bash's obscure syntax or some Windows batch processor glitch.
The command to execute may be just a single line to execute other script.
The motivation is to have just a single application boot command for both Windows and Linux.
Update: The need for system's "native" shell script is that it needs to pick the right interpreter version, conform to certain well-known environment variables etc. Installing additional environments like CygWin is not preferable - I'd like to keep the concept "download & run".
The only other language to consider for Windows is Windows Scripting Host - WSH, which is preset by default since 98.
What I have done is use cmd’s label syntax as comment marker. The label character, a colon (:), is equivalent to true in most POSIXish shells. If you immediately follow the label character by another character which can’t be used in a GOTO, then commenting your cmd script should not affect your cmd code.
The hack is to put lines of code after the character sequence “:;”. If you’re writing mostly one-liner scripts or, as may be the case, can write one line of sh for many lines of cmd, the following might be fine. Don’t forget that any use of $? must be before your next colon : because : resets $? to 0.
:; echo "Hi, I’m ${SHELL}."; exit $?
#ECHO OFF
ECHO I'm %COMSPEC%
A very contrived example of guarding $?:
:; false; ret=$?
:; [ ${ret} = 0 ] || { echo "Program failed with code ${ret}." >&2; exit 1; }
:; exit
ECHO CMD code.
Another idea for skipping over cmd code is to use heredocs so that sh treats the cmd code as an unused string and cmd interprets it. In this case, we make sure that our heredoc’s delimiter is both quoted (to stop sh from doing any sort of interpretation on its contents when running with sh) and starts with : so that cmd skips over it like any other line starting with :.
:; echo "I am ${SHELL}"
:<<"::CMDLITERAL"
ECHO I am %COMSPEC%
::CMDLITERAL
:; echo "And ${SHELL} is back!"
:; exit
ECHO And back to %COMSPEC%
Depending on your needs or coding style, interlacing cmd and sh code may or may not make sense. Using heredocs is one method to perform such interlacing. This could, however, be extended with the GOTO technique:
:<<"::CMDLITERAL"
#ECHO OFF
GOTO :CMDSCRIPT
::CMDLITERAL
echo "I can write free-form ${SHELL} now!"
if :; then
echo "This makes conditional constructs so much easier because"
echo "they can now span multiple lines."
fi
exit $?
:CMDSCRIPT
ECHO Welcome to %COMSPEC%
Universal comments, of course, can be done with the character sequence : # or :;#. The space or semicolon are necessary because sh considers # to be part of a command name if it is not the first character of an identifier. For example, you might want to write universal comments in the first lines of your file before using the GOTO method to split your code. Then you can inform your reader of why your script is written so oddly:
: # This is a special script which intermixes both sh
: # and cmd code. It is written this way because it is
: # used in system() shell-outs directly in otherwise
: # portable code. See https://stackoverflow.com/questions/17510688
: # for details.
:; echo "This is ${SHELL}"; exit
#ECHO OFF
ECHO This is %COMSPEC%
Thus, some ideas and ways to accomplish sh and cmd-compatible scripts without serious side effects as far as I know (and without having cmd output '#' is not recognized as an internal or external command, operable program or batch file.).
EDIT
The binki's answer is almost perfect but still can be improved:
:<<BATCH
#echo off
echo %PATH%
exit /b
BATCH
echo $PATH
It uses again the : trick and the multi line comment. Looks like cmd.exe (at least on windows10) works without problems with the unix style EOLs so be sure that your script is converted into linux format. (same approach has been seen used before here and here ) . Though using shebang still will produce redundant output...
you can try this:
#|| goto :batch_part
echo $PATH
#exiting the bash part
exit
:batch_part
echo %PATH%
Probably you'll need to use /r/n as a new line instead of a unix style.If I remember correct the unix new line is not interpreted as a new line by .bat scripts.Another way is to create an #.exe file in the path that does do nothing in similar manner as my answer here: Is it possible to embed and execute VBScript within a batch file without using a temporary file?
I wanted to comment, but can only add an answer at the moment.
The techniques given are excellent and I use them also.
It is hard to retain a file which has two kinds of line breaks contained within it, that being /n for the bash part and /r/n for the windows part. Most editors try and enforce a common line break scheme by guessing what kind of file you are editing. Also most methods of transferring the file across the internet (particularly as a text or script file) will launder the line breaks, so you could start with one kind of line break and end up with the other. If you made assumptions about line breaks and then gave your script to someone else to use they might find it doesn't work for them.
The other problem is network mounted file systems (or CDs) that are shared between different system types (particularly where you can't control the software available to the user).
One should therefore use the DOS line break of /r/n and also protect the bash script from the DOS /r by putting a comment at the end of each line (#). You also cannot use line continuations in bash because the /r will cause them to break.
In this way whoever uses the script, and in whatever environment, it will then work.
I use this method in conjunction with making portable Makefiles!
The following works for me without any errors or error messages with Bash 4 and Windows 10, unlike the answers above. I name the file "whatever.cmd", do chmod +x to make it executable in linux, and make it have unix line endings (dos2unix) to keep bash quiet.
:; if [ -z 0 ]; then
#echo off
goto :WINDOWS
fi
if [ -z "$2" ]; then
echo "usage: $0 <firstArg> <secondArg>"
exit 1
fi
# bash stuff
exit
:WINDOWS
if [%2]==[] (
SETLOCAL enabledelayedexpansion
set usage="usage: %0 <firstArg> <secondArg>"
#echo !usage:"=!
exit /b 1
)
:: windows stuff
You can share variables:
:;SET() { eval $1; }
SET var=value
:;echo $var
:;exit
ECHO %var%
The previous answers seem to cover pretty much all the options and helped me a lot. I'm including this answer here just to demonstrate the mechanism I used to include both a Bash script and a Windows CMD script in the same file.
LinuxWindowsScript.bat
echo >/dev/null # >nul & GOTO WINDOWS & rem ^
echo 'Processing for Linux'
# ***********************************************************
# * NOTE: If you modify this content, be sure to remove carriage returns (\r)
# * from the Linux part and leave them in together with the line feeds
# * (\n) for the Windows part. In summary:
# * New lines in Linux: \n
# * New lines in Windows: \r\n
# ***********************************************************
# Do Linux Bash commands here... for example:
StartDir="$(pwd)"
# Then, when all Linux commands are complete, end the script with 'exit'...
exit 0
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
:WINDOWS
echo "Processing for Windows"
REM Do Windows CMD commands here... for example:
SET StartDir=%cd%
REM Then, when all Windows commands are complete... the script is done.
Summary
In Linux
The first line (echo >/dev/null # >nul & GOTO WINDOWS & rem ^) will be ignored and the script will flow through each line immediately following it until the exit 0 command is executed. Once exit 0 is reached, the script execution will end, ignoring the Windows commands below it.
In Windows
The first line will execute the GOTO WINDOWS command, skipping the Linux commands immediately following it and continuing execution at the :WINDOWS line.
Removing Carriage Returns in Windows
Since I was editing this file in Windows, I had to systematically remove the carriage returns (\r) from the Linux commands or else I got abnormal results when running the Bash portion. To do this, I opened the file in Notepad++ and did the following:
Turn on the option for viewing end of line characters (View> Show Symbol > Show End of Line). Carriage returns will then show as CR characters.
Do a Find & Replace (Search > Replace...) and check the Extended (\n, \r, \t, \0, \x...) option.
Type \r in the Find what : field and blank out the Replace with : field so there's nothing in it.
Starting at the top of the file, click the Replace button until all of the carriage return (CR) characters have been removed from the top Linux portion. Be sure to leave the carriage return (CR) characters for the Windows portion.
The result should be that each Linux command ends in just a line feed (LF) and each Windows command ends in a carriage return and line feed (CR LF).
There are several ways of executing different commands on bash and cmd with the same script.
cmd will ignore lines that start with :;, as mentioned in other answers. It will also ignore the next line if the current line ends with the command rem ^, as the ^ character will escape the line break and the next line will be treated as a comment by rem.
As for making bash ignore the cmd lines, there are multiple ways. I have enumerated some ways to do that without breaking the cmd commands:
Non-existent # command (not recommended)
If there is no # command available on cmd when the script is run, we can do this:
# 2>nul & echo Hello cmd! & rem ^
echo 'Hello bash!' #
The # character at the beginning of the cmd line makes bash treat that line as a comment.
The # character at the end of the bash line is used to comment out the \r character, as Brian Tompsett pointed out in his answer. Without this, bash will throw an error if the file has \r\n line endings, required by cmd.
By doing # 2>nul, we're tricking cmd to ignore the error of some non-existent # command, while still executing the command that follows.
Don't use this solution if there is a # command available on the PATH or if you have no control over the commands available to cmd.
Using echo to ignore the # character on cmd
We can use echo with it's output redirected to insert cmd commands on bash's commented out area:
echo >/dev/null # >nul & echo Hello cmd! & rem ^
echo 'Hello bash!' #
Since the # character has no special meaning on cmd, it is treated as a part of the text to echo. All we had to do is redirect the output of the echo command and insert other commands after it.
Empty #.bat file
echo >/dev/null # 1>nul 2> #.bat
# & echo Hello cmd! & del #.bat & rem ^
echo 'Hello bash!' #
The echo >/dev/null # 1>nul 2> #.bat line creates an empty #.bat file while on cmd (or replaces existing #.bat, if any), and does nothing while on bash.
This file will be used by the cmd line(s) that follows even if there is some other # command on the PATH.
The del #.bat command on the cmd-specific code deletes the file that was created. You only have to do this on the last cmd line.
Don't use this solution if a #.bat file could be on your current working directory, as that file will be erased.
Recomended: using here-document to ignore cmd commands on bash
:; echo 'Hello bash!';<<:
echo Hello cmd! & ^
:
By placing the ^ character at the end of the cmd line we're escaping the line break, and by using : as the here-document delimiter, the delimiter line contents will have no effect on cmd. That way, cmd will only execute its line after the : line is over, having the same behaviour as bash.
If you want to have multiple lines on both platforms and only execute them at the end of the block, you can do this:
:;( #
:; echo 'Hello' #
:; echo 'bash!' #
:; );<<'here-document delimiter'
(
echo Hello
echo cmd!
) & rem ^
here-document delimiter
As long as there is no cmd line with exactly here-document delimiter, this solution should work. You can change here-document delimiter to any other text.
In all of the presented solutions, the commands will only be executed after the last line, making their behaviour consistent if they do the same thing on both platforms.
Those solutions must be saved to files with \r\n as line breaks, otherwise they won't work on cmd.
I use this technique to create runnable jar files. Since the jar/zip file starts at the zip header, I can put a universal script to run this file at the top:
#!/usr/bin/env sh\n
# 2>/dev/null # 2>nul & echo off & goto BOF\r\n
:\n
<shell commands go here with \n line endings>
exit\n
\r\n
:BOF\r\n
<cmd commands go here with \r\n line endings>\r\n
exit /B %errorlevel%\r\n
}
It is important to set the line endings as outlined above because they can cause issues on the different platforms. Also the goto statement will not work correctly in some cases if the proper line endings are missing around the jump label.
The technique above is what I use currently.
Below is an outdated version with an in-depth explaination:
#!/usr/bin/env sh
# 2>/dev/null # 2>nul & echo off
:; alias ::=''
:: exec java -jar $JAVA_OPTS "$0" "$#"
:: exit
java -jar %JAVA_OPTS% "%~dpnx0" %*
exit /B
The first line does echo off in cmd and doesn't print anything on sh. This is because the # in sh throws an error that is piped to /dev/null and after that a comment starts. On cmd the pipe to /dev/null fails because the file is not recognized on windows but since windows doesn't detect # as a comment the error is piped to nul. Then it does an echo off. Because the whole line is preceded by an # it doesn't get printet on cmd.
The second one defines ::, which starts a comment in cmd, to noop in sh. This has the benefit that :: does not reset $? to 0. It uses the ":; is a label" trick.
Now I can prepend sh commands with :: and they are ignored in cmd
On :: exit the sh script ends and I can write cmd commands
Only the first line (shebang) is problematic in cmd since it will print command not found.
You have to decide yourself if you need it or not.
I needed this for some of my Python package install scripts. Most things between sh and bat file are same but few things like error handling are different. One way to do this is as follows:
common.inc
----------
common statement1
common statement2
Then you call this from bash script:
linux.sh
--------
# do linux specific stuff
...
# call common code
source common.inc
Windows batch file looks like this:
windows.bat
-----------
REM do windows specific things
...
# call common code
call common.inc
Try my BashWin project at https://github.com/skanga/bashwin which uses BusyBox for most Unix commands
There is a platform independent build tools like Ant or Maven with xml syntax (based on Java).
So, you could rewrite all your scripts in Ant or Maven an run them despite os type.
Or you could just create Ant wrapper script, which will analyze os type and run appropriate bat or bash script.
Quick question that I couldn't find an answer to. When piping to a file as such:
echo "hello" > hello.txt
does this operation actually call notepad.exe on Windows, or is this a non application level file operation?
No, echo is a builtin command in the windows Command Prompt (cmd.exe) used to display/output messages.
The output redirector, >, followed by a file path makes cmd.exe write the message to that file instead of to your screen.
You can see the full list of builtin commands in cmd.exe by typing help at the command prompt
echo is an internal command, which means it is built in to the windows command shell cmd.exe.
Source Internal commands:
The Windows CMD shell CMD.exe contains a number of 'internal' commands.
...
ASSOC, BREAK, CALL ,CD/CHDIR, CLS, COLOR, COPY, DATE, DEL, DIR, DPATH,
ECHO, ENDLOCAL, ERASE, EXIT, FOR, FTYPE, GOTO, IF, KEYS, MD/MKDIR,
MKLINK (vista and above), MOVE, PATH, PAUSE, POPD, PROMPT, PUSHD, REM,
REN/RENAME, RD/RMDIR, SET, SETLOCAL, SHIFT, START, TIME, TITLE, TYPE,
VER, VERIFY, VOL
Piping (a form of redirection) is also performed by the windows command shell cmd.exe.
See Redirection for more information.
The means that when you execute echo "hello" > hello.txt the whole of the command (the echo followed by the redirection is performed by cmd.exe.
Is it possible to write a single script file which executes in both Windows (treated as .bat) and Linux (via Bash)?
I know the basic syntax of both, but didn't figure out. It could probably exploit some Bash's obscure syntax or some Windows batch processor glitch.
The command to execute may be just a single line to execute other script.
The motivation is to have just a single application boot command for both Windows and Linux.
Update: The need for system's "native" shell script is that it needs to pick the right interpreter version, conform to certain well-known environment variables etc. Installing additional environments like CygWin is not preferable - I'd like to keep the concept "download & run".
The only other language to consider for Windows is Windows Scripting Host - WSH, which is preset by default since 98.
What I have done is use cmd’s label syntax as comment marker. The label character, a colon (:), is equivalent to true in most POSIXish shells. If you immediately follow the label character by another character which can’t be used in a GOTO, then commenting your cmd script should not affect your cmd code.
The hack is to put lines of code after the character sequence “:;”. If you’re writing mostly one-liner scripts or, as may be the case, can write one line of sh for many lines of cmd, the following might be fine. Don’t forget that any use of $? must be before your next colon : because : resets $? to 0.
:; echo "Hi, I’m ${SHELL}."; exit $?
#ECHO OFF
ECHO I'm %COMSPEC%
A very contrived example of guarding $?:
:; false; ret=$?
:; [ ${ret} = 0 ] || { echo "Program failed with code ${ret}." >&2; exit 1; }
:; exit
ECHO CMD code.
Another idea for skipping over cmd code is to use heredocs so that sh treats the cmd code as an unused string and cmd interprets it. In this case, we make sure that our heredoc’s delimiter is both quoted (to stop sh from doing any sort of interpretation on its contents when running with sh) and starts with : so that cmd skips over it like any other line starting with :.
:; echo "I am ${SHELL}"
:<<"::CMDLITERAL"
ECHO I am %COMSPEC%
::CMDLITERAL
:; echo "And ${SHELL} is back!"
:; exit
ECHO And back to %COMSPEC%
Depending on your needs or coding style, interlacing cmd and sh code may or may not make sense. Using heredocs is one method to perform such interlacing. This could, however, be extended with the GOTO technique:
:<<"::CMDLITERAL"
#ECHO OFF
GOTO :CMDSCRIPT
::CMDLITERAL
echo "I can write free-form ${SHELL} now!"
if :; then
echo "This makes conditional constructs so much easier because"
echo "they can now span multiple lines."
fi
exit $?
:CMDSCRIPT
ECHO Welcome to %COMSPEC%
Universal comments, of course, can be done with the character sequence : # or :;#. The space or semicolon are necessary because sh considers # to be part of a command name if it is not the first character of an identifier. For example, you might want to write universal comments in the first lines of your file before using the GOTO method to split your code. Then you can inform your reader of why your script is written so oddly:
: # This is a special script which intermixes both sh
: # and cmd code. It is written this way because it is
: # used in system() shell-outs directly in otherwise
: # portable code. See https://stackoverflow.com/questions/17510688
: # for details.
:; echo "This is ${SHELL}"; exit
#ECHO OFF
ECHO This is %COMSPEC%
Thus, some ideas and ways to accomplish sh and cmd-compatible scripts without serious side effects as far as I know (and without having cmd output '#' is not recognized as an internal or external command, operable program or batch file.).
EDIT
The binki's answer is almost perfect but still can be improved:
:<<BATCH
#echo off
echo %PATH%
exit /b
BATCH
echo $PATH
It uses again the : trick and the multi line comment. Looks like cmd.exe (at least on windows10) works without problems with the unix style EOLs so be sure that your script is converted into linux format. (same approach has been seen used before here and here ) . Though using shebang still will produce redundant output...
you can try this:
#|| goto :batch_part
echo $PATH
#exiting the bash part
exit
:batch_part
echo %PATH%
Probably you'll need to use /r/n as a new line instead of a unix style.If I remember correct the unix new line is not interpreted as a new line by .bat scripts.Another way is to create an #.exe file in the path that does do nothing in similar manner as my answer here: Is it possible to embed and execute VBScript within a batch file without using a temporary file?
I wanted to comment, but can only add an answer at the moment.
The techniques given are excellent and I use them also.
It is hard to retain a file which has two kinds of line breaks contained within it, that being /n for the bash part and /r/n for the windows part. Most editors try and enforce a common line break scheme by guessing what kind of file you are editing. Also most methods of transferring the file across the internet (particularly as a text or script file) will launder the line breaks, so you could start with one kind of line break and end up with the other. If you made assumptions about line breaks and then gave your script to someone else to use they might find it doesn't work for them.
The other problem is network mounted file systems (or CDs) that are shared between different system types (particularly where you can't control the software available to the user).
One should therefore use the DOS line break of /r/n and also protect the bash script from the DOS /r by putting a comment at the end of each line (#). You also cannot use line continuations in bash because the /r will cause them to break.
In this way whoever uses the script, and in whatever environment, it will then work.
I use this method in conjunction with making portable Makefiles!
The following works for me without any errors or error messages with Bash 4 and Windows 10, unlike the answers above. I name the file "whatever.cmd", do chmod +x to make it executable in linux, and make it have unix line endings (dos2unix) to keep bash quiet.
:; if [ -z 0 ]; then
#echo off
goto :WINDOWS
fi
if [ -z "$2" ]; then
echo "usage: $0 <firstArg> <secondArg>"
exit 1
fi
# bash stuff
exit
:WINDOWS
if [%2]==[] (
SETLOCAL enabledelayedexpansion
set usage="usage: %0 <firstArg> <secondArg>"
#echo !usage:"=!
exit /b 1
)
:: windows stuff
You can share variables:
:;SET() { eval $1; }
SET var=value
:;echo $var
:;exit
ECHO %var%
The previous answers seem to cover pretty much all the options and helped me a lot. I'm including this answer here just to demonstrate the mechanism I used to include both a Bash script and a Windows CMD script in the same file.
LinuxWindowsScript.bat
echo >/dev/null # >nul & GOTO WINDOWS & rem ^
echo 'Processing for Linux'
# ***********************************************************
# * NOTE: If you modify this content, be sure to remove carriage returns (\r)
# * from the Linux part and leave them in together with the line feeds
# * (\n) for the Windows part. In summary:
# * New lines in Linux: \n
# * New lines in Windows: \r\n
# ***********************************************************
# Do Linux Bash commands here... for example:
StartDir="$(pwd)"
# Then, when all Linux commands are complete, end the script with 'exit'...
exit 0
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
:WINDOWS
echo "Processing for Windows"
REM Do Windows CMD commands here... for example:
SET StartDir=%cd%
REM Then, when all Windows commands are complete... the script is done.
Summary
In Linux
The first line (echo >/dev/null # >nul & GOTO WINDOWS & rem ^) will be ignored and the script will flow through each line immediately following it until the exit 0 command is executed. Once exit 0 is reached, the script execution will end, ignoring the Windows commands below it.
In Windows
The first line will execute the GOTO WINDOWS command, skipping the Linux commands immediately following it and continuing execution at the :WINDOWS line.
Removing Carriage Returns in Windows
Since I was editing this file in Windows, I had to systematically remove the carriage returns (\r) from the Linux commands or else I got abnormal results when running the Bash portion. To do this, I opened the file in Notepad++ and did the following:
Turn on the option for viewing end of line characters (View> Show Symbol > Show End of Line). Carriage returns will then show as CR characters.
Do a Find & Replace (Search > Replace...) and check the Extended (\n, \r, \t, \0, \x...) option.
Type \r in the Find what : field and blank out the Replace with : field so there's nothing in it.
Starting at the top of the file, click the Replace button until all of the carriage return (CR) characters have been removed from the top Linux portion. Be sure to leave the carriage return (CR) characters for the Windows portion.
The result should be that each Linux command ends in just a line feed (LF) and each Windows command ends in a carriage return and line feed (CR LF).
There are several ways of executing different commands on bash and cmd with the same script.
cmd will ignore lines that start with :;, as mentioned in other answers. It will also ignore the next line if the current line ends with the command rem ^, as the ^ character will escape the line break and the next line will be treated as a comment by rem.
As for making bash ignore the cmd lines, there are multiple ways. I have enumerated some ways to do that without breaking the cmd commands:
Non-existent # command (not recommended)
If there is no # command available on cmd when the script is run, we can do this:
# 2>nul & echo Hello cmd! & rem ^
echo 'Hello bash!' #
The # character at the beginning of the cmd line makes bash treat that line as a comment.
The # character at the end of the bash line is used to comment out the \r character, as Brian Tompsett pointed out in his answer. Without this, bash will throw an error if the file has \r\n line endings, required by cmd.
By doing # 2>nul, we're tricking cmd to ignore the error of some non-existent # command, while still executing the command that follows.
Don't use this solution if there is a # command available on the PATH or if you have no control over the commands available to cmd.
Using echo to ignore the # character on cmd
We can use echo with it's output redirected to insert cmd commands on bash's commented out area:
echo >/dev/null # >nul & echo Hello cmd! & rem ^
echo 'Hello bash!' #
Since the # character has no special meaning on cmd, it is treated as a part of the text to echo. All we had to do is redirect the output of the echo command and insert other commands after it.
Empty #.bat file
echo >/dev/null # 1>nul 2> #.bat
# & echo Hello cmd! & del #.bat & rem ^
echo 'Hello bash!' #
The echo >/dev/null # 1>nul 2> #.bat line creates an empty #.bat file while on cmd (or replaces existing #.bat, if any), and does nothing while on bash.
This file will be used by the cmd line(s) that follows even if there is some other # command on the PATH.
The del #.bat command on the cmd-specific code deletes the file that was created. You only have to do this on the last cmd line.
Don't use this solution if a #.bat file could be on your current working directory, as that file will be erased.
Recomended: using here-document to ignore cmd commands on bash
:; echo 'Hello bash!';<<:
echo Hello cmd! & ^
:
By placing the ^ character at the end of the cmd line we're escaping the line break, and by using : as the here-document delimiter, the delimiter line contents will have no effect on cmd. That way, cmd will only execute its line after the : line is over, having the same behaviour as bash.
If you want to have multiple lines on both platforms and only execute them at the end of the block, you can do this:
:;( #
:; echo 'Hello' #
:; echo 'bash!' #
:; );<<'here-document delimiter'
(
echo Hello
echo cmd!
) & rem ^
here-document delimiter
As long as there is no cmd line with exactly here-document delimiter, this solution should work. You can change here-document delimiter to any other text.
In all of the presented solutions, the commands will only be executed after the last line, making their behaviour consistent if they do the same thing on both platforms.
Those solutions must be saved to files with \r\n as line breaks, otherwise they won't work on cmd.
I use this technique to create runnable jar files. Since the jar/zip file starts at the zip header, I can put a universal script to run this file at the top:
#!/usr/bin/env sh\n
# 2>/dev/null # 2>nul & echo off & goto BOF\r\n
:\n
<shell commands go here with \n line endings>
exit\n
\r\n
:BOF\r\n
<cmd commands go here with \r\n line endings>\r\n
exit /B %errorlevel%\r\n
}
It is important to set the line endings as outlined above because they can cause issues on the different platforms. Also the goto statement will not work correctly in some cases if the proper line endings are missing around the jump label.
The technique above is what I use currently.
Below is an outdated version with an in-depth explaination:
#!/usr/bin/env sh
# 2>/dev/null # 2>nul & echo off
:; alias ::=''
:: exec java -jar $JAVA_OPTS "$0" "$#"
:: exit
java -jar %JAVA_OPTS% "%~dpnx0" %*
exit /B
The first line does echo off in cmd and doesn't print anything on sh. This is because the # in sh throws an error that is piped to /dev/null and after that a comment starts. On cmd the pipe to /dev/null fails because the file is not recognized on windows but since windows doesn't detect # as a comment the error is piped to nul. Then it does an echo off. Because the whole line is preceded by an # it doesn't get printet on cmd.
The second one defines ::, which starts a comment in cmd, to noop in sh. This has the benefit that :: does not reset $? to 0. It uses the ":; is a label" trick.
Now I can prepend sh commands with :: and they are ignored in cmd
On :: exit the sh script ends and I can write cmd commands
Only the first line (shebang) is problematic in cmd since it will print command not found.
You have to decide yourself if you need it or not.
I needed this for some of my Python package install scripts. Most things between sh and bat file are same but few things like error handling are different. One way to do this is as follows:
common.inc
----------
common statement1
common statement2
Then you call this from bash script:
linux.sh
--------
# do linux specific stuff
...
# call common code
source common.inc
Windows batch file looks like this:
windows.bat
-----------
REM do windows specific things
...
# call common code
call common.inc
Try my BashWin project at https://github.com/skanga/bashwin which uses BusyBox for most Unix commands
There is a platform independent build tools like Ant or Maven with xml syntax (based on Java).
So, you could rewrite all your scripts in Ant or Maven an run them despite os type.
Or you could just create Ant wrapper script, which will analyze os type and run appropriate bat or bash script.
I saw # is used in such contexts:
#echo off
#echo start eclipse.exe
What does # mean here?
It means not to output the respective command. Compare the following two batch files:
#echo foo
and
echo foo
The former has only foo as output while the latter prints
H:\Stuff>echo foo
foo
(here, at least). As can be seen the command that is run is visible, too.
echo off will turn this off for the complete batch file. However, the echo off call itself would still be visible. Which is why you see #echo off in the beginning of batch files. Turn off command echoing and don't echo the command turning it off.
Removing that line (or commenting it out) is often a helpful debugging tool in more complex batch files as you can see what is run prior to an error message.
It means "don't echo the command to standard output".
Rather strangely,
echo off
will send echo off to the output! So,
#echo off
sets this automatic echo behaviour off - and stops it for all future commands, too.
Source: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/batch.mspx?mfr=true
By default, a batch file will display its command as it runs. The purpose of this first command which #echo off is to turn off this display. The command "echo off" turns off the display for the whole script, except for the "echo off" command itself. The "at" sign "#" in front makes the command apply to itself as well.
The # disables echo for that one command. Without it, the echo start eclipse.exe line would print both the intended start eclipse.exe and the echo start eclipse.exe line.
The echo off turns off the by-default command echoing.
So #echo off silently turns off command echoing, and only output the batch author intended to be written is actually written.
It inherits the meaning from DOS.
From the '#' section of Technical Notes > Programming > Batch File Commands (archived version):
#: In DOS version 3.3 and later, hides the echo of a batch command. Any output generated by the command is echoed.
The at-sign can be prefixed to any DOS command, program name, or batch file name within a batch file.
Without it, you could turn off command echoing using the echo off command, but that command would be echoed first.
In batch file:
1 #echo off(solo)=>output nothing
2 echo off(solo)=> the “echo off” shows in the command line
3 echo off(then echo something) =>
4 #echo off(then echo something)=>
See, echo off(solo), means no output in the command line, but itself shows;
#echo off(solo), means no output in the command line, neither itself;
Another useful time to include # is when you use FOR in the command line. For example:
FOR %F IN (*.*) DO ECHO %F
Previous line show for every file: the command prompt, the ECHO command, and the result of ECHO command. This way:
FOR %F IN (*.*) DO #ECHO %F
Just the result of ECHO command is shown.
you can include # in a 'scriptBlock' like this:
#(
echo don't echoed
hostname
)
echo echoed
and especially do not do that :)
for %%a in ("#") do %%~aecho %%~a
Was wondering how you'd do the following in Windows:
From a c shell script (extension csh), I'm running a Python script within an 'eval' method so that the output from the script affects the shell environment. Looks like this:
eval `python -c "import sys; run_my_code_here(); "`
Was wondering how I would do something like the eval statement in Windows using Windows' built in CMD shell. I want to run a Python script within a Windows script and have the script run what the Python script prints out.
** update: specified interest in running from CMD shell.
If it's in cmd.exe, using a temporary file is the only option [that I know of]:
python -c "print(\"Hi\")" > temp.cmd
call temp.cmd
del temp.cmd
(Making some guesses where details are missing from your question)
In CMD, when a batch script modifies the environment, the default behavior is that it modifies the environment of the CMD process that is executing it.
Now, if you have a batch script that calls another batch script, there are 3 ways to do it.
execute the batch file directly:
REM call q.bat
q.bat
REM this line never runs
Usually you don't want this, because it won't return to the calling batch script. This is more like goto than gosub. The CMD process just switches from one script to another.
execute with call:
REM call q.bat
CALL q.bat
REM changes that q.bat affects will appear here.
This is the most common way for one batch file to call another. When q.bat exits, control will return to the caller. Since this is the same CMD process, changes to the environment will still be there.
Note: If q.bat uses the EXIT statement, it can cause the CMD process to terminate, without returning control to the calling script.
Note 2: If q.bat uses EXIT /B, then the CMD process will not exit. This is useful for setting ERRORLEVEL.
Execute in a new CMD process:
REM call q.bat
CMD /C q.bat
REM environment changes in q.bat don't affect me
Since q.bat run ins a new CMD process, it affects the environment of that process, and not the CMD that the caller is running in.
Note: If q.bat uses EXIT, it won't terminate the process of the caller.
The SETLOCAL CMD command will create a new environment for the current script. Changes in that environment won't affect the caller. In general, SETLOCAL is a good practice, to avoid leaking environment changes by accident.
To use SETLOCAL and still push environment changes to the calling script, end the script with:
ENDLOCAL && SET X=%X% && SET Y=%Y%
This will push the values of X and Y to the parent environment.
If on the other hand you want to run another process (not a CMD script) and have it affect the current script's environment, than have the tool generate a batch file that makes the changes you want, then execute that batch file.
REM q.exe will write %TEMP%\runme.cmd, which looks like:
REM set X=Y
q.exe
call "%TEMP%\runme.cmd"