So I want to pass 2 strings and filename from the command line and search for both the strings in the file. Sample input : script.sh file.txt AB 78
Sample output : 001,AB,cse,78

(where the file has this line among others that do not match)

My try:

`#/usr/bin/env bash`  
`grep '$2.*$3\|$3.*$2' $1`

However this doesn't work. Can anyone help?


You need to change you single quote into double quotes to allow the $2 and $3 to be expanded into your search string:

#/usr/bin/env bash
grep "$2.*$3\|$3.*$2" $1

A better explanation is here:


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.