How to Get Day of Week in Bash (With Examples)
You can use the following methods to get the current day of the week in Bash:Method 1: Get Day of Week as Number (e.g. 1Monday, 7 Sunday)date %uMethod 2: Get Day of Week as Full Name (e.g. “Sunday”)date %AMethod 3: Get Day of Week as Abbreviated Name (e.g. “Sun”)date %aThe following examples show how to use each method in practice.Example 1: Get Day of Week as Number in BashThis article is currently being written on a Sunday.We can use the following syntax to get the current day of the week as a number between 1 and 7 in Bash:date %uThe following screenshot shows how to use this syntax in practice:This returns7, which is the number that corresponds to Sunday as the day of the week.If you want to extract the day of the week as a number for a specific date, you can use the following syntax instead:date -d 2024-10-15 %uThe following screenshot shows how to use this syntax in practice:This returns2, which represents the day of the week (Tuesday) for the date2024-10-15that we specified.Example 2: Get Day of Week as Full Name in BashWe can use the following syntax to get the day of the week as a full name in Bash:date %AThe following screenshot shows how to use this syntax in practice:This returnsSunday, which is the day of the week represented as the full name.Example 3: Get Day of Week as Abbreviated Name in BashWe can use the following syntax to get the day of the week as an abbreviated name in Bash:date %aThe following screenshot shows how to use this syntax in practice:This returnsSun, which is the day of the week represented as the abbreviated name.Related TutorialsThe following tutorials explain how to perform other common tasks in Bash:Bash: How to Get Month NameBash: How to Compare DatesBash: How to Extract Date from String