Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash: manipulating with strings (percent sign)

What does a percent sign mean in bash when manipulating strings? For example, what does ${0%/*} mean?

like image 304
musthero Avatar asked May 08 '13 15:05

musthero


People also ask

What does this %s mean in shell scripting?

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).

What does [- Z $1 mean in bash?

$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.

What does %% do in bash?

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.

What does ${} mean in Linux?

${} 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.


1 Answers

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.

like image 51
Ian Stapleton Cordasco Avatar answered Oct 21 '22 14:10

Ian Stapleton Cordasco