'How can I use logical AND (&&) in Powershell?
In terminal I used to run two commands at once using '&&' operator.
For example if I want to compile and run C source code I only need to write:
gcc code.c && ./a.out
.
But unfortunately it doesn't work in Powershell. How can I do this? And I'm sorry I couldn't find any easier method to do this. Therefore, I had to post it here!
TIA!
Solution 1:[1]
After searching here and there I found out '&&' doesn't work in PS. There is '-and' command in lieu of '&&'. But in my case I actually wanted to execute two command at once and it didn't work. I found a way to do this. A simple semicolon could do the work. For example: If I want to compile and run a C++ code, I just need to write g++ /directory/code.cpp; ./a.exe
.
But if someone uses this they should be aware of that the second command will work even though the first one doesn't execute due to any error unlike the '&&' operator!
Solution 2:[2]
You can use a semicolon [;] . Something like this:
(command); (command)
(Write-Host "this"); (Write-Host " or that")
(gcc .\code.c); (.\a.out)
Solution 3:[3]
Check here if you really need the abortive nature of the &&
and not just "two commands, one line".
$ErrorActionPreference="Stop"
# Line break
fakeCommand; echo "Here"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Afif Al Mamun |
Solution 2 | |
Solution 3 | Chris |