如何使用不同的参数多次 运行 一个命令?

How to run a command multiple times with different parameters?

是否可以运行一个命令多次使用不同的参数?

类似于:

sudo apt-get install asd && install qwe && remove ert && autoremove && autoclean

这会循环一组参数并将它们应用于同一命令。没有错误检查,这与您的示例不同,如果前面的命令之一失败,该示例将失败

for param in asd qwe ert; do install $param; done

没有。不幸的是,shell 无法读懂你的想法。

你可以这样做:

alias sag="sudo apt-get"
sag install asd qwe && sag remove ert && sag autoremove && sag autoclean

虽然我不相信你真的想要 && 那里;你可能对 ;

一样满意

使用 for 循环:

for cmd in "install asd" "install qwe" "remove ert" "autoremove" "autoclean"; do sudo apt-get $cmd; done

xargs:

echo -e "install asd\ninstall qwe\nremove ert\nautoremove\nautoclean" | xargs -I "#" sudo apt-get "#"

如果您从命令行工作,您可能可以使用以下命令:一旦您 运行 command parameter1,重复 commandparameter2 而不是键入:

^paramater1^parameter2

例子

我有两个文件:a1a2。让我们ls -l第一个:

$ ls -l a1
-rw-r--r-- 1 me me 21 Apr 21 16:43 a1

现在让我们为 a2 做同样的事情:

$ ^a1^a2
ls -l a2                # bash indicates what is the command being executed
-rw-r--r-- 1 me me 13 Apr 21 16:43 a2

您可以在 What is your single most favorite command-line trick using Bash? 中找到更多类似的技巧。