LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 01-04-2014, 01:12 PM   #1
jms89
Member
 
Registered: Jan 2013
Posts: 41

Rep: Reputation: Disabled
Handling directories with white spaces in bash script


Hey all, I'm new to bash-scripting and trying to write a script which involves calling a command with a string stored as a variable, which is a directory path that contains whitespaces.

For example, consider the following very simple script;

Code:
#! /bin/bash
path="./this\ is\ a\ test\ dir/"

ls $path
The output of the script is,
Code:
./test.sh 
ls: cannot access ./this\: No such file or directory
ls: cannot access is\: No such file or directory
ls: cannot access a\: No such file or directory
ls: cannot access test\: No such file or directory
ls: cannot access dir/: No such file or directory
however typing the command directly in the terminal works fine,
Code:
 ls ./this\ is\ a\ test\ dir/
success!.txt
Can someone explain how to properly handle the whitespaces in the directory name (using the example script I posted) so I can accomplish what I'm trying to?

Thanks!
 
Old 01-04-2014, 02:17 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,454

Rep: Reputation: 2052Reputation: 2052Reputation: 2052Reputation: 2052Reputation: 2052Reputation: 2052Reputation: 2052Reputation: 2052Reputation: 2052Reputation: 2052Reputation: 2052
You need to quote it when it's used

Code:
$ path=this\ is\ a\ test
$ ls "$path"
ls: cannot access this is a test: No such file or directory
$ path="this is a test"
$ ls "$path"
ls: cannot access this is a test: No such file or directory
 
1 members found this post helpful.
Old 01-04-2014, 02:39 PM   #3
jms89
Member
 
Registered: Jan 2013
Posts: 41

Original Poster
Rep: Reputation: Disabled
Ah, I tried that but I guess you can't use the '\ ' delimiters and quotes at the same time... thanks!
 
  


Reply


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
bash scripting with directories that have spaces fakie_flip Linux - Software 14 12-17-2009 03:44 AM
Elements in a "for loop" using bash not being separated by white spaces. ralste Programming 6 11-10-2009 02:26 AM
bash: 'find ./ -type d' how to deal with directories with spaces? xaos5 Programming 4 09-06-2009 06:30 PM
omit white spaces for pattern matching in shell script bharatbsharma Programming 4 09-18-2008 08:25 PM
Bash Scripting Directories with Spaces gtwilliams Linux - Software 3 07-06-2005 03:16 PM


All times are GMT -5. The time now is 12:06 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration