4

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!

6
  • 1
    PowerShell does not support a && construct. If you want to write a command which does this for you, check out this Commented Feb 15, 2018 at 17:20
  • 1
    (Take a look specifically at how to use $?) Commented Feb 15, 2018 at 17:22
  • Thank you. I am going to look at it. I had no idea that '&&' doesn't work in powershell. In cmd it works just fine. I am new with VSCode. They have only powershell there. It would be easier for me if it worked in PS. Commented Feb 15, 2018 at 17:27
  • Possible duplicate of Multiple statements using && Commented Feb 15, 2018 at 17:36
  • 2
    Possible duplicate of Can I get && to work in Powershell? Commented Feb 15, 2018 at 18:00

3 Answers 3

8

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!

Sign up to request clarification or add additional context in comments.

1 Comment

my usage was: git pull origin master; git pull origin dev; exit. very helpful answer. thanks @Afif.
0

You can use a semicolon [;] . Something like this:

(command); (command)

(Write-Host "this");  (Write-Host " or that")

(gcc .\code.c);  (.\a.out)

2 Comments

It's not really the same because if you use && then the right side will be executed only when left side is success.
If you really want it the same then run: cmd /k gcc code.c && ./a.out
0

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"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.