- Start-Process redirect output to $null
- 2 Answers 2
- Not the answer you’re looking for? Browse other questions tagged powershell or ask your own question.
- Linked
- Related
- Hot Network Questions
- Subscribe to RSS
- Redirecting output to $null in PowerShell, but ensuring the variable remains set
- 5 Answers 5
- Not the answer you’re looking for? Browse other questions tagged powershell or ask your own question.
- Linked
- Related
- Hot Network Questions
- Subscribe to RSS
- Is there a /dev/null on Windows?
- 7 Answers 7
- Not the answer you’re looking for? Browse other questions tagged windows or ask your own question.
- Linked
- Related
- Hot Network Questions
- Subscribe to RSS
- Redirect stdout/stderr to null vs2017 c++
- 2 Answers 2
- redirecting to /dev/null
- 6 Answers 6
- Bash I/O Redirection
Start-Process redirect output to $null
I’m starting a new process like this:
And my executable prints a lot to console. Is is possible to not show output from my exe?
I tried to add -RedirectStandardOutput $null flag but it didn’t work because RedirectStandardOutput doesn’t accept null . I also tried to add | Out-Null to the Start-Process function call — didn’t work. Is it possible to hide output of the exe that I call in Start-Process ?
2 Answers 2
Using call operator & and | Out-Null is a more popular option, but it is possible to discard standard output from Start-Process .
Apparently, NUL in Windows seems to be a virtual path in any folder. -RedirectStandardOutput requires a non-empty path, so $null parameter is not accepted, but «NUL» is (or any path that ends with \NUL ).
In this example the output is suppressed and the file is not created:
-RedirectStandardOutput «NUL» works too.
You’re invoking the executable synchronously ( -Wait ) and in the same window ( -NoNewWindow ).
You don’t need Start-Process for this kind of execution at all — simply invoke the executable directly, using & , the call operator, which allows you to:
- use standard redirection techniques to silence (or capture) output
- and to check automatic variable $LASTEXITCODE for the exit code
Not the answer you’re looking for? Browse other questions tagged powershell or ask your own question.
Linked
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.4.16.39093
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
Redirecting output to $null in PowerShell, but ensuring the variable remains set
I have some code:
This outputs a warning message which I want to redirect to $null:
The problem is that when I do this, while successfully supressing the warning message, it also has the negative side-effect of NOT populating $foo with the result of the function.
How do I redirect the warning to $null, but still keep $foo populated?
Also, how do you redirect both standard output and standard error to null? (In Linux, it’s 2>&1 .)
5 Answers 5
I’d prefer this way to redirect standard output (native PowerShell).
But this works too:
To redirect just standard error after defining $foo with result of «someFunction», do
This is effectively the same as mentioned above.
Or to redirect any standard error messages from «someFunction» and then defining $foo with the result:
To redirect both you have a few options:
This should work.
If it’s errors you want to hide you can do it like this
Warning messages should be written using the Write-Warning cmdlet, which allows the warning messages to be suppressed with the -WarningAction parameter or the $WarningPreference automatic variable. A function needs to use CmdletBinding to implement this feature.
To make it shorter at the command prompt, you can use -wa 0 :
Write-Error, Write-Verbose and Write-Debug offer similar functionality for their corresponding types of messages.
using a function:
or if you like one-liners:
Not the answer you’re looking for? Browse other questions tagged powershell or ask your own question.
Linked
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.4.16.39093
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
Is there a /dev/null on Windows?
What is the equivalent of /dev/null on Windows?
7 Answers 7
I think you want NUL , at least within a command prompt or batch files.
doesn’t create a file.
(I believe the same is true if you try to create a file programmatically, but I haven’t tried it.)
In PowerShell, you want $null :
According to this message on the GCC mailing list, you can use the file «nul» instead of /dev/null:
(Credits to Danny for this code; copy-pasted from his message.)
You can also use this special «nul» file through redirection.
NUL in Windows seems to be actually a virtual path in any folder. Just like .. , . in any filesystem.
Use any folder followed with NUL will work.
have the same effect as /dev/null on Linux.
This was tested on Windows 7, 64 bit.
Jon Skeet is correct. Here is the Nul Device Driver page in the Windows Embedded documentation (I have no idea why it’s not somewhere else. ).
Here is another:
NUL works programmatically as well. E.g. the following:
works as expected without creating a file. (MSVC++ 12.0)
If you need to perform in Microsoft Windows the equivalent of a symlink to /dev/null in Linux you would open and administrator’s cmd and type:
For files:
Or, for directories:
This will keep the file/direcotry always at 0 byte, and still return success to every write attempt.
You have to use start and $NUL for this in Windows PowerShell:
Type in this command assuming mySum is the name of your application and 5 10 are command line arguments you are sending.
The start command will start a detached process, a similar effect to & . The /B option prevents start from opening a new terminal window if the program you are running is a console application. and NUL is Windows’ equivalent of /dev/null . The 2>&1 at the end will redirect stderr to stdout, which will all go to NUL .
Not the answer you’re looking for? Browse other questions tagged windows or ask your own question.
Linked
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.4.16.39093
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
Redirect stdout/stderr to null vs2017 c++
I’m trying to upgrade a project from VS2013 to VS2017. I have everything working except for one thing — I’m encountering a crash when printf is called. The crash is as follows:
The line looks like this:
I’ve tracked the crash down to these lines:
I can fix it by replacing the lines with this:
I have three questions for those who might know the internals at play here better than me:
- This worked in VS2013 but crashes in VS2017 (with no OS change). Why is that?
- What is the correct way when targeting Windows to redirect all stdout/stderr output to nothing, such that printf produces no visible output?
- Is there a safe cross-platform way to do this? I’m not necessarily looking for something inserted into a standard, but rather something that every major desktop target (windows, linux, mac) works with.
It seems there’s no portable way to do this, so I just went with the good old platform specific code.
2 Answers 2
This is one of the places where Windows and Unix have irreconcilable differences and you just have to deal. I’d do it like this:
Putting a colon after NUL in the Windows case defends against the possibility that they might, in a future release of Windows, decide that they don’t want magic names in every single directory.
I have no idea why «/dev/null» worked in VS2013 — according to everything I know, it shouldn’t have.
- This worked in VS2013 but crashes in VS2017 (with no OS change). Why is that?
Because MS decided it should change. Nothing in C or C++ requires that either alternative should have the effect you’re after, as the interpretation of the first argument to freopen() is implementation-defined. Different implementations can define it differently.
For what it’s worth, /dev/null is a UNIXism. Windows does not ordinarily have a file by that name, and it anyway does not provide for device files on the filesystem as UNIX does. Therefore, if your code was working on Windows with MSVC++2013 then that implementation must have been providing special-case behavior for it.
- What is the correct way when targeting Windows to redirect all stdout/stderr output to nothing, such that printf produces no visible output?
The Windows device analogous to UNIX /dev/null is named nul (more often written NUL — like for regular file names, case is not significant). I’d say using that is a pretty good bet, but it’s still implementation-dependent.
- Is there a safe cross-platform way to do this? I’m not necessarily looking for something inserted into a standard, but rather something that every major desktop target (windows, linux, mac) works with.
This is fundamentally an issue of C or C++ implementation, not of the platform on which that implementation runs. Most implementations do, however, provide for file names used with fopen() and freopen() to have the same significance that they do to the shell and the rest of the operating system. VS2013 seems to be a little odd in that regard, but it does not thereby fail to conform. It follows that no, there is no single file name that has the significance you want to every C and C++ implementation. It is not even guaranteed that there is any file name with that significance.
In practice, you can probably rely on /dev/null for Mac and Linux, and on NUL for Windows. You might then consider using conditional compilation to select one of those based on the compilation environment (see How do I check OS with a preprocessor directive?).
redirecting to /dev/null
I’m reading an example bash shell script:
I’m trying to understand the use of /dev/null 2>&1 here. At first, I thought this script uses /dev/null in order to gracefully ignore errors, without causing the script to crash (kind of like try catch exception handling in programming languages). Because I don’t see how using tar to compress a directory into a tar file could possibly cause any type of errors.
6 Answers 6
No, this will not prevent the script from crashing. If any errors occur in the tar process (e.g.: permission denied, no such file or directory, . ) the script will still crash.
This is because of using > /dev/null 2>&1 will redirect all your command output (both stdout and stderr ) to /dev/null , meaning no outputs are printed to the terminal.
In the script, you use > /dev/null causing:
And then 2>&1 causing:
I’m trying to understand the use of «> /dev/null 2>&1» here.
(note that I added the redirection before /dev/null in your question.)
The above would redirect the STDOUT and STDERR to /dev/null . It works by merging the STDERR into the STDOUT . (Essentially all the output from the command would be redirected to the null device.)
. without causing the script to crash (kind of like try catch exception handling in programming languages).
It’s not quite like a try/catch or anything. It simply silences any sort of output (including error) from the command.
Because I don’t see how using tar to compress a directory into a tar file could possibly cause any type of errors.
It could result in errors for a number of reasons, including:
- Inadequate permissions on the file(s) you’re attempting to archive or on the file that you’re attempting to write to
- Lack of disk space in order to create the archive
When you run CMD > /dev/null 2>&1
STDOUT redirects to /dev/null, and then STDERR redirects to THE ADDRESS of STDOUT, which has been set to /dev/null , consequently both STDOUT and STDERR point to /dev/null
Oppositely, when you run CMD 2>&1 >/dev/null
STDERR redirects to THE ADDRESS of STDOUT (File descriptor 1 in that moment, or /proc/self/fd/1), and then STDOUT redirects to /dev/null , but STDERR keeps redirecting to fd1!! As a result the normal output from STDOUT is discarded, but the errors coming from STDERR are still being written onto the console.
To understand easily, write it out explicitly. Below is an example command that tries to remove a nonexisting file (to simulate an error)
- 1 is for stdout. Sends info logs to /dev/null
- 2 is for stderr. Sends error logs to /dev/null
Below are couple of enhancements.
Enhancement 1 : You can replace 1> with just > . This is because 1 is the default stdout and you can ignore mentioning defaults.
Enhancement 2 : You can replace the 2nd /dev/null with &1. This is because /dev/null is already by pointed to by stdout 1.
My suggestion : Stick to the first option. Write out the commands explicitly instead of using pointers. Takes little effort but much easier to understand and explain.
This redirects both STDOUT both STDIN
And is equivalent to
Bash I/O Redirection
- > filename redirects stdout to filename
- ( 2>&1 ) redirects stderr to stdout (now filename )
(file descriptor 1 is the default, so > is short for 1> )
Another common example:
redirects stderr and stdout to /dev/null . which means to nowhere. Things sent to /dev/null are not saved, cached, or remembered in any way.
They are just sent to ‘nowhere‘ and forgotten. This is a way of running programs and making sure they produce NO output and will never be seen on the command line or in a log file.
I see this type of question quite a bit . mainly because I’ve had to look it up myself since I haven’t been coding in years. Here is some handy information from the ABSG:
«Redirection simply means capturing output from a file, command, program, or script and sending it as input to another file, command, program, or script.»
ABSG: Advanced Bash Scripting Guide: The Chapter 20 link above is a link to the I/O redirection page of the open source tldp.org document called the Advanced Bash Scripting Guide by Mendel Cooper. It is listed as «An in-depth exploration of the art of shell scripting » and I absolutely agree. It is a terrific resource and has tons of answers to all sorts of crazy situations.
Other Valuable Resources: There are many valuable resources in the current/maintained section (in several handy formats like html, pdf, text, etc) on the Linux Documentation Project Guides page. Here are a few I have found useful: