inverting a binary

Hi ,

I have a binary in a variable and i want to invert it and store in a new variable. i mean, replace 0 with 1 and vice versa. I tried reading character by character with the below script but it didnt provide me the proper result.

#!/bin/bash
count=1
var1="00100011"
while [[ $count -le ${#var1} ]]
do
     var2=$(expr substr $var1 $count 1)
     if [ "$var2" -eq 1 ];
        then
        var3=0
     else
        var3=1
        echo $var3
        count=`expr $count + 1`
     fi
     done

please suggest a solution for this.

echo "$var1" | sed 's 0 - g; s 1 0 g; s - 1 g'

Homework?

Hi.

Modifying the OP code:

#!/bin/bash
count=1
var1="00100011"
printf " Original: $var1\n"
printf " Inverted: "
while [[ $count -le ${#var1} ]]
do
  var2=$(expr substr $var1 $count 1)
  if [ "$var2" -eq 1 ];
  then
    var3=0
  else
    var3=1
  fi
  # echo $var3
  printf $var3
  count=`expr $count + 1`
done
printf "\n"

producing:

 Original: 00100011
 Inverted: 11011100

Think about how the changes made it work correctly ... cheers, drl

This task is tailor made for tr:

tr 01 10

Regards,
Alister

Just to add using sed

echo 101101 | sed 'y/01/10/'
010010

--ahamed