disruptive
disruptive

Reputation: 5946

Variable not populating in a bash script

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.

Upvotes: 0

Views: 133

Answers (3)

LeGEC
LeGEC

Reputation: 51850

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

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

Upvotes: 1

anubhava
anubhava

Reputation: 785146

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 {} +

Upvotes: 0

Antonio Petricca
Antonio Petricca

Reputation: 11026

Change

FNAME=cmd basename $i;

into

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

Upvotes: 0

Related Questions