1

I have a string, like this:

DayOfWeek=$(date +%A|tr -d '\n') echo $DayOfWeek Montag

I wish to compare the result of the command with a string. Just like this:

[ $DayOfWeek == "Monday" ] && echo True But the result was anytime false.

Here some more attempts:

[ "$DayOfWeek" == "Monday" ] && echo True [ $(date +%A|tr -d '\n') == "Monday" ] && echo True [ $(date +%A|tr -d '\n') == "Monday" ] && echo True [ $(date +%A|tr -d '\n') == 'Monday' ] && echo True [ "$(printf "%s" "$DayOfWeek")" == "Monday" ] && echo True [ "$(date +%A|tr -d '\n')" == 'Monday' ] && echo True [[ $(date +%A|tr -d '\n') == 'Monday' ]] && echo True [[ $(date +%A|tr -d '\n') == "Monday" ]] && echo True

In the end following is working: [[ $(date +%A|tr -d '\n') == $(echo -n "Montag") ]] && echo True

Can someone explain this behavior of bash to me? The used bash is version 4.4.19.

best regards, akendo

Akendo
  • 2,299
  • 2
  • 18
  • 16
  • Some comment to this: I assume at first, that the result wasn't stored as a string. I took a look at the [documentation for 'if' in bash](http://www.tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html#sect_07_01_02_03). In the last line of the page, there is an quite similar example to mine approach. In the example they do quite the same, just using a `!=` instead of `==` so it should be remain a string, even when stored into a variable of bash? – Akendo Mar 05 '18 at 09:46
  • 1
    `=` is the normal comparison operator for strings, not `==`. What are you trying to do with that `tr`? It is not needed unless maybe you're messing with IFS? – Mat Mar 05 '18 at 09:48
  • 1
    Every single one of your attempts works for me (`bash 4.4.12`) - note your last example has `Montag` - this is a locale, not a bash issue... – match Mar 05 '18 at 09:50
  • I had this [stackoverflow link](https://stackoverflow.com/a/2237103/997915) that seems to support my view. The `tr` will remove the newline, because I did assume that he would store it within the string and that was the reason for the failure. – Akendo Mar 05 '18 at 09:51
  • I'm going to check the compre with one `=`. – Akendo Mar 05 '18 at 09:52
  • Same result as with `==`. This makes sense, because in the official documentation of bash it is written as: `[ STRING1 == STRING2 ]` – Akendo Mar 05 '18 at 09:53
  • `==` when using `[[ ]]`, but `=` when using `[ ]`. – Mat Mar 05 '18 at 09:58
  • 1
    `[ $DayOfWeek == "Monday" ]` is exactly the wrong way to quote. The string `Monday` does not contain any variables and there is no need to protect it from interpolation. Ignoring the question of `==` instead of `=`, it is better to write this as `[ "$DayOfWeek" = Monday ]`. Since quotes seem to cause confusion, it would be preferred to write `[ "$DayOfWeek" = "Monday" ]` – William Pursell Mar 05 '18 at 09:59

1 Answers1

1

You are using the English word Monday in your attempts, but your working example uses Montag. I'm going to guess you're using a DE locale.

You can fix this by either using Montag in your comparison, or if you really need to use English words, do something like:

LC_ALL=en_US.utf8 date +%A

To force using an English locale.

match
  • 10,388
  • 3
  • 23
  • 41