Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


  Search this Thread
Old 02-05-2005, 02:49 AM   #1
LQ Newbie
Registered: Jan 2005
Location: Canada
Distribution: Fedora3
Posts: 26

Rep: Reputation: 15
a bash question

I have a question like this:

When I use "ls *.jpg", it's gonna print out every files jpg in my folder. the question is : How can I actually use each file as a variable ?

For example,

$ ls *jpg > new
$ cat new

So I have 3 jpg files in "new".

I want to make :
face1 = "face.jpg"
face2 = "owl-face.jpg"
face3 = "man-face.jpg"
to use:
echo $face1, $face2, $face3

Thanks a lot

Last edited by thanhVic; 02-05-2005 at 03:40 AM.
Old 02-05-2005, 04:05 AM   #2
Registered: Jun 2003
Location: Florence, SC
Distribution: Gentoo i386
Posts: 180

Rep: Reputation: 30
I'm not sure I understand exactly what you're asking, but if you want to run a command for every item in a glob, you can use the following format (a trick I probably use 10 times a day)

for FILE in *.jpg; do echo ${FILE}; done
You can put any number of commands between "do" and "done". Just terminate each with a semicolon.

It's also often useful to use a command substition in the above format.

for FILE in `cat new.txt`; do echo ${FILE]; done
Or, if your files are scattered about, you can have the find command run commands on files.

find . -iname *.jpg -exec echo \{\} \;
Note that \{\} gets replaced with the filename, and the \; is manditory
(This particular example would do the exact same thing that that find normally does, though)
Old 02-05-2005, 05:39 AM   #3
LQ Newbie
Registered: Jan 2005
Location: Canada
Distribution: Fedora3
Posts: 26

Original Poster
Rep: Reputation: 15
exactly what I need, thank you


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
A question about BASH operand Linux - Newbie 2 11-27-2005 10:49 PM
BASH question robscott Linux - Software 3 11-23-2005 10:39 AM
Bash question lnxduck Programming 3 09-30-2005 09:50 AM
bash question? shanenin Linux - Software 1 11-26-2004 01:22 PM
little bash question gplacek Linux - General 2 09-01-2004 01:10 PM > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 10:19 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration