大网数据广告活动
服务器运维问答

Prevent accidental execution of commands in Linux if pasting text containing one or more return characters

· 52CE 我爱测

Sometimes if text that is on the clipboard is pasted into shell and it contains a return character it will execute even though that wasn't the intention. Sometimes even multiple lines may be pasted in by mistake and then unintend…

问题原文

原帖链接 · sa289 · CC BY-SA 3.0

Sometimes if text that is on the clipboard is pasted into shell and it contains a return character it will execute even though that wasn't the intention. Sometimes even multiple lines may be pasted in by mistake and then unintended commands could be run.

I understand in a perfect world, these mistakes would never happen, but is there a way to prevent this to avoid the risk of human error?

社区高分解答

原帖链接 · Kal · CC BY-SA 4.0

What you want is called 'bracketed paste', a feature that's available in some shells.

If your version of bash supports it, you can turn it on for the current session like so:

bind 'set enable-bracketed-paste on'

Now try it out by pasting multiple lines:

echo Hello world
echo Again, I say, hello!

The shell recognises that the text was pasted (not typed), and waits with a prompt for your confirmation. If it all looks safe to proceed, hit the Enter key. If not, hit Control-C to cancel.

If you'd like to enable bracketed paste for every new session, add the command to your .inputrc file:

cd ~
echo "set enable-bracketed-paste" >> .inputrc

Another option is to switch from bash to zsh, where bracketed paste is enabled by default. To replace your current shell with a zsh shell:

exec zsh

With zsh, pasted text gets highlighted, which is nice. Again, hit Enter to execute the command/s or Control-C to cancel.

If you like zsh, and wish to make it the default shell:

chsh -s /bin/zsh

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2015-10-22)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0 / CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。