簡體   English   中英

Bash,參數列表段

[英]Bash, argument list segment

如果你有一個python列表,你想要從2到n的元素可以做一些很好的事情

list[2:]

我想在Bash中使用與argv類似的東西。 我想將$ 2中的所有元素傳遞給argc到命令。 我現在有

command $2 $3 $4 $5 $6 $7 $8 $9

但這不是優雅的。 會是“適當”的方式嗎?

你也可以做“切片”, $@獲取bash中的所有參數。

echo "${@:2}"

獲得第二個參數

例如

$ cat shell.sh
#!/bin/bash
echo "${@:2}"

$ ./shell.sh 1 2 3 4
2 3 4

在某個地方存儲$1 ,然后shift並使用$@

script1.sh:

#!/bin/bash
echo $@

script2.sh:

#!/bin/bash
shift
echo $@

$ sh script1.sh 1 2 3 4
1 2 3 4 
$ sh script2.sh 1 2 3 4 
2 3 4

暫無
暫無

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

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