1

The following single command line in a Windows Desktop shortcut Target field will ping an address and timestamp it.

C:\Windows\System32\bash.exe -c "ping 1.0.0.1 | while read line; do echo `date` - $line; done"

Example output:

Fri Nov 27 14:08:05 CST 2020 - PING 1.0.0.1 (1.0.0.1) 56(84) bytes of data.
Fri Nov 27 14:08:05 CST 2020 - 64 bytes from 1.0.0.1: icmp_seq=1 ttl=58 time=26.7 ms
Fri Nov 27 14:08:06 CST 2020 - 64 bytes from 1.0.0.1: icmp_seq=2 ttl=58 time=35.5 ms
Fri Nov 27 14:08:07 CST 2020 - 64 bytes from 1.0.0.1: icmp_seq=3 ttl=58 time=42.0 ms

I read here that wsl.exe should be used instead of bash but I cannot figure out how to create the equivalent command.

(To do any of this in Windows 10 requires that you enable "Windows Subsystem for Linux" in Windows 10 and install Ubuntu from the Microsoft Store per this HTG article.)

2 Answers 2

0

The answer you linked is wrong : bash.exe has not been deprecated, and in your case it is even mandatory.

The reason is that, while WSL has a command parameter, it only allows a single command, so it will interpret the following command line as a single invocation that won't work:

enter image description here

Trying to use built-in shell commands will also not work:

enter image description here

In short, you are forced to use the bash command for your example. If you have more than one Linux distribution installed in WSL, bash will invoke the shell of the distribution that is defined as the default.

0

I read here that wsl.exe should be used instead of bash but I cannot figure out how to create the equivalent command.

Just replace bash.exe -c with wsl.exe --exec bash -c : 1

C:\Windows\System32\wsl.exe --exec bash -c
 "ping 1.0.0.1 | while read line; do echo `date` - $line; done"

This works fine in cmd.exe. I recommend trying it! 2

Output example :

date - PING 1.0.0.1 (1.0.0.1) 56(84) bytes of data.
date - 64 bytes from 1.0.0.1: icmp_seq=1 ttl=56 time=4.58 ms
date - 64 bytes from 1.0.0.1: icmp_seq=2 ttl=56 time=4.31 ms
^C

I learned this myself just a few days ago by reading this post.

You are right that bash.exe has been deprecated (as have wslconfig.exe and lxrun.exe).

References


1 In wsl.exe --exec bash -c, the bash is the Linux command and not the Windows bash.exe command.
This is (indirectly) clear if you try replacing bash with bash.exe – and experience that then there is no output.

2 To open the command line (as administrator) – hit WinKey+r, type cmd, hold down Ctrl+Shift and press Enter.
(Then copy-paste the command with Ctrl+c, Ctrl+v.)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .