0

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?

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255

1 Answers1

1

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:

https://stackoverflow.com/questions/6697753/difference-between-single-and-double-quotes-in-bash#6697781