Xargs Sh -c Skipping the First Argument

时间:2022-07-23
本文章向大家介绍Xargs Sh -c Skipping the First Argument,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

其实这个问题已经见过很多次了,但是知其然不知其所以然;今天偶尔在stackoverflow上看到了,记录一下;

shell中的arg1, arg2…

在bash shell中,1, 2代表arg1, arg2,比如

1 2 3

# echo hello world|xargs echo $1 $2 hello world

0 代表执行环境,如果是一个执行脚本的话,0 代表其脚本名;比如下面这个脚本hello.sh:

1 2 3 4 5

#!/bin/bash echo $0 echo $1 echo $2

执行:

1

# ./hello.sh arg1 arg2

会输出

1 2 3

./hello.sh arg1 arg2

xargs 调用sh -c 中的arg

但是使用xargs sh -c时会出现一个比较疑惑的情况,比如执行:

1 2 3

# echo hello world|xargs sh -c 'echo $1 $2' world

此时1代表world,2已经没有值了;而执行

1 2 3

# echo hello world|xargs sh -c 'echo $0 $1' hello world

反而得到了正确结果;

why

之前我一直认为xargs sh -c调用的时候吃掉了$0,不求甚解;偶尔读了一下sh的手册才发现玄机:

From the documentation for the -c option:

Read commands from the command_string operand. Set the value of special parameter 0 (see Special Parameters) from the value of the command_name operand and the positional parameters (1, 2, and so on) in sequence from the remaining argument operands.

就是说在上面这条命令中,其实是没有找到要执行的命令,或者说要执行的命令为空,而hello world作为1, 2传给一个空命令了;

后面追加一个dummy的命令会看的更清楚:

1 2 3

# echo hello world|xargs sh -c 'echo $1 $2' _ hello world

后面我加了一条下划线作为xargs的dummy command,这样1, 2就恢复正常了;

总结

虽然这是一个啥用也没有的Magic Topic,但是搞明白之后还是挺有意思的,娱乐用;

另外隐隐约约觉得这里面隐含着一些安全方面的问题,暂时只是一种感觉,将来需要留意有没有这方面的hack手段;

引用

https://stackoverflow.com/questions/41043163/xargs-sh-c-skipping-the-first-argument