1

I have written a small bash script to check machine's architecture:

#!/bin/bash

v=$(uname -m)
echo $v

if [[ $v  =~ ".*64.*" ]] ; then
  echo "64 bits!"
else 
  echo "32 bits!"
fi

being launch on my machine:

$ uname -m
x86_64

So why do I get this result ?

$ ./test.sh
x86_64
32 bits!
Patryk
  • 22,602
  • 44
  • 128
  • 244

1 Answers1

3

It works if you take out the quotes.

if [[ $v  =~ .*64.* ]] ; then

Bash is picky with the regex support; things like quoting have significant and sometimes unexpected consequences. It has also changed between Bash versions

The portable way to write that if you don't need full regex support is

case $v in *64*) echo 64 bits ;; *) echo 32 bits;; esac

This is portable all the way back to the original v7 Bourne shell.

Community
  • 1
  • 1
tripleee
  • 175,061
  • 34
  • 275
  • 318
  • If you do need to use a regular expression, it's usually better to assign it to a variable first (`regex='.*64.*'`) then expand the variable in the conditional command (`[[ $v =~ $regex ]]`). – chepner Mar 13 '14 at 19:36