O'Reilly logo

Korn Shell: Unix and Linux Programming Manual, Third Edition, The by Anatole Olczak

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Basename - kbasename

This is the Korn shell version of the Unix basename command. It is used to return the last part of a pathname. A suffix can also be given to be stripped from the resulting base directory. The substring feature is used to get the basename and strip off the suffix.

					#!/bin/ksh
					#
					#     kbasename - Korn shell basename
					#
					# Check arguments
					if (($# == 0 || $# > 2))
					then
					print "Usage: $0 string [suffix]"
					exit 1
					fi
					# Get the basename
					BASE=${1##*/}
					# See if suffix arg was given
					if (($# > 1))
					then
					# Display basename without suffix
					print ${BASE%$2}
					else
					# Display basename
					print $BASE
					fi
				

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required