How to Run Linux Commands From Outside the Bash Shell on Windows 10

how-to-run-linux-commands-from-outside-the-bash-shell-on-windows-10 photo 1

Windows 10 includes an underlying “Windows Subsystem for Linux” to run Linux applications, but it’s only accessible via the Bash shell. However, there is a way to run Linux applications without first launching a Bash window.

This is possible thanks to the bash -c command. Use it to run Linux commands via Command Prompt and PowerShell, or even create a desktop shortcut that runs a Linux command or program when launched.

How to Run Linux Commands in the Command Prompt or PowerShell

Before you do any of this, you’ll need to install and set up Windows 10’s Bash shell. After you do, you can open a Command Prompt window and  run the following command to run Linux software outside a Bash window:

bash -c "command"

For example, let’s say you wanted to install the emacs package via apt-get. You’d run the following command in a Command Prompt window:

bash -c "sudo apt-get install emacs"

When you use bash -c, Windows will launch a Bash shell in the background and pass the command to it. The command will run until it’s done and then quit, along with the Bash shell. Any terminal output the current command runs will appear directly in the Command Prompt window, rather than a separate Bash shell window.

how-to-run-linux-commands-from-outside-the-bash-shell-on-windows-10 photo 2

This trick also allows you to run Linux commands in PowerShell, just as you’d run a normal Windows executable. You can do this by directly running the .exe file as you would in a Command Prompt window, or via any other method for running executables in PowerShell.

bash -c "uname -a"

As with a Command Prompt window, the output of the command will appear directly in PowerShell. These methods can be used to add Linux commands to a batch file or PowerShell script, too.

how-to-run-linux-commands-from-outside-the-bash-shell-on-windows-10 photo 3

How to Run Linux Commands From the Run Dialog or Start Menu

This trick also works for running commands from the Run dialog, Start menu, or any third-party application launcher.

For example, to launch vi from the Run dialog, you’d press Windows+R on your keyboard and then run the following command:

bach -c "vi"

The vi text editor will appear in its own window.

how-to-run-linux-commands-from-outside-the-bash-shell-on-windows-10 photo 4

This also works for the Start menu, so you can type the same command directly into the Start menu and press Enter to run it as a command.

how-to-run-linux-commands-from-outside-the-bash-shell-on-windows-10 photo 5

How to Run Linux Commands with a Desktop Shortcut

The bash -c command also makes it possible to launch Linux applications via a desktop shortcut. For example, let’s say you wanted to launch the vi text editor through a desktop shortcut. You’d right-click the desktop, point to New > New Shortcut, and enter the following target:

bash -c "vi"

You can now save the shortcut and it will launch vi when you double-click it.

how-to-run-linux-commands-from-outside-the-bash-shell-on-windows-10 photo 6

Interestingly enough, this trick also works for launching graphical Linux applications from a desktop shortcut. Install an X server and set up graphical Linux desktop applications. You should be able to create shortcuts to launch them directly.

However, you’d have to launch them like this:

bash -c "DISPLAY=:0 command"

While you can launch Bash commands from elsewhere in Windows, there’s no way to run Windows software or executables directly from a Bash shell window.

Article How to Run Linux Commands From Outside the Bash Shell on Windows 10 compiled by Original article here

More stories

How to Enable HomeKit on the Ecobee3 Thermostat

When you first set up your Ecobee3 smart thermostat, Apple’s HomeKit isn’t automatically enabled. However, if you have an iPhone and want to use Siri to control your thermostat–or integrate it with other HomeKit products–here’s how to enable HomeKit on the Ecobee3 thermostat.

How to Turn Off the Back and Forward Trackpad Gestures on a Mac

If you use a Mac laptop to surf, you’ve probably noticed that lightly swiping two fingers left or right on the trackpad causes your web browser to go forward and back a page. For some, this is a great. For others, it happens accidentally more often than it happens on purpose, which can be annoying.

How to Make a Shortcut Icon to Create a System Restore Point in Windows

Windows’ System Restore doesn’t get as much praise as it once did, even though it’s still an incredibly useful feature. Judging by the feedback on our own forums, it saves people from certain destruction on a nearly daily basis. The only problem is that it takes far too many steps to manually create

How to Set Automatic Quiet Times in Android with Do No Disturb

Do Not Disturb mode on Android can be handy if you’re in a meeting, at a movie, or anywhere else where your phone needs to not be a distraction for a little while, but the real value is found in Do Not Disturb’s automatic rules. Basically, you can tell Android when not to bother you—like at night