Echo backslash


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Echo backslash
# 1  
Old 03-25-2011
Echo backslash

If I echo "\\" I get a backslash returned

Code:
~$ echo "\\"
\

Why doesn't this work:
Code:
string=`echo "\\"`
echo $string

I get the error message:

bash: command substitution: line 1: unexpected EOF while looking for matching `"'
bash: command substitution: line 2: syntax error: unexpected end of file

How do I eval a string like "awk 'BEGIN{ FS="\""}/string/ {print $0}' filename" and attach the result to a variable??

The backslash of this string creates a problem when i try
Code:
variable=`eval $string`


Last edited by locoroco; 03-25-2011 at 08:28 PM..
# 2  
Old 03-25-2011
Code:
string=`echo "\\\"`
echo $string
\

This User Gave Thanks to yinyuemi For This Post:
# 3  
Old 03-28-2011
Quote:
Originally Posted by locoroco
Why doesn't this work:
Code:
string=`echo "\\"`
echo $string

Backslashes within backquoted command substitutions do not behave the same way they would outside of backqutoed command substitution even when the command is otherwise identical (in this case an echo with a double-quoted argument). The backslashes are seen/parsed/consumed by the shell before it even invokes the subshell. In this case, you are left with echo "\" and so you get the error that the closing quote is not found because it's being escaped in the subshell.

Do yourself a massive favor and abandon all use of this archaic and obsoleted form of command substitution. It's quirky and buggy. Use $(...) instead and you'll live longer.

The string=$(echo "\\") form will work as expected, even as the command within the parenthesis increases in complexity. The same cannot be said of `...`.

Regards,
Alister

Last edited by alister; 03-28-2011 at 12:22 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Not able to input \ backslash when vi editor

Hi, How do I input \ when I do a vi of my file ? I try to input the \ but it came out as @. Appreciate any help. (4 Replies)
Discussion started by: snowfrost88
4 Replies

2. Shell Programming and Scripting

[Solved] Disappearing backslash

Let's say I have a text file called process.out that contains: cn=long\, ann,cn=users cn=doe\, john,cn=users I need to have the following appended in the beginning ldapdelete -h $OIDHOST So the final output looks like: ldapdelete -h $OIDHOST "cn=long\, ann,cn=users" ldapdelete -h... (4 Replies)
Discussion started by: exm
4 Replies

3. Shell Programming and Scripting

Disabling Backslash Interpretation with "echo -E"?

Hello All, In a Bash Script I'm writing I have a section where I loop through a text file that was outputted by another script. In the text file some of the strings in there are enclosed with the BOLD "character sequences" (i.e. "\033But it's weird, because if I run this command: echo -E... (12 Replies)
Discussion started by: mrm5102
12 Replies

4. Shell Programming and Scripting

md5sum on a file with backslash in its name

Hi there, I found something very weird! Should I report that as a bug or is it me misusing the command? I've got a file with a backslash in its name. I know it's a horrible policy but it's not me. The file came from a mac computer because this is a backup server. Anyway, when using... (8 Replies)
Discussion started by: chebarbudo
8 Replies

5. UNIX for Dummies Questions & Answers

Weird behavior of backslash, please help!!

Hi I am getting absurd behavior of escape character in echos as followed:oinlcso003{arsadm} #: echo "\as shdd" \as shdd oinlcso003{arsadm} #: echo "Well, isn't that \"special\"?" Well, isn't that "special"? oinlcso003{arsadm} #: echo "Well, isn't that \special\?" Well, isn't that \special\?... (3 Replies)
Discussion started by: nixhead
3 Replies

6. UNIX for Dummies Questions & Answers

How to correctly use an echo inside an echo?

Bit of a weird one i suppose, i want to use an echo inside an echo... For example... i have a script that i want to use to take users input and create another script. Inside this script it creates it also needs to use echos... echo "echo "hello"" >$file echo "echo "goodbye"" >$file ... (3 Replies)
Discussion started by: mokachoka
3 Replies

7. Shell Programming and Scripting

How to use backslash and variables in sed

I have a line that contains backslashes in which I want sed to substitute text with variables. The line; \\s008\2033330user$ I want to change this in \\s008.ourschool.com\2033330user$ I now use this script: USER=2033330user sed 's/\\'"$USER"'/.ourschool.com\\'"$USER/" This doesn't... (3 Replies)
Discussion started by: Tubbie
3 Replies

8. Shell Programming and Scripting

double backslash in ksh

Hi I need the "\\hello" (without double quotes) to be written to a file. echo "\\\\hello" >file is working under bash shell but not working under ksh shell (gives only one / in the output) Please advise. TIA Prvn (4 Replies)
Discussion started by: prvnrk
4 Replies

9. 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

10. UNIX for Dummies Questions & Answers

backslash issues

Hi, I have a script which looks through an input file and takes data from the file to use within the script. Everything works fine until the script reads the item \windows\directory\structure\ from the input file into a variable. As unix sees the backslash as an escape character, the... (5 Replies)
Discussion started by: Bab00shka
5 Replies
Login or Register to Ask a Question