Emulate Bash Command in Windows

My special interest is computers. Let's talk geek here.
Post Reply
User avatar
Kellemora
Guardian Angel
Guardian Angel
Posts: 7494
Joined: 16 Feb 2015, 17:54

Emulate Bash Command in Windows

Post by Kellemora »

Hi Yogi

Remember the little Bash command I use for day and time?

Code: Select all

date +%A_%I:%M_%p -d "today +12 hours" 
It does not work on Windows computers.
Since I don't use a Windows computer, nor do I know Bash, much less whatever Windows uses, I was thinking if you were familiar enough with Windows, perhaps you might know what to use on a Windows computer to get the same results.

If not, no biggie, it is only for an on-line acquaintance, a Windows die hard user who won't use anything else.
User avatar
yogi
Posts: 9978
Joined: 14 Feb 2015, 21:49

Re: Emulate Bash Command in Windows

Post by yogi »

The Windows equivalent to Bash is PowerShell. Unlike Bash, PowerShell is not based on the Bourne shell created for Unix. This difference is out of necessity in that Unix/Linux have a different approach to operating systems than Windows. Thus the underlying shells must perform similar system tasks but in different ways. Fortunately, the date function is similar enough in both shells so that you may be able to translate some of what you already know from experiences with Bash into PowerShell. Whether you can do this or not depends largely on what exactly you want to do.

I don't have much use for Bash and even less need for PowerShell, but I have tinkered with both. Bash is much easier for me because of my background in Unix administration. The greatest impediment for me to use PowerShell is the lack of support documentation. Given that PowerShell is a whole other animal, I seldom know what I'm looking for. The good news is that Microsoft does have a TechNet which is manned by highly paid engineers, as opposed to the hit and miss forum style of support prevalent in the Linux world. It may not be any easier finding information in TechNet than it is in the Ubuntu support forum, but once you find what you are looking for the information is authoritative and not somebody's best guess.

If you want to experiment, or test your mettle, it's easy enough to get into the Windows PowerShell environment.
  • GOTO the START menu and in the search box TYPE: powershell
This will be the first test, to see if PowerShell is installed. The answer to that depends largely on what version of Windows you are using. PowerShell was a standard install from Vista going forward, but it often had to be specified during installation for it to be there. Win 7, Win 8, and now Win 10 all seem to have a version of PowerShell pre-installed. I understand that PS can also be run from Win XP. They are up to version 5 these days, but apparently they are all backward compatible. Regardless, the "date" function hasn't changed much between versions.

If PowerShell is installed you will see it in the list of search results. CLICK on PowerShell to open the command window.
If PowerShell is not installed, you will have to do it manually. It's not a big deal but it's not like apt-get install ... either.

Some Examples ...
  • Image
The top image is what you might see by simply typing in the 'date' command. There is a whole list of formatting options on this page. By the time you get to the bottom of the page it's starting to look a lot like Linux man pages, but with better examples and a sense of humor (imho). The other two images I'm showing just point out how to set up the command line to get different date formats.

I hope this much is helpful although I have a feeling you want to do more than what I've shown here. Keep in mind that PowerShell is actually a scripting language that is more object oriented than Bash. It's a lot like the difference between writing instructions in BASIC verses doing the same in C++ (or CSS3 vs boostrrap). If you understand one language well enough, picking out what you need from the other shouldn't be much of a problem. Google is your friend, after all.

One final comment is that the command line (cmd.exe) in Windows is no longer DOS even though it looks and feels like DOS. That's why some of the old commands you may be familiar with do not work any more. It's all PowerShell driven no matter what it looks like on the screen.
User avatar
Kellemora
Guardian Angel
Guardian Angel
Posts: 7494
Joined: 16 Feb 2015, 17:54

Re: Emulate Bash Command in Windows

Post by Kellemora »

Thanks Yogi
I don't know enough about BASH to even write the line I'm currently using for myself.
In fact, I think you are the one who wrote it for me in the first place.
I've played with it a bit so it has become a little more useful for me. I only have to enter the hours once now, and it prints out both the first needed time and the second needed time.
I actually don't enter anything, I just hit the up arrow on my keyboard until it scrolls up to the last time I used that particular timestamp, hi hi... Faster and Easier!
Post Reply