I am new to unix and just started with the shell and its commands. I have to do a few tasks for an assignment.
And my Question is how do I print out -e with echo. I know there is the $ printf '%s\n' -e Option, but i have to use echo.
Asked
Active
Viewed 83 times
0

Vadim Kotov
- 8,084
- 8
- 48
- 62

MassU
- 51
- 1
- 2
- 5
-
is this some homework assignment, or why do you have to use echo? As programmer, you try to use the right tool, if possible, and in this case, that'd probably be `printf`. – Marcus Müller Nov 22 '15 at 12:51
-
What should `-e` do since the only option defined is`-n`? Additionally, `echo` has some limitations. Please see [docs] ru(http://www.freebsd.org/cgi/man.cgi?echo) for further information. – albert Nov 22 '15 at 12:58
-
@albert He's using bash's echo, which uses `-e` to mean "allow backslash escapes." The man page is for /bin/echo, which is different. Check the bash man page for all details. – Rob Napier Nov 22 '15 at 14:52
-
@Marcus Müller: Yes, its a homework assignment, and thats why i have to use echo. – MassU Nov 22 '15 at 15:21
2 Answers
2
How about this:
echo -e "\055e"
If you are ever unsure of the ASCII codes (like \055
above), you can easily check them out with
man ascii
NAME ascii -- octal, hexadecimal and decimal ASCII character sets
DESCRIPTION The octal set:
000 nul 001 soh 002 stx 003 etx 004 eot 005 enq 006 ack 007 bel 010 bs 011 ht 012 nl 013 vt 014 np 015 cr 016 so 017 si 020 dle 021 dc1 022 dc2 023 dc3 024 dc4 025 nak 026 syn 027 etb 030 can 031 em 032 sub 033 esc 034 fs 035 gs 036 rs 037 us 040 sp 041 ! 042 " 043 # 044 $ 045 % 046 & 047 ' 050 ( 051 ) 052 * 053 + 054 , 055 - 056 . 057 / 060 0 061 1 062 2 063 3 064 4 065 5 066 6 067 7 070 8 071 9 072 : 073 ; 074 < 075 = 076 > 077 ? 100 @ 101 A 102 B 103 C 104 D 105 E 106 F 107 G 110 H 111 I 112 J 113 K 114 L 115 M 116 N 117 O 120 P 121 Q 122 R 123 S 124 T 125 U 126 V 127 W 130 X 131 Y 132 Z 133 [ 134 \ 135 ] 136 ^ 137 _ 140 ` 141 a 142 b 143 c 144 d 145 e 146 f 147 g 150 h 151 i 152 j 153 k 154 l 155 m 156 n 157 o 160 p 161 q 162 r 163 s 164 t 165 u 166 v 167 w 170 x 171 y 172 z 173 { 174 | 175 } 176 ~ 177 del

Mark Setchell
- 191,897
- 31
- 273
- 432
-
When I responded Marcus that I didn't know the ASCII codes, I only meant that I did not know it by heart. When the alarm rings early, then I stutter `s/sound/silence/g` and not `cut -d "\007" -f7-`. Upvote. – Walter A Nov 23 '15 at 21:30
-1
You need some work-around. How about:
echo "x-u" | cut -c2-
or
echo "I want to learn sed x-e" | sed 's/.*x//'

Walter A
- 19,067
- 2
- 23
- 43
-
I do think this is not really any better than using `printf`; basically you could just pipe anything into `printf`, e.g. `echo "" | printf '%s' -e`; so if he's not allowed to use `printf` he's pretty surely not allowed to use `sed`, `cut` etc. – Marcus Müller Nov 22 '15 at 16:29
-
@Marcus: I just don't know. The answer of Mark seems best, but I didn't know the ASCII codes. For homework, learning how you can deal with problems like an unrealistic restriction of the forbidden use of printf, might be interesting for the OP. – Walter A Nov 22 '15 at 18:56
-
1good point, really. I'd take from this: sometimes, school (and usually a bit less often, work) gives you arbitrary restrictions. Finding your way through this is the real challenge, and hence, your answer is pretty valuable. – Marcus Müller Nov 22 '15 at 19:22
-