DEV Community

Cover image for Understanding cmd /c and cmd /k
Sajjad Rahman
Sajjad Rahman

Posted on

Understanding cmd /c and cmd /k

When I started learning Command Prompt, I was very confused about cmd /c and cmd /k. Even after running the commands, everything looked the same, so I thought nothing was executed. If you feel the same, this post is for you.

First, understand this important point

You are already inside a Command Prompt when you see something like:

I:\ethical-test>
Enter fullscreen mode Exit fullscreen mode

This is your parent CMD.

What actually happens when you run cmd /k dir

Command:

cmd /k dir
Enter fullscreen mode Exit fullscreen mode

What it does step by step:

  1. Your current CMD starts a new CMD (child CMD)
  2. The new CMD executes the dir command
  3. Because of /k, the new CMD stays open
  4. Since the path and prompt look the same, it appears unchanged
  5. In reality, you are now inside the child CMD

You can prove this by typing:

exit
Enter fullscreen mode Exit fullscreen mode

This will take you back to the parent CMD.

What happens when you run cmd /c echo hello

Command:

cmd /c echo hello
Enter fullscreen mode Exit fullscreen mode

Steps:

  1. A new CMD opens
  2. It runs echo hello
  3. Prints hello
  4. Immediately closes the new CMD
  5. Control returns to the parent CMD

That’s why you only see the output and nothing else.

Why this feels confusing

  • You are running CMD inside CMD
  • Windows does not show any visual difference
  • Same path, same prompt, same window
  • Execution happens very fast

So it looks like nothing changed, but it actually did.

Simple rule to remember

  • cmd → creates a new Command Prompt
  • /k → run command and keep CMD open
  • /c → run command and close CMD immediately

Final takeaway

cmd /c and cmd /k do work correctly.
The confusion happens because beginners usually run them inside an already opened CMD, so the difference is not visually obvious.

If you understand this, most CMD-related confusion disappears.

Top comments (0)