Send string to std output without interpreting (script)
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Send string to std output without interpreting (script)
I have a string containing "\ " that I need to send to standard output (in bash) without losing the "\". The only way I know of to send it to standard out in string form (i.e. $string) is with echo, but that will interpret the "\ " as " ", even with the -E flag. The work-around now is to add a few more "\" using 'sed "s/\\\ /\\\\\\\\\ /g"' (string is assigned with grep, so this is placed in between), but then the string has "\\\", so after the point where I echo it to something else I need to remove the extra "\\" before passing the same string as an argument. I guess another solution would be to send the string to sed as an argument without using |. I appreciate any help. Thanks.
ta0kira
PS Here is what I am dealing with:
Code:
#!/bin/bash
#...
grep "." $file | sed "s/\\\ /\\\\\\\\\ /g" | while read lines
do
{
begin="`echo "$lines" | sed -r "s/$end$//"`"
}
done
I need a replacement for echo in this case so I can take out the ridiculous sed command before "while".
The problem seems to only be with echo. A line of the file looks something like this:
Code:
New\ Folder /etc/New\ Folder
When I grep or cat this without piping, it shows up on the screen correctly (with the "\"s), but if you were to assign the result of the grep to a string and echo the string then it would show up like this:
Code:
New Folder /etc/New Folder
The way around this was to turn the line from the file into
Code:
New\\\ Folder /etc/New\\\ Folder
so that echo would send
Code:
New\ Folder /etc/New\ Folder
to the sed command, but then if I were to use the string as an argument for ln (etc.) later then it would see argument 1 as New\ Folder instead of taking the \ to mean that the space is a part of the name; "\\\ " becomes "\ " on the command line, whereas "\ " becomes " ":
Code:
ln -s New\ Folder /etc/New\ Folder
#vs
ln -s New\\\ Folder /etc/New\\\ Folder
[edit]
#I could use something like:
ln -s `echo "$lines"`
#but I am trying to avoid being sloppy by not adding the extra \\ to begin with
Ok, so are you saying it's 'read' that takes away the "\" and -r will keep it from doing that? I know echo also takes it away, so I'll try read with -r and echo with -E. So there is no other way to display a string other than echo? Thanks again.
ta0kira
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.