Cosa fa il segno delle percentuali nelle stringhe di shell Linux?

Sommario:

Cosa fa il segno delle percentuali nelle stringhe di shell Linux?
Cosa fa il segno delle percentuali nelle stringhe di shell Linux?

Video: Cosa fa il segno delle percentuali nelle stringhe di shell Linux?

Video: Cosa fa il segno delle percentuali nelle stringhe di shell Linux?
Video: Paolo Crepet: Per paura abbiamo smesso di Parlare e Pensare - YouTube 2024, Aprile
Anonim
Quando stai imparando a utilizzare pienamente la shell di Linux, potresti trovarti curioso di sapere quanto puoi manipolare le stringhe per ottenere i migliori risultati. Con questo in mente, il post di Q & A di SuperUser di oggi ha la risposta alla domanda di un lettore curioso.
Quando stai imparando a utilizzare pienamente la shell di Linux, potresti trovarti curioso di sapere quanto puoi manipolare le stringhe per ottenere i migliori risultati. Con questo in mente, il post di Q & A di SuperUser di oggi ha la risposta alla domanda di un lettore curioso.

La sessione di domande e risposte di oggi ci viene fornita per gentile concessione di SuperUser, una suddivisione di Stack Exchange, un raggruppamento di domande e risposte basato sulla comunità.

La domanda

Lettore SuperUser Nissim Kaufmann vuole sapere qual è il segno di percentuale nelle stringhe di shell Linux:

When using the Linux shell, what does the percent sign (%) do? For example:

Image
Image

Cosa fa la percentuale di accesso alle stringhe di shell Linux?

La risposta

Il collaboratore di SuperUser Marek Rost ha la risposta per noi:

When the percent sign (%) is used in the pattern ${variable%substring}, it will return content of the variable with the shortest occurrence of substring deleted from the back of the variable.

This function supports wildcard patterns, that is why it accepts an asterisk (star) as a substitute for zero or more characters. It should be mentioned that this is Bash specific. Other Linux shells do not necessarily contain this function.

If you want to learn more about string manipulation in Bash, then I highly suggest reading the following page, Advanced Bash-Scripting Guide: Chapter 10. Manipulating Variables. Among many other handy functions, it explains what a double percent sign (%%) does, for example.

I forgot to mention that when it is used in the pattern $((variable%number)) or $((variable1%$variable2)), the percent sign (%) character will function as a modulo operator.

When the percent sign (%) is used in different contexts, it should be recognized as a regular character only.

Hai qualcosa da aggiungere alla spiegazione? Audio disattivato nei commenti. Vuoi leggere più risposte dagli altri utenti di Stack Exchange esperti di tecnologia? Controlla la discussione completa qui.

Immagine di credito: Linux Screenshots (Flickr)

Consigliato: