Bash shell, trying to create and evaluate a mask

Neuromante picture Neuromante · Dec 5, 2013 · Viewed 8.6k times · Source

I'm trying to create a mask and use the bitwise operator "&" to compare to another variable and see the output. Let there be code:

mask=00000
mesk=00010
mosk=$mask&$mesk
echo $mosk
echo meec

I'm trying to expand this functionality to be able to have more characters (different error/success codes), but those lines just don't work: Executing the script will print an empty line, then "meec".

I came from an object oriented programming background, and although I've read through several documents on this subject, it seems there's something I'm missing. Any help would be appreciated.

Edit: For some reason, turns out the code doesn't work, it says "command 00010 not found" >_>

Answer

Some programmer dude picture Some programmer dude · Dec 5, 2013

It's because usually the & character in the shell is the modifier to put a command in the background.

You have to use Arithmetic Expansion of Bash (for example) for it to work:

mosk=$(($mask & $mesk))