Search code examples
bashsed

Get Day Of Week in bash script


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").


Solution

  • Use %u. Like this:

    DOW=$(date +%u)
    

    From the man page:

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