Home > Software design >  Store Result of Bash Command in Shell Variable
Store Result of Bash Command in Shell Variable

Time:04-22

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.

CodePudding user response:

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
  • Related