2

In Unix, how would one do this?

#!/bin/sh
x=echo "Hello" | grep '^[A-Z]'

I want x to take the value "Hello", but this script does not seem to work. What would be the proper way of spelling something like the above out?

codaddict
  • 445,704
  • 82
  • 492
  • 529
Waffles
  • 447
  • 2
  • 5
  • 11

3 Answers3

11

You can use command substitution as:

x=$(echo "Hello" | grep '^[A-Z]')

You could also use the outdated back-quote style as:

x=`echo "Hello" | grep '^[A-Z]'`
codaddict
  • 445,704
  • 82
  • 492
  • 529
2

you can also use shell internals without calling external tools, eg case/esac

str="Hello"
case "$str" in
 [A-Z]* ) x=$str;;
esac
ghostdog74
  • 327,991
  • 56
  • 259
  • 343
0

be sure that you are using expected regex supporting grep, grep has many variants across unixs.

Jokester
  • 5,501
  • 3
  • 31
  • 39