Home > Software design >  Variable not populating in a bash script
Variable not populating in a bash script

Time:09-28

I'm having a duh moment. I'm not getting this variable populated.

for i in ~/Ws/*.png; 
do 
  echo $i; 
  FNAME=cmd basename $i;
  echo $FNAME;
done;

I am getting the following output for example

home/Ws/BrainLearning.png
BrainLearning.png

But the last line is blank and I do not understand why. Basically $FNAME is not populated with the information I was expecting.

CodePudding user response:

If your intention is to have FNAME filled with the basename of $i :

FNAME=$(basename $i)
echo "FNAME: $FNAME"

CodePudding user response:

Change

FNAME=cmd basename $i;

into

FNAME="cmd $(basename $i)";

CodePudding user response:

Your shown code has syntax errors and I suggest running your script on shellcheck.net

However what you're attempting to do can be done using this find command as well without any loop:

find ~/Ws -name '*.png' -execdir echo {}  
  • Related