Patryk
Patryk

Reputation: 24092

Bash regex test working the opposite way?

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!

Upvotes: 1

Views: 189

Answers (1)

tripleee
tripleee

Reputation: 189317

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.

Upvotes: 3

Related Questions