0

I am trying to store the result of a bash command within a for loop for use in a command. This is what I currently have:

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

However, I am getting these errors:

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

Does anyone know how to fix this or how to do what I am trying to do?

Thanks.

joanis
  • 10,635
  • 14
  • 30
  • 40
zhxo49
  • 37
  • 5

1 Answers1

0

Your for statement is wrong, and your variable assignment statement is also wrong. You shall write something like this:

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
nsilent22
  • 2,763
  • 10
  • 14
  • 1
    `echo "$var"`, rather. Otherwise you get the bugs described in [I just assigned a variable, but `echo $variable` shows something else!](https://stackoverflow.com/questions/29378566/i-just-assigned-a-variable-but-echo-variable-shows-something-else) – Charles Duffy Oct 19 '22 at 01:33