Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/22.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/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
Linux 如何将此结果保存在变量中?_Linux_Shell - Fatal编程技术网

Linux 如何将此结果保存在变量中?

Linux 如何将此结果保存在变量中?,linux,shell,Linux,Shell,我只想将这个命令的结果存储在一个变量中,但它不起作用 echo "${dir##*/}" | sed -r 's/[$ :@?!]+/_/g' 试一试 fixfolderNamesTest.sh: line 76: syntax error near unexpected token `|' line=(${dir##*/} | sed -r 's/[$ :@?!]+/_/g') echo "$line" 为了进一步简化您的问题,即使下面也会提供相同的错误: $ ls a.csv c

我只想将这个命令的结果存储在一个变量中,但它不起作用

echo "${dir##*/}" | sed -r 's/[$  :@?!]+/_/g'
试一试

fixfolderNamesTest.sh: line 76: syntax error near unexpected token `|'

line=(${dir##*/} | sed -r 's/[$  :@?!]+/_/g')
echo "$line"

为了进一步简化您的问题,即使下面也会提供相同的错误:

$ ls
a.csv  cr  f1  f2  file  s  t
$ ls | grep file
file
$ echo $out

$ out=(ls | grep file)
-bash: syntax error near unexpected token `|'
$ echo $out

$ out=`ls | grep file`
$ echo $out
file
我建议使用反勾号将输出捕获到变量中。

line=$(${dir###*/}sed-r的/[$:@?!]+/\ug')有效吗?这将使用
var=$(…)
out=$()
将是正确的形式。你错过了美元符号