Переменные bash: различия между версиями

Материал из ALT Linux Wiki
Нет описания правки
Нет описания правки
Строка 10: Строка 10:
|<code>"${url#*/}"</code>
|<code>"${url#*/}"</code>
|style="text-align:center"|<s>https:/</s>/guide.bash.academy/variables.html
|style="text-align:center"|<s>https:/</s>/guide.bash.academy/variables.html
<br/>↓<br/>
↓<br/>
/guide.bash.academy/variables.html
/guide.bash.academy/variables.html
|-
|-
Строка 17: Строка 17:
|<code>"${url##*/}"</code>
|<code>"${url##*/}"</code>
|style="text-align:center"|<s>https://guide.bash.academy/</s>variables.html
|style="text-align:center"|<s>https://guide.bash.academy/</s>variables.html
<br/>↓<br/>
↓<br/>
variables.html
variables.html
|-
|-
Строка 24: Строка 24:
|<code>"${url%/*}"</code>
|<code>"${url%/*}"</code>
|style="text-align:center"|https://guide.bash.academy<s>/variables.html</s>
|style="text-align:center"|https://guide.bash.academy<s>/variables.html</s>
<br/>↓<br/>
↓<br/>
https://guide.bash.academy
https://guide.bash.academy
|-
|-
Строка 31: Строка 31:
|<code>"${url%%/*}"</code>
|<code>"${url%%/*}"</code>
|style="text-align:center"|https:<s>//guide.bash.academy/variables.html</s>
|style="text-align:center"|https:<s>//guide.bash.academy/variables.html</s>
<br/>↓<br/>
↓<br/>
https:
https:
|-
|-
Строка 38: Строка 38:
|<code>"${url/./-}"</code>
|<code>"${url/./-}"</code>
|style="text-align:center"|https://guide<s>.</s>bash.academy/variables.html
|style="text-align:center"|https://guide<s>.</s>bash.academy/variables.html
<br/>↓<br/>
↓<br/>
https://guide-bash.academy/variables.html
https://guide-bash.academy/variables.html
|-
|-
Строка 45: Строка 45:
|<code>"${url//./-}"</code>
|<code>"${url//./-}"</code>
|style="text-align:center"|https://guide<s>.</s>bash<s>.</s>academy/variables<s>.</s>html
|style="text-align:center"|https://guide<s>.</s>bash<s>.</s>academy/variables<s>.</s>html
<br/>↓<br/>
↓<br/>
https://guide-bash-academy/variables-html
https://guide-bash-academy/variables-html
|}
|}
{{Category navigation|title=Системному администратору|category=Admin|sortkey={{SUBPAGENAME}}}}
{{Category navigation|title=Системному администратору|category=Admin|sortkey={{SUBPAGENAME}}}}
[[Категория:Admin]]
[[Категория:Admin]]

Версия от 02:52, 16 марта 2022

Всякий раз вспоминая, как можно выудить часть bash-переменной без разбиения её вызовом команд, гуглил что-то вроде этой статьи. Чтобы в дальнейшем искать не приходилось, решил перевести сюда тот самый блок, ради которого всё затевалось.

Разберём оперирование переменными на примере строки url='https://guide.bash.academy/variables.html'.
Оператор Действие Пример Результат
${переменная#образец} Удаление кратчайшего куска, совпадающего с образцом в начале строки. "${url#*/}" https://guide.bash.academy/variables.html


/guide.bash.academy/variables.html

${переменная##образец} Удаление длиннейшего куска, совпадающего с образцом в начале строки. "${url##*/}" https://guide.bash.academy/variables.html


variables.html

${переменная%образец} Удаление кратчайшего куска, совпадающего с образцом в конце строки. "${url%/*}" https://guide.bash.academy/variables.html


https://guide.bash.academy

${переменная%%образец} Удаление длиннейшего куска, совпадающего с образцом в конце строки. "${url%%/*}" https://guide.bash.academy/variables.html


https:

${переменная/образец/заменитель} Замена первого вхождения, совпадающего с образцом. "${url/./-}" https://guide.bash.academy/variables.html


https://guide-bash.academy/variables.html

${переменная//образец/заменитель} Замена каждого вхождения, совпадающего с образцом. "${url//./-}" https://guide.bash.academy/variables.html


https://guide-bash-academy/variables-html