Waffles
Waffles

Reputation: 457

Is it possible to set variables equal to expressions in UNIX?

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?

Upvotes: 2

Views: 215

Answers (3)

Jokester
Jokester

Reputation: 5617

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

Upvotes: 0

ghostdog74
ghostdog74

Reputation: 342819

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

str="Hello"
case "$str" in
 [A-Z]* ) x=$str;;
esac

Upvotes: 2

codaddict
codaddict

Reputation: 455350

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]'`

Upvotes: 11

Related Questions