LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 09-03-2012, 01:50 AM   #1
ArfaSmif
Member
 
Registered: Oct 2008
Location: Brisbane Australia
Distribution: Fedora, Centos, Manjaro
Posts: 317

Rep: Reputation: 70
bash scripting using $@


Hello all,

Before linux allowed file names with spaces I could do something like this in bash :-

for i in `ls -1`
do
do-some-stuff-to $i
done

Now $i gets split up via the spaces in the file name. How do you get the same result (as the above simple script) for file names with spaces?

I understand that "$@" is used somehow but I can't get my old brain around it anymore. I'm using 4.2.37(1)-release (x86_64-redhat-linux-gnu) if it's relevant.
 
Old 09-03-2012, 02:00 AM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Hi,

please don't use ls for things like that. It is better to use shell globbing, and you should always quote your variables. Try the following:
Code:
for i in *
do
  dosomestuffto "$i"
done
HTH,

Evo2.
 
1 members found this post helpful.
Old 09-03-2012, 03:07 AM   #3
ArfaSmif
Member
 
Registered: Oct 2008
Location: Brisbane Australia
Distribution: Fedora, Centos, Manjaro
Posts: 317

Original Poster
Rep: Reputation: 70
Thanks Evo2. Shell globbing - noted.
 
Old 09-03-2012, 06:53 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by ArfaSmif View Post
Before linux allowed file names with spaces.....
If I am not mistaken, Unix / Linux has always allowed spaces in file names. It just was not a good idea------and still isn't.
 
Old 09-03-2012, 08:05 PM   #5
ArfaSmif
Member
 
Registered: Oct 2008
Location: Brisbane Australia
Distribution: Fedora, Centos, Manjaro
Posts: 317

Original Poster
Rep: Reputation: 70
Quote:
Originally Posted by pixellany View Post
If I am not mistaken, Unix / Linux has always allowed spaces in file names. It just was not a good idea------and still isn't.
You're right. We just never used them before ...
 
  


Reply

Tags
bash script $@



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
bash scripting help fernfrancis Linux - Newbie 4 05-10-2010 01:57 AM
Reading a bash variable in bash scripting problem freeindy Programming 3 11-27-2008 02:29 AM
Bash scripting pete1234 Programming 1 09-27-2005 01:48 AM
help on bash scripting Kendo1979 Linux - Newbie 3 05-17-2005 07:10 PM
Bash scripting NSKL Linux - General 2 06-08-2002 12:10 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 08:46 AM.

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