简体   繁体   中英

How can I pass command line argument

I want to pass a parameter in command and that need to execute.

For example:

read -s -p "ls -ltr " $1

Then I will pass parameter $1 -> abc* . It should execute and provide the files with abc*

read -s -p "ls -ltr " $1

read -s -p "ls -ltr " $1

You're almost there, you just can't read into a positional variable.

read -s -p "ls -ltr " mask
ls -ltr $mask

I don't understand why you're hiding the input, though?

PS: This won't work for filenames w/ eg spaces in them, or if you enter multiple space separated masks ...

您可以使用$ n访问传递的参数,其中n是参数编号-1,2,3,...。传递参数的方式就像使用其他任何命令一样。

$ cat myscript

#!/bin/bash

echo "First arg: $1"

echo "Second arg: $2"

$ ./myscript hello world

First arg: hello

Second arg: world

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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