Register
It is currently Mon Nov 24, 2014 3:21 am

Command replacement


All times are UTC - 6 hours


Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Fri Jan 30, 2009 9:06 am   

Joined: Fri Jan 30, 2009 9:00 am
Posts: 1
Hi,

I'm trying to do a script that creates two files using an array and touch command:
Code:
FILES=("first file" "second file")
touch `for i in "${FILES[@]}"; do echo $i; done`


But it creates these files:
Code:
$ ls
file
first
second
script.sh


Does anyone have any idea?

PD: I can't type the touch command into the for.


Top
 Profile  
 PostPosted: Fri Jan 30, 2009 11:47 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 229
Location: London - UK
the filenames have spaces in, you must correctly quote variables to deal with it like so

Code:
var="one two"

# the following touches 2 files called 'one' and 'two'
touch $var

# the following touches 1 file called 'one two'
touch "$var"


generally I advise always quoting and bracketing all vars if possible like so;
Code:
touch "${var}"


DW


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP