* [newbies] Неиспользуемые переменные в Bash и оптимизация
@ 2025-12-01 14:14 Alexander Lubyagin
2025-12-01 14:24 ` Alexander Lubyagin
2025-12-01 15:40 ` Leonid Krivoshein
0 siblings, 2 replies; 4+ messages in thread
From: Alexander Lubyagin @ 2025-12-01 14:14 UTC (permalink / raw)
To: devel-newbies
Разбирался тут с подсказками shellcheck, и обнаружил следующее.
Вот такой скрипт не выполняет функцию func:
#!/bin/bash
func()
{
echo "---" "$1"
}
i="abc:123"
echo $i
v="$(func ${i%%:*})"
# echo "$v"
# END
Если `echo` раскомментировать, то он выполняет функцию func, и
допечатывает "--- abc".
Вопрос. Bash "выкидывает" неиспользуемые ветви кода? Есть ли у него
встроенная оптимизация?
--
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [newbies] Неиспользуемые переменные в Bash и оптимизация
2025-12-01 14:14 [newbies] Неиспользуемые переменные в Bash и оптимизация Alexander Lubyagin
@ 2025-12-01 14:24 ` Alexander Lubyagin
2025-12-01 14:27 ` Alexander Lubyagin
2025-12-01 15:40 ` Leonid Krivoshein
1 sibling, 1 reply; 4+ messages in thread
From: Alexander Lubyagin @ 2025-12-01 14:24 UTC (permalink / raw)
To: devel-newbies
01.12.2025 17:14, Alexander Lubyagin пишет:
> func()
> {
> echo "---" "$1"
> }
Всё, дошло. v="$(...)" - "забирает" стандартный вывод команд.
--
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [newbies] Неиспользуемые переменные в Bash и оптимизация
2025-12-01 14:24 ` Alexander Lubyagin
@ 2025-12-01 14:27 ` Alexander Lubyagin
0 siblings, 0 replies; 4+ messages in thread
From: Alexander Lubyagin @ 2025-12-01 14:27 UTC (permalink / raw)
To: devel-newbies
01.12.2025 17:24, Alexander Lubyagin пишет:
> 01.12.2025 17:14, Alexander Lubyagin пишет:
>> func()
>> {
>> echo "---" "$1"
>> }
>
> Всё, дошло. v="$(...)" - "забирает" стандартный вывод команд.
Проверил таким кодом:
#!/bin/bash
func()
{
echo "---" "$1"
echo "+++" "$1" > /dev/stderr
}
i="abc:123"
echo $i
v="$(func \"bcdef\")|$(func ${i%%:*})"
# echo "$v"
# END
Кстати, хорошая задачка для студентов, на внимательность.
--
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [newbies] Неиспользуемые переменные в Bash и оптимизация
2025-12-01 14:14 [newbies] Неиспользуемые переменные в Bash и оптимизация Alexander Lubyagin
2025-12-01 14:24 ` Alexander Lubyagin
@ 2025-12-01 15:40 ` Leonid Krivoshein
1 sibling, 0 replies; 4+ messages in thread
From: Leonid Krivoshein @ 2025-12-01 15:40 UTC (permalink / raw)
To: devel-newbies
On 12/1/25 17:14, Alexander Lubyagin wrote:
> Разбирался тут с подсказками shellcheck, и обнаружил следующее.
>
> Вот такой скрипт не выполняет функцию func:
>
> #!/bin/bash
> func()
> {
> echo "---" "$1"
это небезопасно, лучше так:
printf "%s %s\n" "---" "$1"
> }
> i="abc:123"
> echo $i
> v="$(func ${i%%:*})"
> # echo "$v"
> # END
>
> Если `echo` раскомментировать, то он выполняет функцию func, и
> допечатывает "--- abc".
>
> Вопрос. Bash "выкидывает" неиспользуемые ветви кода? Есть ли у него
> встроенная оптимизация?
Нет. Но shellcheck может найти неиспользуемый код и предупредит об этом.
--
WBR, Leonid Krivoshein.
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2025-12-01 15:40 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-12-01 14:14 [newbies] Неиспользуемые переменные в Bash и оптимизация Alexander Lubyagin
2025-12-01 14:24 ` Alexander Lubyagin
2025-12-01 14:27 ` Alexander Lubyagin
2025-12-01 15:40 ` Leonid Krivoshein
devel@ where you _can_ ask
This inbox may be cloned and mirrored by anyone:
git clone --mirror http://lore.altlinux.org/devel-newbies/0 devel-newbies/git/0.git
# If you have public-inbox 1.1+ installed, you may
# initialize and index your mirror using the following commands:
public-inbox-init -V2 devel-newbies devel-newbies/ http://lore.altlinux.org/devel-newbies \
devel-newbies@lists.altlinux.org devel-newbies@lists.altlinux.ru devel-newbies@lists.altlinux.com
public-inbox-index devel-newbies
Example config snippet for mirrors.
Newsgroup available over NNTP:
nntp://lore.altlinux.org/org.altlinux.lists.devel-newbies
AGPL code for this site: git clone https://public-inbox.org/public-inbox.git