&1 >/dev/null" $ ls -la $redir-6ren">
gpt4 book ai didi

linux - 无法访问 2>&1 : How to treat variable content as a redirection not as filename

转载 作者:IT王子 更新时间:2023-10-29 01:00:35 25 4
gpt4 key购买 nike

如何根据可变内容控制进程输出重定向?我试过以下,但它将 $redirect 的内容视为文件名而不是重定向本身。

$ redirect="2>&1 >/dev/null"
$ ls -la $redirect
ls: cannot access 2>&1: No such file or directory
$ redirect=""
$ ls -la $redirect
total 376
drwx------ 1 wakatana users 4096 Feb 5 15:32 .
drwx------ 1 wakatana users 4096 Feb 2 18:44 ..
-rw------- 1 wakatana users 390 Feb 5 13:34 .bashrc

最佳答案

如果您接受更改命令的顺序,这里有一种实现您想要的方法:使用别名。别名通常不受欢迎,而且,正如 Bash 手册中所述,对于几乎所有用途,别名都被 shell 函数所取代。

看来我们正处于几乎的突破口中:

$ alias redirect='2>&1 > /dev/null'
$ redirect ls
$

并且没有eval

关于linux - 无法访问 2>&1 : How to treat variable content as a redirection not as filename,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28347210/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com