将 Bash 命令的结果存储在 Shell 变量中

Store Result of Bash Command in Shell Variable

我正在尝试将 bash 命令的结果存储在 for 循环中以供命令使用。这是我目前拥有的:

for filename in /home/WIN/USER/files/*
var=$(basename ${filename%.*}) | awk -F'[_.]' '{print }'
do echo var
done

但是,我收到了这些错误:

./script.sh: line 2: syntax error near unexpected token `var=$(basename ${filename%.*})'
./script.sh: line 2: `var=$(basename ${filename%.*}) | awk -F'[_.]' '{print }''

有谁知道如何解决这个问题或如何做我想做的事?

谢谢。

你的for语句是错误的,你的变量赋值语句也是错误的。你应该这样写:

for filename in /home/WIN/USER/files/*; do
    var=$( your shell code goes here ) # you assign the output of the shell code here
    echo $var # you echo the results here
done