Bash,参数列表段 [英] Bash, argument list segment

查看:38
本文介绍了Bash,参数列表段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果你在 python 中有一个列表,并且你希望从 2 到 n 的元素可以做一些像

If you have a list in python, and you want the elements from 2 to n can do something nice like

list[2:]

我想在 Bash 中使用类似于 argv 的东西.我想将所有元素从 $2 传递给 argc 给一个命令.我目前有

I'd like to something similar with argv in Bash. I want to pass all the elements from $2 to argc to a command. I currently have

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

但这不够优雅.会是正确"的方式吗?

but this is less than elegant. Would would be the "proper" way?

推荐答案

你也可以切片",$@ 获取 bash 中的所有参数.

you can do "slicing" as well, $@ gets all the arguments in bash.

echo "${@:2}"

从第二个参数开始

例如

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

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

这篇关于Bash,参数列表段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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