1

I have a script which contains the following line:

propFile="${0%/*}/anteater.properties"
  1. What does "${0%/*}" mean?
  2. This command gives a path to the script - but there is a spaces at path and script can't find this file - how to deal with it?

2 Answers 2

2

The % operator in variable expansion removes the matching suffix pattern given to it. So ${0%/*} takes the variable $0, and removes all matching /* at the end. This is equivalent to the command dirname, which, when given a path, returns the parent directory of that path.

In order to deal with spaces in bash variable, whenever expanding the variable (i.e. whenever you write $var), you should quote it. In short, always use "$var" instead of just $var.

Consider reading shell parameter expansion and variable quoting in the bash manual to learn more about these two subjects.

Sign up to request clarification or add additional context in comments.

Comments

1
  1. strips the suffix matching /*, i.e. everything after last slash including the slash itself.

  2. quote it wherever you use it (cat "$propFile").

2 Comments

Quoting definitely works. If you ever want to refer to a filename with space in it by name directly, however, you can escape your spaces with the \ character.
@asf107, and if you every want to refer to a filename stored in variable — quote it. Just in case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.