# alias-p.sh: test of aliases for any POSIX-compliant shell posix="true" setup 'set -e' test_OE -e 0 'defining alias' alias a='echo ABC' __IN__ ( setup "alias a='echo ABC'" test_oE -e 0 'using alias' a a a __IN__ ABC ABC ABC __OUT__ test_OE -e 0 'redefining alias - exit status' alias a='echo BCD' __IN__ test_oE 'redefining alias - redefinition' alias a='echo BCD' a __IN__ BCD __OUT__ test_OE -e 0 'removing specific alias - exit status' alias true=false unalias true __IN__ test_OE -e 0 'removing specific alias - removal' alias true=false unalias true true __IN__ test_OE -e 0 'removing multiple aliases - exit status' alias true=a cat=b echo=c unalias true cat echo __IN__ test_oE -e 0 'removing multiple aliases - removal' alias true=a cat=b echo=c unalias true cat echo true echo ok | cat __IN__ ok __OUT__ test_OE -e 0 'removing all aliases - exit status' alias a=a b=b c=c unalias -a __IN__ test_OE 'removing all aliases - removal' alias a=a b=b c=c unalias -a alias __IN__ test_OE -e 0 'printing specific alias' alias a | grep -q '^a=' __IN__ test_oE -e 0 'reusing printed alias (simple)' save="$(alias a)" unalias a eval alias "$save" a __IN__ ABC __OUT__ test_oE -e 0 'reusing printed alias (complex quotation)' alias a='printf %s\\n \"['\\\'{'\\'}\\\'']\"' save="$(alias a)" unalias a eval alias "$save" a __IN__ "['{\}']" __OUT__ test_OE -e 0 'printing all aliases' alias b=b c=c e='echo OK' alias >save_alias_1 unalias -a IFS=' ' # trick to replace each newline in save_alias_1 with a space eval alias -- $(cat save_alias_1) alias >save_alias_2 diff save_alias_1 save_alias_2 __IN__ test_OE -e 0 'subshell inherits aliases' (alias a) | grep -q '^a=' __IN__ test_oE 'subshell cannot affect main shell' (alias a='echo BCD') a __IN__ ABC __OUT__ test_O -d -e n 'printing undefined alias is error' unalias -a alias a __IN__ test_O -d -e n 'removing undefined alias is error' alias true=false unalias true unalias true __IN__ test_oE 'using alias after assignment (simple)' alias s=sh a=A s -c 'echo $a' __IN__ A __OUT__ test_oE 'using alias after assignment (complex)' alias b=' b=B s '\''echo $a $b'\''; echo C' s=' sh -c ' a=A b __IN__ A B C __OUT__ test_OE 'using alias after redirection (simple)' alias e=echo >/dev/null e not_printed __IN__ test_OE 'using alias after redirection (complex)' alias e=' >&- c >/dev/null ' c=' echo ' /dev/null echo \> 3 < __OUT__ test_oE 'alias starting with blank' alias e=' echo' b=' { e B; }' e A b __IN__ A B __OUT__ test_oE 'alias ending with blank' alias c=cat e='echo ' e c c cat alias c='cat ' e c c cat alias echo='e x x ' x=. echo echo alias x='x . ' echo echo __IN__ cat c cat cat cat cat . x echo . x x . x . echo x . x . __OUT__ test_oE 'alias ending with blank followed by line continuation' alias foo=bar a='echo \ ' a foo __IN__ foo __OUT__ test_OE 'alias substitution can be part of an operator' alias lt='<' # The "lt" token followed by ">" becomes the "<>" redirection operator. lt>/dev/null >&0 echo not printed __IN__ test_oE 'aliases cannot substitute reserved words' alias if=: then=: else=: fi=: for=: in=: do=: done=: if true; then echo then; else echo else; fi for a in A; do echo $a; done __IN__ then A __OUT__ test_oE 'quoted aliases are not substituted' alias echo=: \echo backslash at head ech\o backslash at tail e'c'ho partial single-quotation 'echo' full single-quotation e"c"ho partial double-quotation "echo" full double-quotation __IN__ backslash at head backslash at tail partial single-quotation full single-quotation partial double-quotation full double-quotation __OUT__ test_oE 'line continuation in alias name' alias eeee=echo ee\ e\ e ok __IN__ ok __OUT__ test_oE 'line continuation between alias names (1)' alias echo='\ echo\ ' foo='\ bar\ ' bar=X echo \ foo __IN__ X __OUT__ test_oE 'line continuation between alias names (2)' alias eeee='echo\ ' eeee eeee __IN__ echo __OUT__ test_oE 'alias substitution to line continuation' alias e='echo ' bs='\' bsnl='\ ' e bs foo e bsnl bar __IN__ foo bar __OUT__ test_oE 'characters allowed in alias name' alias Aa0_!%,@=echo Aa0_!%,@ ok __IN__ ok __OUT__ test_oE 'recursive alias' alias echo='echo % ' e='echo echo' e ! # e ! # echo echo ! # echo % echo % ! __IN__ % echo % ! __OUT__ test_oE 'alias in command substitution' alias e=: func() { alias e=echo echo "$(e ok)" } func __IN__ ok __OUT__