What does a percent sign mean in bash when manipulating strings? For example, what does ${0%/*}
mean?
To bash, %s means nothing. To some programs (commands) which you launch from bash, %s might mean something. E.g., to the date command (within its “+” format string).
$1 means an input argument and -z means non-defined or empty. You're testing whether an input argument to the script was defined when running the script. Follow this answer to receive notifications.
In your case ## and %% are operators that extract part of the string. ## deletes longest match of defined substring starting at the start of given string. %% does the same, except it starts from back of the string.
${} Parameter Substitution/Expansion A parameter, in Bash, is an entity that is used to store values. A parameter can be referenced by a number, a name, or by a special symbol.
If you use @fedorqui's resource, you'll see it is going to strip the shortest match of /*
from the end of the first positional argument. An example:
example_foo(){ echo ${1%/*} } example_foo path/to/directory/sub_directory # => path/to/directory
In the example I used the second positional argument since the first is the name of the function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With