Listing time every second as Bash script

first time I finally started learning programming. Anyway, I'm just trying to print the time in nanoseconds every second here, and I have this:

#!/usr/bin/env bash

while true;
do
 date=(date +%N) ;
 echo $date ;
 sleep  1 ;
done

      

Now this just gives a date string which is not what I want. What's wrong? My training was pretty messy, so I hope you will excuse me for this if it is very easy. Also, I managed to do this, which worked in the prompt:

while true ; do date +%N ; sleep 1 ; done

      

But this obviously doesn't work as a script?

Edit if anyone sees this: Ahh, this actually fixes my mistake. I note that you have not added; Is it because I only defined a variable? Also, could you please explain what $ does? I thought it was for calling variables. And I see that the above line will actually work like a script; I expected the output to not fit on the screen.

+2


a source to share


3 answers


Edit

date=(date +%N) ;

      



to

date=$(date +%N)

      

+5


a source


This version should work



#!/bin/bash

while true; do
 date=$(date +"%N")
 echo Current date is $date
 sleep 1
done

      

+1


a source


You can also wrap your command between "` "(Backtick). For instance:

date=`date +%N`

      

Hello

0


a source







All Articles