Honored Contributor Honored Contributor
Re: not getting these variable values ${0##*/} and ${0%/*}

These basically strip down the path of the command executed to the directory (DIR) and FILE (NAME).

 

For example, I created a script:

 

 # cat /root/test.pw
#!/usr/bin/sh

typeset -x NAME=${0##*/}
typeset -x DIR=${0%/*}

echo NAME=${NAME}
echo DIR=${DIR}

 

And then I ran it.

 

First I ran it from the /root directory and invoked it with just a './test.pw'.

# ./test.pw
NAME=test.pw
DIR=.

 

The NAME is the script name and the DIR is the '.' portion indicating it's run from the current directory.

 

I then ran it as '/root/test.pw':

 

# /root/test.pw
NAME=test.pw
DIR=/root

 

NAME still gives test.pw, which it should, and DIR gives '/root' as that is the directory portion of the command line.

 

If I place the script in /var/tmp and run it via '/var/tmp/test.pw' then you get:

 

# /var/tmp/test.pw
NAME=test.pw
DIR=/var/tmp

 

If I copy the script to a directory that is in my PATH environment variable, /root/bin in this case, and run it:

 

# test.pw
NAME=test.pw
DIR=/root/bin

 

I get the directory that the script is in.

 

Does this all make sense?