为什么要在复制/粘贴到终端之前先运行cat | bash? [英] Why would one run cat|bash before copying/pasting into a terminal?
问题描述
在将命令从剪贴板粘贴到终端之前,我见过有人在运行 cat | bash
.
I've seen someone running cat|bash
before pasting commands from the clipboard into a terminal.
bash 在bash中执行命令,我不知道为什么我们在这里需要 cat
和 |(pipe)
.
bash executes commands in bash I do not know why do we need cat
here and |(pipe)
.
这种做法有什么有益的作用?
What useful effects does this practice have?
推荐答案
是我的同事在终端中执行了一堆命令,在粘贴这些命令集之前,他做了
cat | bash
他很可能会阻止Bash将命令中的任何内容解释为编辑输入,以覆盖他设置的所有特殊键绑定和别名,和/或防止命令将命令流用作输入.
He likely this to stop Bash from interpreting anything in the commands as editing input, to override any special keybindings and aliases he's set up, and/or to prevent commands from consuming the command stream as input.
将此命令与两个嵌入式制表符一起考虑:
Consider this command with two embedded tab characters:
if true
then
echo "yup"
fi
将其粘贴到外壳中时会发生以下情况(因为制表符会触发交互式输入中的制表符完成):
Here's what happens when you paste it into a shell (because the tab characters trigger tab completion in interactive input):
$ if true
> then
>
Display all 2600 possibilities? (y or n)
! grub-mkfont pnmmontage
./ grub-mkimage pnmnlfilt
: grub-mklayout pnmnoraw
这是涉及 ssh
的代码段:
ssh localhost uptime
echo "Finished"
将其粘贴到外壳中时,第二个命令将被 ssh
用作输入,因此您将获得以下信息:
When you paste it into a shell, the second command gets consumed as input by ssh
, so you get this:
$ ssh localhost uptime
09:53:40 up 28 days, 18:33, 25 users, load average: 0.06, 0.01, 0.00
在两种情况下,都粘贴到 cat |bash
会导致命令按原样运行.
In both cases, pasting into cat | bash
instead will cause the commands to run as-is.
这篇关于为什么要在复制/粘贴到终端之前先运行cat | bash?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!