Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 07-02-2009, 03:01 PM   #1
LQ Newbie
Registered: Jun 2009
Posts: 9

Rep: Reputation: 0
Assign file name to a variable w/out knowing file name...

I'm writing a shell script and am a little stuck. I know the name of the directory but not the file inside the directory. How can I assign the file name to a variable? I hope that makes sense. If I need to clarify this let me know. Thanks for your help!
Old 07-02-2009, 03:09 PM   #2
Registered: Dec 2007
Location: Charleston WV, USA
Distribution: Slackware 12.2, Arch Linux Amd64
Posts: 896

Rep: Reputation: 69
Something like this?

for $file in ${filenames[@]}; do echo $file; done
Old 07-02-2009, 03:16 PM   #3
LQ Newbie
Registered: Jun 2009
Posts: 9

Original Poster
Rep: Reputation: 0
Your reply made it click! Gees, I feel like an idiot! This is how I got it to work: FILE=`ls` Thank you very much!
Old 07-02-2009, 03:19 PM   #4
Senior Member
Registered: Mar 2004
Location: england
Distribution: Debian, Mint, Puppy, Raspbian
Posts: 3,421

Rep: Reputation: 200Reputation: 200Reputation: 200
you don't need ls

for file in *
   echo $file

F="*" # quote spaces

set *
echo $1  # first file
echo ${n}  #nth file
the set is space resilient.

Last edited by bigearsbilly; 07-02-2009 at 03:24 PM.


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 Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get variable from text file into Bash variable mcdef Linux - Software 2 06-10-2009 01:15 PM
How do you assign a variable to be a variable file name in a directory? David_Elliott Programming 4 04-14-2009 10:19 AM
how to select a particular line from a file and assign it to a variable? pdklinux79 Linux - Newbie 4 06-18-2008 07:21 PM
Knowing if the File is already in use or open. sameergk Programming 1 10-10-2006 04:33 AM
How to read variable from one file & update its value in another file minil Programming 1 03-22-2005 12:12 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:43 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