Where does the 8 come from? (or what happend to the first `)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Where does the 8 come from? (or what happend to the first `)
# 1  
Old 03-10-2009
Where does the 8 come from? (or what happend to the first `)

Code:
$ ksh
$ echo "\047"0"\047"

result:
Code:
8'

# 2  
Old 03-10-2009
Code:
# ksh
# echo "\047"0"\047"
\0470\047

# 3  
Old 03-10-2009
Quote:
Originally Posted by Neo
Code:
# ksh
# echo "\047"0"\047"
\0470\047

But not in cygwin or aix.
# 4  
Old 03-10-2009
Wow. On AIX, I had to treble the number of '\' to get the intended result.

# echo "\\\047"0"\\\047"
\0470\047
# echo "\\047"0"\\047"
8'
# echo "\047"0"\047"
8'

Tried with other numbers also:
# echo "\046"0"\046"
0&
# echo "\045"0"\045"
(%
# echo "\044"0"\044"
$
# echo "\043"0"\043"
#

Who knew?...
# 5  
Old 03-10-2009
use 'printf'
Code:
$ ksh
$ echo "\047"0"\047"
8'
$ printf "\047"0"\047"
'0'

# 6  
Old 03-10-2009
To answer the question, the echo man page says:
Code:
 \0n   where n is the 8-bit character whose ASCII  code
                 is the 1-, 2- or 3-digit octal number represent-
                 ing that character.

So the \0 just says that 1, 2, or 3 digits may follow. That zero in \0 is not counted as of of the digits. So
echo "\047"0
is seen by the echo command exactly like
echo "\0470"
and is interrpreted as an attempt to use a byte with the octal value "470". This is too big and so leading bits are dropped. In this case the bit represented by the 4 is dropped and character becomes "\070".

Code:
# echo "\0470" | od -to1
0000000 070 012
0000002
# bc
obase=2
ibase=8
470
100111000
#

Edit:
By the way, a couple of ways that do work:
Code:
# echo "'0'"
'0'
# echo "\00470\0047"
'0'
#

# 7  
Old 03-10-2009
That answers it. Thank you very much!
Login or Register to Ask a Question

Previous Thread | Next Thread

2 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

What happend?

Hi everybody: Could anybody tell me how I can solve this mistake. I have two files and i would like one, so I use the cat command, like always: cat file1.dat file2.dat > file3.dat But the first line from file2.dat is concatenated with the last line from file1.dat: file1.dat... (2 Replies)
Discussion started by: tonet
2 Replies

2. Shell Programming and Scripting

its happend after restart.

From Sept 12th onwards i am facing some problem in the file permission. Normally the file get generated by a script with the permission 666 and from sept 12th onwards it get changed to 644 If we create a blank file using touch command it is with the permission 666, but the files which are... (3 Replies)
Discussion started by: onlytorobin
3 Replies
Login or Register to Ask a Question