0

I have a script (./lookup) that will search a file ($c). The file will contain a list of cities. What I would like to do be able to search the file for what the user enters as an argument (./lookup Miami). For example; I can make the script return what I want if it is a single word city (Miami), but I can't figure out a way to make it work for 2 or more words (Los Angeles). I can get the single strings to return what I want with the following.

grep $1 $c

I was thinking about a loop, but I am not sure on how to do that as I am new to scripting and Linux. Thanks for any help.

1 Answer 1

1

Whenever arguments could possibly contain spaces, proper quoting is essential in Bash:

grep "$1" "$c"

The user will need to say ./lookup "Los Angeles". If you don't like that, you can try:

grep "$*" "$c"

Then all arguments to the script will be passed together as one string to grep.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much! This makes sense. I was under the impression that by quoting $*, it would take it literally. Thanks for the solution!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.