not being able to escape "$" is doing my head in.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting not being able to escape "$" is doing my head in.
# 1  
Old 04-23-2008
not being able to escape "$" is doing my head in.

why oh why oh why is this happening?

i need to replace the $ with \$ in the file below

Code:
file=products_jsp\$products_jspHelper.class

echo $file

products_jsp$products_jspHelper.class


this works

echo ${file} | sed 's/\$/\\$/'

products_jsp\$products_jspHelper.class


but this doesn't

file=`echo ${file} | sed 's/\$/\\$/'`

echo $file

products_jsp$products_jspHelper.class$

i need the new $file variable to be the old $file variable but with the $ escaped.

why is it seeing $ as the 'end of line' special character? it should be escaped.

thanks in advance.
# 2  
Old 04-23-2008
As usual, your problem is with quoting.

Code:
file='products_jsp$products_jspHelper.class'  # single quotes don't need backslashes
file="`echo "$file" | sed 's/\\$/\\\\$/'`"
echo "$file"

When you echo $file without double quotes, the shell will do backslash parsing etc. The simple lesson to learn is to always use double quotes around variables.

The doubling of backslashes in the sed expression is also an artefact of the shell's backslash parsing. In order to get a literal backslash in the regular expression, sed wants it doubled; but each of those doubled backslashes needs to be backslash-escaped in order for the shell to pass it literally to sed. (Whew.)

(Oh, and lesson two: don't trust echo to tell you the truth, the whole truth, and nothing but the truth.)

Last edited by era; 04-23-2008 at 07:34 AM.. Reason: Need absurd amounts of backslashes
# 3  
Old 04-23-2008
thanks, that works now.

i've just wasted about 2 hours trying a billion combinations through being stubborn and refusing to ask for help. i can now move on and finish my work.

cheers very much.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

3. Shell Programming and Scripting

sed escape character for comment string "/*"

Good afternoon all, I'm hoping my newbie question can help bolster someone's street_cred.sh today. I'm trying to "fingerprint" SQL on its way into the rdbms for a benchmarking process (so I can tie the resource allocation back to the process more precisely). To do this, I'm essentially... (4 Replies)
Discussion started by: toeharp
4 Replies

4. UNIX for Dummies Questions & Answers

"tail -n 1 filename" error while "head -n 1 filename" is ok?

Hi all, I was wondering why tail -n 2 filename produce an error when I manage to do similar command on head -n 2 filename SunOS{type8code0}: tail -n 2 filename usage: tail ] tail ] (2 Replies)
Discussion started by: type8code0
2 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. Solaris

Solaris escape my script from "-" to "/226"

Hello everyone. I beg your guys pardon please. I try to ls -al in many path/directories. So, I put the code in text file which look like below; ls -al / ls -al /etc ls -al /etc/default ... however, when I paste it to Solaris over SecureCRT, it seems the code was escaped from "-" to... (0 Replies)
Discussion started by: Smith
0 Replies

7. Shell Programming and Scripting

Sed - How to escape variable number of "/" (slash) ?

Hi, Can you tell me how to escape a variable number of slash characters in sed "/" ? In the script the code looks like this: cat $file_to_update | sed s/^$param/$param=$tab2*\#\*/1 And the $tab2 value is a path so it will have a number of "/" charracters. # cat db.cfg | sed... (4 Replies)
Discussion started by: majormark
4 Replies

8. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

9. UNIX for Advanced & Expert Users

What is "escape sequence" in tcsh on Solaris

I've tried \e, \033, ^[, they doesn't work with tcsh on Solaris. Anyone could give a help? Thanks, WP (2 Replies)
Discussion started by: modemer
2 Replies
Login or Register to Ask a Question