在命令行中在unix中传递参数

时间:2022-05-26 23:15:35

How can I pass arguments to a command in unix? For example, if I have to open a file:

如何在unix中将参数传递给命令?例如,如果我必须打开一个文件:

R> vi john/pic/mars/NASA/rover.txt

In the above vi command, I want to replace "mars" with a variable, and pass the variable value in the same line, as in:

在上面的vi命令中,我想用变量替换“mars”,并在同一行中传递变量值,如:

R> vi john/pic/$variable/NASA/rover.txt | $varaiable=pluto

Of course this doesn't work. But I hope my question is clear. Can anyone help me with this?

当然这不起作用。但我希望我的问题很明确。谁能帮我这个?

1 个解决方案

#1


1  

Simply move the variable definition to the beginning of the command line, as in:

只需将变量定义移动到命令行的开头,如下所示:

variable=pluto; vi john/pic/$variable/NASA/rover.txt

or even:

甚至:

variable=pluto && vi john/pic/$variable/NASA/rover.txt

OBS:

OBS:

  1. notice you can't use $ while defining variables, only while using their values;
  2. 注意,只有在使用它们的值时,才能在定义变量时使用$;
  3. piping your vi command to a variable assignment does not make much sense, although you could achieve some clearer parameterization from:

    尽管你可以从以下方面获得更清晰的参数化,但是将vi命令输入到变量赋值没有多大意义。

    function opener() {
        vi john/pic/$1/NASA/rover.txt
    }
    $ opener "pluto"
    

#1


1  

Simply move the variable definition to the beginning of the command line, as in:

只需将变量定义移动到命令行的开头,如下所示:

variable=pluto; vi john/pic/$variable/NASA/rover.txt

or even:

甚至:

variable=pluto && vi john/pic/$variable/NASA/rover.txt

OBS:

OBS:

  1. notice you can't use $ while defining variables, only while using their values;
  2. 注意,只有在使用它们的值时,才能在定义变量时使用$;
  3. piping your vi command to a variable assignment does not make much sense, although you could achieve some clearer parameterization from:

    尽管你可以从以下方面获得更清晰的参数化,但是将vi命令输入到变量赋值没有多大意义。

    function opener() {
        vi john/pic/$1/NASA/rover.txt
    }
    $ opener "pluto"