Escaping embedded variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Escaping embedded variables
# 1  
Old 01-12-2009
Question Escaping embedded variables

I'm running into a problem with a differential backup script written in GNU Bash 3.0 - the following stripped down code demonstrates the problem quite nicely.

Code:
$ DATE="last tuesday"
$ date --date="$DATE"
Tue Jan  6 00:00:00 PST 2009

So far so good.

Code:
$ CMD="date --date=\"$DATE\""
$ echo $CMD
date --date="last tuesday"
$ $CMD
date: the argument `tuesday"' lacks a leading `+';
When using an option to specify date(s), any non-option
argument must be a format string beginning with `+'.
Try `date --help' for more information.

How can I wrap these variables up correctly? It seems like it's not parsing the quotes correctly, but from the contents of the CMD variable (see above), it looks fine. What gives?
# 2  
Old 01-12-2009
Code:
DATE="last tuesday"
date --date="+$DATE"

# 3  
Old 01-12-2009
Quote:
Originally Posted by Ikon
Code:
DATE="last tuesday"
date --date="+$DATE"

Code:
$ DATE="last tuesday"
$ CMD="date --date=\"+$DATE\""
$ $CMD
date: the argument `tuesday"' lacks a leading `+';
When using an option to specify date(s), any non-option
argument must be a format string beginning with `+'.
Try `date --help' for more information.

No dice.
# 4  
Old 01-12-2009
Code:
# DATE="last tuesday"
# CMD=`date --date="+$DATE"`
# echo $CMD
Tue Jan 6 00:00:00 MST 2009

# 5  
Old 01-12-2009
That just runs "date" immediately and sticks the output into the variable CMD. I want to be able to craft a longer command out of several variables, and then run it multiple times.
# 6  
Old 01-12-2009
Code:
# DATE="last tuesday"
# CMD="date --date=\"+$DATE\""
# eval $CMD

# 7  
Old 01-12-2009
Hah! That did it - thanks Ikon!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Escaping the \

So I understand that I should be able to ouput a literal \ by escaping it with a preceding \. My problem is that I am trying to ouput a script that will subsequently be run on a different system with UNC pathing, so I want to ouput two \\ in a row, but escaping them both in sequential order is not... (4 Replies)
Discussion started by: JourneyRider
4 Replies

2. Shell Programming and Scripting

escaping path

Hi I use : path=/var/www/admin echo "$path" | sed -e 's/\//\\\//g' this return \/var\/www\/admin and is ok. but path2=`echo "$path" | sed -e 's/\//\\\//g'` echo $path2 return an error: sed: -e expression #1, char 9: unknown option to `s' Can anyone help me? Thanks (3 Replies)
Discussion started by: georgian
3 Replies

3. Shell Programming and Scripting

escaping '

I'm cleaning this from some html files style='' but when I try 's/style=\'\''//' I get an unmatched ' error (4 Replies)
Discussion started by: dba_frog
4 Replies

4. Shell Programming and Scripting

Escaping ** correctly

Hello This should be easy, but bash is giving me headaches. At the command line the following command works: duplicity --include /home --exclude '**' / file:///foo Doing that from a script is not straightforward. Note that it is basically a requirement that I place the... (3 Replies)
Discussion started by: brsett
3 Replies

5. UNIX for Dummies Questions & Answers

Escaping comma with \ in file

Hi, I have pipe delimited file in which some of the description fields can have commas. e.g. 1|123|abc,def 2|456|qwert 3|345|aty,try,rty I need to convert this to a 'csv' file BUT i need to add \ before every comma present in the description values (so that my next program can read it as... (3 Replies)
Discussion started by: dsrookie
3 Replies

6. Shell Programming and Scripting

Escaping Special characters

I want to append the following line to /var/spool/cron/root: */7 * * * * /root/'Linux CPU (EDF).sh' > /dev/null 2>&1 How to accomplish this using echo? ---------- Post updated at 04:09 PM ---------- Previous update was at 04:07 PM ---------- "Linux CPU (EDF)" is actually stored in a... (11 Replies)
Discussion started by: proactiveaditya
11 Replies

7. UNIX for Advanced & Expert Users

Escaping special character stored in variables : perl

Hi just for regular use i m working on small module written in perl for getting date in specified format like i have to specify date format and then seperator to seperate date i am 95% done. now i m sure explanation i gave is not good enough so i am putting output here : C:\Documents and... (2 Replies)
Discussion started by: zedex
2 Replies

8. UNIX for Dummies Questions & Answers

Escaping backslash

I have a variable containt something like this, c:\mask\mask. How can I escape "\" in the values? I want the value as it it. (9 Replies)
Discussion started by: swmk
9 Replies

9. Shell Programming and Scripting

escaping single quote

hi, echo 'abc' will give output abc how can i get output as 'abc' plz help. thanks in advance (3 Replies)
Discussion started by: javeed7
3 Replies

10. Shell Programming and Scripting

Escaping '*' in Bash

I have the following situation ============ export DirectoryName=/tmp/xyz if ; then some_new_env=$DirectoryName"/*" ======================= I tried all the ways of escaping the '*', but still the shell seems to expand the '*' character. I want some_new_env to contain "/tmp/xyz/*" ... (7 Replies)
Discussion started by: rkshukla14
7 Replies
Login or Register to Ask a Question