LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 06-16-2004, 03:31 AM   #1
wipe
Member
 
Registered: Jun 2004
Location: High Green
Distribution: Fedora Core 4
Posts: 180

Rep: Reputation: 30
Automatic special character escaping in Bash scripts?


Hi

I would like to basically do the opposite of "echo -e" or "echo $'stuff'". That is, instead of interpreting the escaped special C-like sequences, such as \n, my Bash script would print the supplied string with everything special and non-printable in the backslash-escaped form.

It surprises me that this kind of functionality doesn't seem to be implemented as (or in) a normal utility like echo. Programs like ls seem to use such routines (--quoting-style argument). The task is probably easy with Perl, Python or some such language, but it seems a bit overkill and I'd like to do it in a shell script. If I were to make the function myself, I would have to be able to turn characters into octal sequences.

Cheers
Simon
 
Old 06-05-2009, 07:41 PM   #2
dash9
LQ Newbie
 
Registered: Mar 2009
Posts: 6

Rep: Reputation: 1
Quote:
Originally Posted by wipe View Post
Hi

I would like to basically do the opposite of "echo -e" or "echo $'stuff'". That is, instead of interpreting the escaped special C-like sequences, such as \n, my Bash script would print the supplied string with everything special and non-printable in the backslash-escaped form.

It surprises me that this kind of functionality doesn't seem to be implemented as (or in) a normal utility like echo. Programs like ls seem to use such routines (--quoting-style argument). The task is probably easy with Perl, Python or some such language, but it seems a bit overkill and I'd like to do it in a shell script. If I were to make the function myself, I would have to be able to turn characters into octal sequences.

Cheers
Simon
I think printf does what you want:
Code:
$ x="a
b"

$ printf '%q\n' "$x"
$'a\nb'

$ echo $'a\nb'
a
b
 
  


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
block special and character special files s_shenbaga Linux - Newbie 4 06-23-2015 03:16 AM
How do I insert special character with alt? Meriadoc Linux - Newbie 5 05-17-2006 11:42 AM
NOT escaping bash history ivanatora Linux - General 3 06-07-2005 02:17 PM
special character boolim Linux - General 1 08-29-2003 10:57 AM
i need help with a special character Hone101 Linux - Newbie 1 05-03-2003 08:21 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 06:01 AM.

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