-
Notifications
You must be signed in to change notification settings - Fork 151
/
echo.sh
64 lines (38 loc) · 2.03 KB
/
echo.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
## echo
#POSIX 7.
## versions
#POSIX says that: A string to be written to standard output.
#If the first operand is -n, or if any of the operands contain a <backslash> character,
#the results are implementation-defined.
#Which means that is your `echo` input statrs with `-n` or contains a backslash `\`,
#behaviour is undetermined.
#To make things worse, in practice different implementations *do* have different standards.
#- On Ubuntu 13.04, `sh` has an `echo` built-in.
#This version only accepts `-n` as a command line option,
#and backslash escapes are always interpreted.
#- `/bin/echo` by GNU. On Ubuntu 13.04, `bash` has no built-in called `echo`,
#and therefore uses this one.
#In this version, you need to use the `-e` option to activate the backslash escapes.
#It seems that this is is slighltly *not* POSIX compliant since other options are introduced
#such as `-e`, and POSIX seems to mandate that such strings be printed (`echo -e 'a' would print `-e a`)
#The message then is clear: if you want to use escape chars, or ommit the ending newline,
#do *not* use `echo`. Or even better, never use echo, only `printf`.
#print to stdout:
[ "`echo a`" = a ] || exit 1
#multiple arguments are space separated:
[ "`echo a b c`" = "a b c" ] || exit 1
## gnu implementation
#As explained in the versions section, POSIX does not specificy behaviour
#if `-n` input starts or if input contains `\n`, and in practice inplementations
#recognze other command line options besides `-n`.
#Appends newline at end by default.
#-n: no final newline:
echo -n a
#Does not interpret `\` escaped chars by default:
[ "`echo 'a\nb'`" = $'a\\nb' ] || exit 1
#-e: interprets \ escaped chars:
[ "`echo -e 'a\nb'`" = $'a\nb' ] || exit 1
[ "`echo -e '\x61'`" = $'a' ] || exit 1
#Print the `-n` string:
#IMPOSSIBLE! not even gnu echo supports `--` since POSIX says that this should be supported.
#=) use `printf`.