I am trying to understand a test script, which includes the following segment:
SCRIPT_PATH=${0%/*}
if [ "$0" != "$SCRIPT_PATH" ] && [ "$SCRIPT_PATH" != "" ]; then
cd $SCRIPT_PATH
fi
What does the ${0%/*} stand for? Thanks
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It is called
Parameter Expansion. Take a look at this page and the rest of the site.What
${0%/*}does is, it expands the value contained within the argument 0 (which is the path that called the script) after removing the string/*suffix from the end of it.So,
$0is the same as${0}which is like any other argument, eg.$1which you can write as${1}. As I said$0is special, as it’s not a real argument, it’s always there and represents name of script. Parameter Expansion works within the{}— curly braces, and%is one type of Parameter Expansion.%/*matches the last occurrence of/and removes anything (*means anything) after that character. Take a look at this simple example: