BladeSystem Forums have moved here
To make BladeSystem information easier to find, we have moved the BladeSystem forums here, to Servers and Operating Systems.
Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

not getting these variable values ${0##*/} and ${0%/*}

SOLVED
Go to Solution
Trusted Contributor

not getting these variable values ${0##*/} and ${0%/*}

Hi,

 

Please tell what is the actual value (         ${0##*/}  and  ${0%/*}      ) defined to the variable below. we have a script and i did not get these values

 

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

 

 

Thanks

4 REPLIES
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?

Highlighted
Acclaimed Contributor

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

Basically these are the shell builtin replacements for dirname(1) and basename(1), operating on $0:

export DIR=$(dirname $0)

export NAME=$(basename $0)

Trusted Contributor

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

Thanks Partick and Dennis,

so it means if it use the same to $1 variable, then it will provide the DIR and NAME value for that script executed...correct?

e.g. typeset value=$1
typeset file=${1##*/}

It will give me the value of the file name i will be entering at $1 variable...got it.

But how do i get details about these symbols like ##*/ or %/* as one counts for file name and other for directory name. Are there other symbols like these and how can i get all those symbols so that i can use them also???

Much Thanks for your reply.
Acclaimed Contributor

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

[ Edited ]

>how do I get details about these symbols like ##*/ or %/*

 

Read the manpage carefully.  Look at what other well written shell scripts do.

And you also need to understand patterns.

 

>Are there other symbols like these

 

Look at the man pages for real shells: ksh(1) and sh_posix(1)

Parameter Substitution:

${parameter}  
${#parameter}
${#identifier[*]}
${parameter:-word}
${parameter:=word}
${parameter:?word}
${parameter:+word}


${parameter#pattern}
${parameter##pattern}
        If the shell pattern matches the beginning of  the value of parameter, the value of this  substitution is the value of the parameter with the matched portion deleted; otherwise the value of this parameter substituted.  In the former case, the smallest matching pattern is deleted; in the latter case, the largest matching pattern is deleted.

${parameter%pattern}
${parameter%%pattern}
        If the shell pattern matches the end of the value of parameter, the value of parameter with the matched part is deleted; otherwise substitute the value of parameter.  In the former, the smallest matching pattern is deleted; in the latter, the largest matching pattern is deleted.

$#
$-
$?
$$
$_
$!