string manipulation in ksh

Hi all,

I'm trying to extract the name of a script that is being run with a full path. i.e.
if the script name is /some/where/path/script_name.ksh
I'd like to extract only: script_name

i know that it is possible to do so in two phases:
echo "${0##/}" will give me script_name.ksh
and echo "${0%.
}" will give me /some/where/path/script_name

and joining them:
a=`echo "${0##/}"`
echo $a
echo "${a%.
}"
Does anybody know how to do this is one command, without using the temporary variable "a"?

Thanks

With sed:

echo "$0" | sed 's_.*/\(.*\)\..*_\1_'

Yes :slight_smile:

% s=/some/where/path/script_name.ksh zsh -c 'print $s:t:r'
script_name

P.S. What's the problem with the two-pass approach?

There is a little-known usage of "basename" for this. You specify the suffix as the second parameter. See "man basename".

basename /some/where/path/script_name.ksh .ksh

Response is:
script_name

I wanted it to be in one command so it would be more compact.

the basename command is also useful.

Thanks