【问题标题】:How to get bash script to execute a command that is the arguments to the script?如何让 bash 脚本执行作为脚本参数的命令?
【发布时间】:2023-01-18 07:41:22
【问题描述】:

我想要一个 bash 脚本,将其命名为 args.sh,以执行作为脚本参数的命令。
特别是,我想要这个命令(注意多个空格):

$./args.sh echo 'foobar *0x0'

精确执行此命令:

echo 'foobar *0x0'

我试试这个args.sh

#!/bin/bash
set -x
$*

但它不起作用:

./args.sh echo 'foobar    *0x0'
+ echo foobar '*0x0'
foobar *0x0

- 单空白。
如何编写这样的 bash 脚本?

【问题讨论】:

    标签: bash


    【解决方案1】:
    "$@"
    

    这扩展到所有带有空格和完整引用的命令行参数。相比之下,$* 容易出现不必要的分词和通配,因为它没有被引用。

    【讨论】:

      猜你喜欢
      • 2021-05-02
      • 2011-10-16
      • 2014-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-04
      • 2018-07-23
      相关资源
      最近更新 更多