Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a bash variable ($HTTP_COOKIE) which contains a string. I want to check if this string matches the content of a given file (token). I'm working on a busybox linux.

How should I do?

share|improve this question
What do you mean by “matches”? That file contains a regular expression? – manatwork Oct 14 '11 at 8:19
I mean that "match exactly byte per byte" the content of the file. – michelemarcon Oct 14 '11 at 8:23
up vote 4 down vote accepted
 if [ "x$HTTP_COOKIE" = "x$(cat myfile)" ]

keep the x in front in case the string may start with a minus sign (in that case it would be considered as an option to the command test ([ ... ]).

share|improve this answer
There's a bug in that code: $() removes trailing newlines, so you should check "x${HTTP_COOKIE}x" = "x$(cat myfile; echo x)" – l0b0 Oct 14 '11 at 8:54
Valid if the string in question contains newlines, but most of the times one wants to get rid of newlines. – ktf Oct 14 '11 at 9:00

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.