| 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | ### Remove const suffix |
| 4 | v=abcd |
| 5 | echo ${v%d} ${v%%cd} |
| 6 | # stdout: abc ab |
| 7 | |
| 8 | ### Remove const prefix |
| 9 | v=abcd |
| 10 | echo ${v#a} ${v##ab} |
| 11 | # stdout: bcd cd |
| 12 | |
| 13 | ### Remove const suffix is vectorized on user array |
| 14 | a=(1a 2a 3a) |
| 15 | argv.py ${a[@]%a} |
| 16 | # stdout: ['1', '2', '3'] |
| 17 | # status: 0 |
| 18 | # N-I dash/mksh stdout-json: "" |
| 19 | # N-I dash status: 2 |
| 20 | # N-I mksh status: 1 |
| 21 | |
| 22 | ### Remove const suffix is vectorized on $@ array |
| 23 | set -- 1a 2a 3a |
| 24 | argv.py ${@%a} |
| 25 | # stdout: ['1', '2', '3'] |
| 26 | # status: 0 |
| 27 | # N-I dash stdout: ['1a', '2a', '3'] |
| 28 | # N-I dash status: 0 |
| 29 | # N-I mksh stdout-json: "" |
| 30 | # N-I mksh status: 1 |
| 31 | |
| 32 | ### Remove const suffix from undefined |
| 33 | echo ${undef%suffix} |
| 34 | # stdout: |
| 35 | |
| 36 | ### Remove shortest glob suffix |
| 37 | v=aabbccdd |
| 38 | echo ${v%c*} |
| 39 | # stdout: aabbc |
| 40 | |
| 41 | ### Remove longest glob suffix |
| 42 | v=aabbccdd |
| 43 | echo ${v%%c*} |
| 44 | # stdout: aabb |
| 45 | |
| 46 | ### Remove shortest glob prefix |
| 47 | v=aabbccdd |
| 48 | echo ${v#*b} |
| 49 | # stdout: bccdd |
| 50 | |
| 51 | ### Remove longest glob prefix |
| 52 | v=aabbccdd |
| 53 | echo ${v##*b} |
| 54 | # stdout: ccdd |
| 55 | |
| 56 | ### Strip char class |
| 57 | v=abc |
| 58 | echo ${v%[[:alpha:]]} |
| 59 | # stdout: ab |
| 60 | # N-I mksh stdout: abc |
| 61 | |
| 62 | ### Strip unicode prefix |
| 63 | # NOTE: LANG is set to utf-8. Problem: there is no way to represent the |
| 64 | # invalid character! Instead of stdout-json, how about stdout-bytes? |
| 65 | v='μ-' |
| 66 | echo ${v#?} # ? is a glob that stands for one character |
| 67 | # stdout: - |
| 68 | # BUG dash/mksh stdout-repr: '\xbc-\n' |
| 69 | # BUG zsh stdout-repr: '\n' |
| 70 | |
| 71 | ### Bug fix: Test that you can remove everything with glob |
| 72 | s='--x--' |
| 73 | argv "${s%%-*}" "${s%-*}" "${s#*-}" "${s##*-}" |
| 74 | ## STDOUT: |
| 75 | ['', '--x-', '-x--', ''] |
| 76 | ## END |
| 77 | |
| 78 | ### Test that you can remove everything with const |
| 79 | s='abcd' |
| 80 | argv "${s%%abcd}" "${s%abcd}" "${s#abcd}" "${s##abcd}" |
| 81 | # failure case: |
| 82 | argv "${s%%abcde}" "${s%abcde}" "${s#abcde}" "${s##abcde}" |
| 83 | ## STDOUT: |
| 84 | ['', '', '', ''] |
| 85 | ['abcd', 'abcd', 'abcd', 'abcd'] |
| 86 | ## END |
| 87 | |
| 88 | |
| 89 |