LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-24-2008, 06:44 PM   #1
DrPoodle
LQ Newbie
 
Registered: Jul 2008
Posts: 2

Rep: Reputation: 0
Bash Directory Variables


Hey, I'm not new the Linux or programming in general but I am new to bash programming. I wonder if anyone can help me with this problem.

Essentially I want to pass in two directories into a bash script in the following manner:

% script /home /media/disk\ 1

The variables go through ok and are set to $1 and $2 respectively, but in the process the "\ " in the second variable becomes a space. The problem occurs in the script because I am using the second directory as a copy-to directory and so instead of things being copied to /media/disk\ 1 they are copied to the non-existent directory "1".

Is there a simple way to set a variable to include the "\ "? I've tried various replace methods but all of them come up with errors. Any help would be most appreciated.

~DrPoodle
 
Old 07-24-2008, 07:51 PM   #2
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
See Lecture 4 Notes: http://cis68b1.mikecappella.com/
 
Old 07-24-2008, 08:07 PM   #3
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
The way this works is the first thing that happens is your shell parses the entire command line and then calls the resulting command (including parameters). So, your shell sends the "script" the parameters:

/home /media/disk 1

You can fix this from the command line by escaping the \ like:

% script /home /media/disk\\\ 1

Or you can fix it inside the script with sed like:

Code:
SOURCE=`echo $1 | sed - 's/ /\\ /'`
DEST=`echo $2 | sed - 's/ /\\ /'`
HTH

Forrest
 
Old 07-24-2008, 08:12 PM   #4
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
There is no reason to perform these operations; this is a clumsy approach, which only addresses spaces (and not other meta-characters). Perhaps a bit of reading might give you some better solutions.
 
Old 07-24-2008, 08:18 PM   #5
DrPoodle
LQ Newbie
 
Registered: Jul 2008
Posts: 2

Original Poster
Rep: Reputation: 0
Many thanks. It's a wonder what simple quotes can do to a program
 
Old 07-24-2008, 08:23 PM   #6
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
You're on it! Go DrPoodle!
 
  


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: what happens to my variables? elinenbe Programming 5 01-30-2008 02:32 PM
Directory variables in a bash script madtinkerer Programming 9 12-04-2006 12:30 PM
Help With Variables In Bash jamie_h Programming 3 11-01-2006 08:18 AM
bash variables pfaendtner Linux - Newbie 4 11-23-2004 01:00 PM
Variables in bash tcaptain Programming 1 03-03-2003 02:07 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:02 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
Open Source Consulting | Domain Registration