How to parse out the end of a path in a unix shell script?
I have a .sh unix shell script that receives as a parameter a path, say /home/test/user1
. So, in the variable ${1}
, that is the parameter开发者_JAVA百科.
I want to create a file whose name is based on the last part of that path, user1. How can I parse that path to retrieve the last piece of the path?
I think you are looking for the basename command.
$ basename /home/test/user1
user1
Edit: (in response to your comment)
In a shell-script variable, you need backticks:
END_OF_PATH=`basename "${1}"`
精彩评论