如何将位置参数与"bash -c"一起使用?命令? [英] How to use positional parameters with "bash -c" command?

查看:123
本文介绍了如何将位置参数与"bash -c"一起使用?命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道使用命令bash -c时使用位置参数的最佳方法.

I'd like to know the best way of using positional parameters when using the command bash -c.

手册页指示-c选项:

如果command_string后面有参数,则将其分配给位置参数,从$ 0开始.

If there are arguments after the command_string, they are assigned to the positional parameters, starting with $0.

所以我想下面的形式是正确的:

So I guess that the following form is the right one:

$ bash -c 'printf "%s %s %s\n" $0 $1 $2' param1 param2 param3
param1 param2 param3

但是我看到了以下表格,我想知道为什么会更好

However I saw the following form, and I wonder why it would be better

$ bash -c 'printf "%s %s %s\n" $1 $2' _ param1 param2
param1 param2 

在这种情况下,字符_代替$0.

In this case, the character _ replaces $0.

我知道对于许多解释器$0都有特殊含义(命令名称,所有位置参数...),但是在这种情况下,手册页非常清楚.

I know that for many interpreters $0 has a special meaning (command name, all positional parameters...), but in this case the man pages are quite clear.

那么,第一种形式有什么问题吗?使用$0作为bash -c命令的第一个位置参数是否有任何缺点?

So, is there something wrong with the first form and is there any drawback of using $0 as the first positional parameter for the bash -c command?

推荐答案

bash -c 'printf "%s %s %s\n" $0 $1 $2' param1 param2 param3

以上方法有效,但许多人会认为这是一个坏习惯.如果要将代码从-c字符串复制到脚本,它将失败.同样,如果要将代码从脚本复制到-c字符串,则会失败.

The above works but many would consider it a bad habit to get into. If you were to copy code from the -c string to a script, it would fail. Similarly, if you were to copy code from a script to a -c string, it would fail.

相比之下,$1具有以下形式,它在-c字符串中的含义与在脚本或shell函数中的含义相同:

By contrast, with the following form, $1 means the same thing in the -c string that it would mean in a script or shell function:

bash -c 'printf "%s %s %s\n" $1 $2 $3' _ param1 param2 param3

编程风格的一致性减少了错误.

Consistency of programming style reduces bugs.

通常使用$@$*引用脚本的所有参数.请注意,这些变量包括$0:

One customarily refers to all of a script's arguments with $@ or $*. Note that these variables do not include $0:

$ bash -c 'echo "$*"' param1 param2 param3
param2 param3
$ bash -c 'echo "$@"' param1 param2 param3
param2 param3

$0是程序名称

在常规脚本中,$0是脚本的名称.因此,在使用bash -c时,某些人喜欢为$0参数使用一些有意义的名称,例如:

$0 is the program name

In regular scripts, $0 is the name of the script. Consequently, when using bash -c, some people prefer to use some meaningful name for the $0 parameter, such as:

bash -c 'printf "%s %s %s\n" $1 $2 $3' bash param1 param2 param3

或者:

bash -c 'printf "%s %s %s\n" $1 $2 $3' printer param1 param2 param3

如果-c字符串生成错误,则此方法具有明显的优势.例如,考虑以下脚本:

This approach has a clear advantage if the -c string generates an error. For example, consider this script:

$ cat script.sh
#!/bin/bash
bash -c 'grepp misspelling "$1"' BadPgm file.txt

如果运行脚本,将产生以下输出:

If we run the script, the following output is produced:

$ ./script.sh 
BadPgm: grepp: command not found

这将错误源标识为bash -c字符串中的命令.

This identifies the source of the error as the command in the bash -c string.

这篇关于如何将位置参数与"bash -c"一起使用?命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆