Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Day Of Week in bash script

Tags:

bash

sed

I want to have the day of week in the variable DOW.

So I use the following bash-script:

DOM=$(date +%d) DOW=($($DOM % 7) ) | sed 's/^0*//' 

Unfortunately there I get this error: bash: 09: command not found. The expected result is 2 ( 9 % 7 = 2) in the variable $DOW.

How can I get this working? It works for the days 1-8 but because of the C-hex, there is no number over 8 available and the following message appears: bash: 09: value too great for base (error token is "09").

like image 203
bbholzbb Avatar asked Apr 09 '14 08:04

bbholzbb


People also ask

How do I get day of week in Linux?

+”%A” – Get weekday in full format i.e. as Tuesday. +”%a” – Get weekday in abbreviated format i.e. as Tue. +”%u” – Get day of week starting with Monday (1), i.e. mtwtfss. +”%w” – Get day of week starting with Sunday (0), i.e. smtwtfs.

How can we get weekday based on given date in Unix?

Very simple. Just use the date command itself with correct options. Show activity on this post.


1 Answers

Use %u. Like this:

DOW=$(date +%u) 

From the man page:

%u day of week (1..7); 1 is Monday

like image 169
hek2mgl Avatar answered Oct 03 '22 19:10

hek2mgl