Examples of Basename Command in Unix
The basename utility is used to
- Remove any prefix ending in /.
- Remove the suffix from a string.
basename [string] [suffix]
Here 'string' is the input string and suffix is the string which needs to removed from the input string.
Examples:
1. basename /usr/bin/perlscript
This will remove the prefix, /usr/bin/, and prints only the string 'perlscript'
2. basename perlscript script
This will remove the suffix 'script' from 'perlscript' and prints only 'perl'
3. basename /usr/bin/perlscript script
This will remove both the prefix and suffix and prints only 'perl'
basename command is mostly used in shell scripts to get the name of the shell script file you are running. Sample shell script code is shown below
#!/usr/bin/sh filename=`basename $0` echo $filename
posted on 2012-08-15 12:40 hunter_gio 阅读(126) 评论(0) 编辑 收藏 举报