~ubuntu-branches/ubuntu/trusty/bash/trusty-security

« back to all changes in this revision

Viewing changes to examples/functions/basename

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2014-03-03 22:52:05 UTC
  • mfrom: (1.3.5) (2.2.6 experimental)
  • Revision ID: package-import@ubuntu.com-20140303225205-87ltrt5kspeq0g1b
Tags: 4.3-1ubuntu1
* Merge with Debian; remaining changes:
  - skel.bashrc:
    - Run lesspipe.
    - Enable ls aliases.
    - Set options in ll alias to -alF.
    - Define an alert alias.
    - Enabled colored grep aliases.
  - etc.bash.bashrc:
    - Add sudo hint.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Date: Fri, 11 Oct 91 11:22:36 edt
 
2
# From: friedman@gnu.ai.mit.edu
 
3
# To: bfox@gnu.ai.mit.edu
 
4
 
 
5
# A replacement for basename(1).  Not all the systems I use have this
 
6
# program.  Usage: basename [path] {extension}
 
7
function basename ()
 
8
{
 
9
 local path="$1"
 
10
 local suffix="$2"
 
11
 local tpath="${path%/}"
 
12
 
 
13
    # Strip trailing '/' characters from path (unusual that this should
 
14
    # ever occur, but basename(1) seems to deal with it.)
 
15
    while [ "${tpath}" != "${path}" ]; do
 
16
       tpath="${path}"
 
17
       path="${tpath%/}"
 
18
    done
 
19
 
 
20
    path="${path##*/}"       # Strip off pathname
 
21
    echo ${path%${suffix}}   # Also strip off extension, if any.
 
22
}
 
23