简体   繁体   English

如何将参数传递给shell脚本

[英]How to pass the arguments to the shell script

I want to pass the argument in the console while running a script called run.sh as follows 我想在运行名为run.sh的脚本时在控制台中传递参数,如下所示

run.sh first count run.sh第一次计数

for example: 例如:

./run.sh first 10

i have to pass those first and 10 to the script here 我必须将第一个和第10个传递给脚本

cat file1.txt | head -10

here first should refer to head and count value should be 10. how can i do this? 这里首先应该参考head和count值应该是10.我怎么能这样做?

Use Positional Parameters 使用位置参数

Bash supports positional parameters . Bash支持位置参数 Parameters one through nine are stored in $1 .. $9 , but you can have more stored in $ * or $@ . 参数1到9存储在$ 1 .. $ 9中 ,但您可以将更多存储在$ *或$ @中

For example: 例如:

#!/bin/bash

# Read x lines from some arbitrary file. 
head -n "$2" "$1"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM