1

I'm trying to run a command on every entry in a line of text:

#! /bin/bash

range=$(ls -s /home/user/Pictures/Wallpapers | wc -l)

for (( i=0; i<$range; i++ ))
do  
   d=$(ls /home/user/Pictures/Wallpapers | paste -sd " " | awk -F' ' '{ print $i }')
echo $d
done

But when I run it, it simply prints out the entire line of file entries with every iteration of the loop. I think it's because the "i" is not being interpreted since it's technically in between single quotes, and I can't figure out how to prevent it from doing this.

Thanks.

3
  • 1
    What are you trying to do? Print out the numbers from 0 to $range, $range times? Commented Oct 2, 2013 at 22:17
  • I'm trying to print out every filename in a directory, range is the number of files in the directory, so I want to print out a file name $range times. Commented Oct 2, 2013 at 22:23
  • 2
    That's all? Why complicate it with paste and awk? Commented Oct 2, 2013 at 22:28

3 Answers 3

3

You can pass variables to awk with -v argument like in:

d=$(ls /home/user/Pictures/Wallpapers | paste -sd " " | awk -v var="$i" -F' ' '{ print var }')
Sign up to request clarification or add additional context in comments.

Comments

1

To print the names of files in a directory repeated as many times as there are files in the directory, you can use:

for x /home/user/Pictures/Wallpapers/*; do
   ls /home/user/Pictures/Wallpapers/
done

or

for x /home/user/Pictures/Wallpapers/*; do
  for y /home/user/Pictures/Wallpapers/*; do
    echo $x
  done
done

Comments

0
for f in /home/user/Pictures/Wallpapers/*; do echo $f; done

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.