LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-22-2017, 12:48 PM   #1
taylorkh
Senior Member
 
Registered: Jul 2006
Location: North Carolina
Distribution: CentOS 6, CentOS 7 (with Mate), Ubuntu 16.04 Mate
Posts: 1,965

Rep: Reputation: 159Reputation: 159
Wrap quotes around string in a bash script


This should not be this hard. Perhaps if I stand on my head

I am sorting out some duplicates in my movie collection. I can search my inventory thus
Code:
[ken@vmCentOS7Mate Desktop]$ grep -i 'pink horse' myflicks.txt 
Ride The Pink Horse                               1947 noir avi
To make for a little less typing I wish to create a script which will accept the search string and then do the search. Should not be difficult.
Code:
#!/bin/bash
read check4
clear
grep -i $check4 myflicks.txt
exit
This will of course work on single words. If I want to search on a multiple word string it must be wrapped in quotes. I have attempted to concatenate quotes to the string thus
Code:
check4="'"$check4"'"
and several other permutations such as wrapping quotes in quotes, escaping the single quotes etc. No luck. I am obviously overlooking something. Any suggestions?

TIA,

Ken
 
Old 12-22-2017, 12:58 PM   #2
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,293

Rep: Reputation: 595Reputation: 595Reputation: 595Reputation: 595Reputation: 595Reputation: 595
As always in scripts, variables in command arguments should be in "quotes"!
Code:
grep -i "$check4" myflicks.txt
 
1 members found this post helpful.
Old 12-22-2017, 01:16 PM   #3
taylorkh
Senior Member
 
Registered: Jul 2006
Location: North Carolina
Distribution: CentOS 6, CentOS 7 (with Mate), Ubuntu 16.04 Mate
Posts: 1,965

Original Poster
Rep: Reputation: 159Reputation: 159
Thanks MadeInGermany,

That did the trick although I am not quite sure why. I guess it causing grep to treat whatever is in the variable as a single "chunk." If I echo $check4 and echo "check4" I see the same thing. The script must interpret it differently. Bottom line, I am good to go.

Many thanks,

Ken
 
Old 12-22-2017, 04:46 PM   #4
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,293

Rep: Reputation: 595Reputation: 595Reputation: 595Reputation: 595Reputation: 595Reputation: 595
It is the shell that first does variable expansion then word splitting then globbing (replacing wildcards with matching filenames).
The result is passed to grep as arguments.
The "quotes" let the shell only expand variables.
The quotes are removed also; grep sees the string without the quotes.

Last edited by MadeInGermany; 12-22-2017 at 04:49 PM.
 
Old 12-23-2017, 03:41 PM   #5
taylorkh
Senior Member
 
Registered: Jul 2006
Location: North Carolina
Distribution: CentOS 6, CentOS 7 (with Mate), Ubuntu 16.04 Mate
Posts: 1,965

Original Poster
Rep: Reputation: 159Reputation: 159
Amazing!

I need to teach my mind to think like a shell (or perhaps find a shell which thinks like my mind). Or perhaps not

Thanks again,

Ken
 
Old 12-24-2017, 10:37 AM   #6
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: Currently: openSUSE, Raspbian, Slackware. Formerly: CentOS, MacOS, Red Hat. Other: Solaris, Tru64
Posts: 1,735

Rep: Reputation: 213Reputation: 213Reputation: 213
Quote:
Originally Posted by MadeInGermany View Post
As always in scripts, variables in command arguments should be in "quotes"!
Code:
grep -i "$check4" myflicks.txt
Just make sure they're double quotes---single quotes have a different "gotcha" effect.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] BASH Script String Manipulation - not working in script. BW-userx Programming 14 05-22-2017 02:31 AM
[SOLVED] Silly mistake in lftp bash script probably quotes emmalg Linux - Software 3 02-08-2013 06:12 AM
Yet Another Bash Quotes Within Quotes Issue tboyer Linux - Software 17 11-03-2012 12:17 PM
[SOLVED] Using a long Bash command including single quotes and pipes in a Bash script antcore Linux - General 9 07-22-2009 12:10 PM
same find command not working in bash script, quotes? QuakerOatz Linux - Software 1 07-14-2003 01:04 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:25 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration