开发者

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

开发者 https://www.devze.com 2023-01-20 02:28 出处:网络
In Unix, how would one do this? #!/bin/sh x=echo \"Hello\" | grep \'^[A-Z]\' I want x to take the val开发者_JS百科ue \"Hello\", but this script does not seem to work. What would be the proper way o

In Unix, how would one do this?

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

I want x to take the val开发者_JS百科ue "Hello", but this script does not seem to work. What would be the proper way of spelling something like the above out?


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


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

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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消