简体   繁体   中英

Can I send a string to grep with commands and file names in a bash script?

Is it possible to send an array variable from the command line,

(where argsGrep="$@" and the command line input is something to the extent of -i Something) to a grep command

eg

result=$(grep $argsGrep ./file)

When $argsGrep has only the term to be searched, it works just fine, but the moment it contains more than the text and has a grep command, I can't get it to work whatsoever.

Don't use the intermediate string. It will just break things.

Just expand "$@" at the point you need it.

If you must save the contents of "$@" for some reason then you must use another array.

argsarr=("$@")

result=$(grep "${argsarr[@]}" ./file)

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