> Then there is the claim that in bash, all external commands (e.g.,
> dirname) can be avoided using this tactic:
>
> case $0 in
> */*) EXEC_DIR=${0:%/*} ;;
> *) EXEC_DIR=$PWD ;;
> esac
>
> Does that ever work? I think the problem is more than just missing
> quotes. It gives me this error:
>
> 0: %/*: syntax error: operand expected (error token is "%/*")
Note the missing colon in the second example:
[noland at a90 ~]$ ./mike.sh
./mike.sh: line 3: 0: %/*: syntax error: operand expected (error token is "%/*")
EXEC_DIR =
--------------
EXEC_DIR = .
[noland at a90 ~]$ cat mike.sh
#!/bin/bash
case $0 in
*/*) EXEC_DIR=${0:%/*} ;;
*) EXEC_DIR=$PWD ;;
esac
echo EXEC_DIR = $EXEC_DIR
echo '--------------'
case $0 in
*/*) EXEC_DIR=${0%/*} ;;
*) EXEC_DIR=$PWD ;;
esac
echo EXEC_DIR = $EXEC_DIR
This is using parameter substitution.
http://tldp.org/LDP/abs/html/parameter-substitution.html
Brock