簡體   English   中英

我如何在bash函數的字符串中使用參數

[英]How can I use an argument in a string in a bash function

我嘗試:

ctests() {
    curl -X POST \
        http://route.to.host/cucumber/execute-tests \
        -H 'Authorization: Basic xxxxxxxxxxxxxxxxxxxxx' \
        -H 'Content-Type: application/json' \
        -H 'cache-control: no-cache' \
        -d '{ "text": "cucumber! alltests products=$1" }'
}

並希望這樣稱呼

> ctests someproduct

但是1美元不會解決。 我嘗試了$ {1},但還是一樣。 有一個好的解決方案嗎?

$1不能解析,因為您使用的是單行號' ,它禁止變量解析。

請改用雙引號( " )(您必須在雙引號內轉義雙引號;或在雙引號內使用單引號;具體取決於您的上下文)

ctests() {
    curl -X POST \
        http://route.to.host/cucumber/execute-tests \
        -H 'Authorization: Basic xxxxxxxxxxxxxxxxxxxxx' \
        -H 'Content-Type: application/json' \
        -H 'cache-control: no-cache' \
        -d "{ \"text\": \"cucumber! alltests products=$1\" }"
}

引用bash(1)

報價

[...]

將字符括在單引號中可保留引號內每個字符的字面值。 即使在單引號之前加反斜杠,也不能在單引號之間引起單引號。

用雙引號引起來的字符保留引號內所有字符的文字值,但$ [...]除外

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM