Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在UNIX中是否可以将变量设置为表达式?_Unix_Shell_Variables_Sh - Fatal编程技术网

在UNIX中是否可以将变量设置为表达式?

在UNIX中是否可以将变量设置为表达式?,unix,shell,variables,sh,Unix,Shell,Variables,Sh,在Unix中,如何执行此操作 #!/bin/sh x=echo "Hello" | grep '^[A-Z]' 我希望x获取值“Hello”,但此脚本似乎不起作用。将上述内容拼写出来的正确方法是什么?您可以将其用作: 您还可以使用过时的后引号样式,如下所示: x=`echo "Hello" | grep '^[A-Z]'` 您也可以在不调用外部工具的情况下使用shell内部构件,例如case/esac str="Hello" case "$str" in [A-Z]* ) x=$str;;

在Unix中,如何执行此操作

#!/bin/sh
x=echo "Hello" | grep '^[A-Z]'
我希望
x
获取值
“Hello”
,但此脚本似乎不起作用。将上述内容拼写出来的正确方法是什么?

您可以将其用作:

您还可以使用过时的后引号样式,如下所示:

x=`echo "Hello" | grep '^[A-Z]'`

您也可以在不调用外部工具的情况下使用shell内部构件,例如case/esac

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

请确保您使用的是支持
grep
的预期正则表达式,grep在unix中有许多变体。

+1。我非常讨厌背虱,因为它们不适合筑巢。
str="Hello"
case "$str" in
 [A-Z]* ) x=$str;;
esac