From: Leonid Krivoshein <klark.devel@gmail.com>
To: devel-newbies@lists.altlinux.org
Subject: Re: [newbies] Неиспользуемые переменные в Bash и оптимизация
Date: Tue, 2 Dec 2025 05:12:36 +0300
Message-ID: <5aa01baf-424b-42f1-8da9-073cae10e65b@gmail.com> (raw)
In-Reply-To: <216bcbe7-65e7-443e-a1dc-77369a7eece3@yandex.ru>
On 12/1/25 22:25, Alexander Lubyagin wrote:
> 01.12.2025 18:40, Leonid Krivoshein пишет:
>>> echo "---" "$1"
>>
>> это небезопасно, лучше так:
>>
>> printf "%s %s\n" "---" "$1"
>
> Небезопасно, потому что echo может быть внешней программой, и быть
> подменена?
>
Потому что echo может воспринимать аргументы как опции, особенно, если
они идут в самом начале. И допускает бинарный вывод с "-e". То есть,
результат может быть не таким, как его ожидают. В зависимости от
аргументов и версии оболочки, неожиданная реакция может быть и на такие
аргументы, как --help или --version.
Кстати по этой причине многие программы допускают отделение свободных
аргументов от параметров необязательным отбойником "--". В этом случае,
чему бы не была равна переменная, она будет воспринята как свободный
аргумент. Например:
name="-x my-binary"
:> "$name"
chmod +x -- "$name"
ls -- "$name"
Те, кто привык писать chmod +x $name или ls $name, получат неприятный
сюрприз в этом месте. :-)
Хотя echo встроена во все оболочки, её можно переопределить, как любое
другое имя. Например, так:
echo() {
: Do nothing
}
--
WBR, Leonid Krivoshein.
prev parent reply other threads:[~2025-12-02 2:12 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-12-01 14:14 Alexander Lubyagin
2025-12-01 14:24 ` Alexander Lubyagin
2025-12-01 14:27 ` Alexander Lubyagin
2025-12-01 15:40 ` Leonid Krivoshein
2025-12-01 19:25 ` Alexander Lubyagin
2025-12-02 2:12 ` Leonid Krivoshein [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=5aa01baf-424b-42f1-8da9-073cae10e65b@gmail.com \
--to=klark.devel@gmail.com \
--cc=devel-newbies@lists.altlinux.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
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