1>#! /bin/bash
2>umask 022
3>export PATH=".:~/bin:/bin:/usr/bin"
4>export HOME=`(cd ~; pwd)`
5>alias which="$HOME/c/which/which"
6
7>touch cat; chmod 755 cat; pwd
8/home/carlo/c/which
9>alias
10
11>which
12Usage: which [options] [--] COMMAND [...]
13Write the full path of COMMAND(s) to standard output.
14
15  --version, -[vV] Print version and exit successfully.
16  --help,          Print this help and exit successfully.
17  --skip-dot       Skip directories in PATH that start with a dot.
18  --skip-tilde     Skip directories in PATH that start with a tilde.
19  --show-dot       Don't expand a dot to current directory in output.
20  --show-tilde     Output a tilde for HOME directory for non-root.
21  --tty-only       Stop processing options on the right if not on tty.
22  --all, -a        Print all matches in PATH, not just the first
23  --read-alias, -i Read list of aliases from stdin.
24  --skip-alias     Ignore option --read-alias; don't read stdin.
25  --read-functions Read shell functions from stdin.
26  --skip-functions Ignore option --read-functions; don't read stdin.
27
28Recommended use is to write the output of (alias; declare -f) to standard
29input, so that which can show aliases and shell functions. See which(1) for
30examples.
31
32If the options --read-alias and/or --read-functions are specified then the
33output can be a full alias or function definition, optionally followed by
34the full path of each command used inside of those.
35
36Report bugs to <which-bugs@gnu.org>.
37
38>which --version
39GNU which v2.21, Copyright (C) 1999 - 2015 Carlo Wood.
40GNU which comes with ABSOLUTELY NO WARRANTY;
41This program is free software; your freedom to use, change
42and distribute this program is protected by the GPL.
43
44>which -- --version
45which: no --version in (.:~/bin:/bin:/usr/bin)
46
47>which cat
48/home/carlo/c/which/cat
49
50>which --show-tilde cat
51~/c/which/cat
52
53>which --show-dot cat
54./cat
55
56>which --show-tilde --show-dot cat
57./cat
58
59>which --skip-dot cat
60/bin/cat
61
62>(cd /bin; which cat)
63/bin/cat
64
65>(cd /bin; which --show-dot cat)
66./cat
67
68>(cd /bin; PATH=".:/bin:/usr/bin" which --show-dot cat)
69./cat
70
71>(cd /bin; PATH="/bin:.:/usr/bin" which --show-dot cat)
72/bin/cat
73
74>(cd /bin; PATH=".:/bin:/usr/bin" which --skip-dot --show-dot cat)
75/bin/cat
76
77>which ls
78/bin/ls
79
80>which xxx
81which: no xxx in (.:~/bin:/bin:/usr/bin)
82
83>which ./ls
84which: no ls in (.)
85
86>(cd /; which bin/ls)
87/bin/ls
88
89>(cd /; which --show-dot bin/ls)
90./bin/ls
91
92>(cd /; which --show-dot /bin/ls)
93/bin/ls
94
95>(cd /; which --show-dot bin/xxx)
96which: no xxx in (./bin)
97
98>(cd /; which --show-dot /bin/xxx)
99which: no xxx in (/bin)
100
101>which --all cat
102/home/carlo/c/which/cat
103/bin/cat
104
105>touch xxx
106>which ./xxx
107which: no xxx in (.)
108
109>chmod 711 xxx
110>which ./xxx
111/home/carlo/c/which/xxx
112
113>chmod 655 cat
114>which cat
115/bin/cat
116
117>su
118>chown root cat
119>exit
120>which cat
121/home/carlo/c/which/cat
122
123>su
124>chmod 545 cat
125>exit
126>which cat
127/bin/cat
128
129>su
130>chgrp bin cat
131>exit
132>which cat
133/home/carlo/c/which/cat
134
135>su
136>chmod 541 cat
137>exit
138>which cat
139/home/carlo/c/which/cat
140>su
141>rm -f cat
142
143>chown root xxx
144>exit
145>which ./xxx
146/home/carlo/c/which/xxx
147
148>su
149>chmod 700 xxx
150>exit
151>which ./xxx
152which: no xxx in (.)
153
154>id
155uid=1000(carlo) gid=1000(carlo) groups=1000(carlo),24(cdrom),25(floppy),29(audio),30(dip),44(video),46(plugdev),50(staff),104(scanner),107(bluetooth),109(netdev),122(lpadmin)
156>ls -l xxx
157-rwx------ 1 root carlo 0 Mar 20 17:30 xxx
158>su
159>chmod 750 xxx
160>chgrp carlo xxx
161>exit
162>which ./xxx
163/home/carlo/c/which/xxx
164
165>su
166>chgrp bin xxx
167>exit
168>which ./xxx
169which: no xxx in (.)
170
171>alias which='alias | which --tty-only --read-alias --show-tilde --show-dot'
172>alias test1='test1'
173>alias test2='echo "test2" | cat | sort'
174>alias test3='  echo "test2"|cat&sort'
175>alias test4='	ls &&sort ||/usr/bin/which || exit'
176
177>which which
178alias which='alias | which --tty-only --read-alias --show-tilde --show-dot'
179	~/c/which/which
180>which test1
181alias test1='test1'
182>which test2
183alias test2='echo test2 | cat | sort'
184	/bin/echo
185	/bin/cat
186	/usr/bin/sort
187>which test3
188alias test3='  echo test2|cat&sort'
189	/bin/echo
190	/bin/cat
191	/usr/bin/sort
192>which test4
193alias test4='	ls &&sort ||/usr/bin/which || exit'
194	/bin/ls
195	/usr/bin/sort
196	/usr/bin/which
197