1 |
#!/usr/bin/env bash |
2 |
# |
3 |
# Alias is in POSIX. |
4 |
# |
5 |
# http://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_03_01 |
6 |
# |
7 |
# Bash is the only one that doesn't support aliases! |
8 |
|
9 |
### basic alias |
10 |
shopt -s expand_aliases # bash requires this |
11 |
alias hi='echo hello world' |
12 |
hi |
13 |
echo hi # second word is not |
14 |
## STDOUT: |
15 |
hello world |
16 |
hi |
17 |
## END |
18 |
|
19 |
### alias with trailing space causes second alias expansion |
20 |
shopt -s expand_aliases # bash requires this |
21 |
|
22 |
alias hi='echo hello world ' |
23 |
alias punct='!!!' |
24 |
|
25 |
hi punct |
26 |
|
27 |
alias hi='echo hello world' # No trailing space |
28 |
|
29 |
hi punct |
30 |
|
31 |
## STDOUT: |
32 |
hello world !!! |
33 |
hello world punct |
34 |
## END |
35 |
|
36 |
### iterative alias expansion of first word |
37 |
shopt -s expand_aliases # bash requires this |
38 |
alias hi='echo hello world' |
39 |
alias echo='echo --; echo ' |
40 |
hi # first hi is expanded to echo hello world; then echo is expanded. gah. |
41 |
## STDOUT: |
42 |
-- |
43 |
hello world |
44 |
## END |
45 |
|
46 |
|
47 |
### expansion of alias with value |
48 |
shopt -s expand_aliases # bash requires this |
49 |
x=x |
50 |
alias echo-x='echo $x' # nothing is evaluated here |
51 |
x=y |
52 |
echo-x hi |
53 |
## STDOUT: |
54 |
y hi |
55 |
## END |
56 |
|
57 |
|
58 |
### first and second word are the same |
59 |
shopt -s expand_aliases # bash requires this |
60 |
x=x |
61 |
alias echo-x='echo $x' # nothing is evaluated here |
62 |
echo-x echo-x |
63 |
## STDOUT: |
64 |
x echo-x |
65 |
## END |
66 |
## BUG dash STDOUT: |
67 |
x echo x |
68 |
## END |
69 |
|
70 |
### first and second word are the same with trailing space |
71 |
shopt -s expand_aliases # bash requires this |
72 |
x=x |
73 |
alias echo-x='echo $x ' # nothing is evaluated here |
74 |
echo-x echo-x |
75 |
## STDOUT: |
76 |
x echo x |
77 |
## END |
78 |
|
79 |
### defining multiple aliases, then unalias |
80 |
shopt -s expand_aliases # bash requires this |
81 |
x=x |
82 |
y=y |
83 |
alias echo-x='echo $x' echo-y='echo $y' |
84 |
echo-x X |
85 |
echo-y Y |
86 |
unalias echo-x echo-y |
87 |
echo-x X || echo undefined |
88 |
echo-y Y || echo undefined |
89 |
## STDOUT: |
90 |
x X |
91 |
y Y |
92 |
undefined |
93 |
undefined |
94 |
## END |
95 |
|
96 |
|
97 |
### Invalid syntax of alias |
98 |
shopt -s expand_aliases # bash requires this |
99 |
alias e= 'echo --; echo' # bad space here |
100 |
e x |
101 |
## status: 127 |
102 |
|
103 |
### Dynamic alias definition |
104 |
shopt -s expand_aliases # bash requires this |
105 |
x=x |
106 |
name='e' |
107 |
val='=echo' |
108 |
alias "$name$val" |
109 |
e X |
110 |
## stdout: X |
111 |
|
112 |
### Alias detection happens before expansion |
113 |
shopt -s expand_aliases # bash requires this |
114 |
alias e='echo' |
115 |
cmd=e |
116 |
e X |
117 |
$cmd X |
118 |
e status=$? |
119 |
## STDOUT: |
120 |
X |
121 |
status=127 |
122 |
## END |
123 |
|
124 |
### Alias name with punctuation |
125 |
# NOTE: / is not OK in bash, but OK in other shells. Must less restrictive |
126 |
# than var names. |
127 |
shopt -s expand_aliases # bash requires this |
128 |
alias e_.~x='echo' |
129 |
e_.~x X |
130 |
## stdout: X |