I want to grep a file, and I want to get all the lines that have a certain environment variable (to be exact, $PWD).

Of course, using just

cat file | grep '/'$PWD'/'

is not working, since $PWD contains slashes.

I am trying to figure out how to do it correctly, but I come up only with weird and over-complicated solutions. What's some simple way to do this?

  • 3
    if you are looking for instance of $PWD inside a file, why add the leading and trailing / to search string? – iruvar Nov 22 '14 at 3:53
  • that....is a good question. I thought it's necessary to have the trailing /, apparently it's not. – Karel Bílek Nov 22 '14 at 3:57

Just use double quote instead of single quote, and you don't have to use cat (See UUOC):

grep -F -- "$PWD" file

And remember that without -F, $PWD would be treated as a regular expression as opposed to a string to be looked for in the file.

  • 1
    It works, but only with removed slashes; at which point I don't need to use the quotes at all. – Karel Bílek Nov 22 '14 at 3:58
  • 2
    @KarelBílek: General, you don't. But using quotes will protect you if your variable contain special character. And you should always use double quotes when you don't want word splitting occurs. – cuonglm Nov 22 '14 at 4:02
  • With Stéphane's edit, this answer is the better one. Thanks to both – Karel Bílek Nov 22 '14 at 20:38
  • Why do you need to use -- ? – CodeGodie Aug 2 '18 at 19:42
  • @CodeGodie because PWD can be set to anything – cuonglm Aug 3 '18 at 1:55

Since forward slash is not a special character, you can use it freely:

grep "$PWD/" file

Adding the tailing / will not cause any problem. However forward slash is a special character in awk and sed. Patterns are enclosed by / /. In this situation, you can use

awk -v pattern="$PWD/" '$0 ~ pattern' file

to avoid the awk /pattern/ {action} grammar. Here "~" is the match operator, $0 means the whole line.

  • 3
    It doesn't make sense for $PWD to be treated as a regex here. So, that should be grep -F -- "$PWD/" file or awk 'index($0, ENVIRON["PWD"])' – Stéphane Chazelas Nov 22 '14 at 9:06
  • I have to say grep -F runs faster and works in most cases, but if the dir names contains some special characters, eg: current dir is foo\nbar. grep -F "$PWD" will match the line containing foo or bar. I have tested it with (1) mkdir "echo -e "foo\nbar"", (2) create a file with four lines: 1. foo, 2.abc, 3. foo, 4 abc (3) grep -F "$PWD" returns line one and line three. – Jianyi CHEN Nov 22 '14 at 15:35
  • That's true with or without -F. grep -F -- "$PWD" file returns all the lines in file that contain any of the lines in $PWD. grep -- "$PWD" file return all the lines in file that match any of the regexps in $PWD (newline separated). – Stéphane Chazelas Nov 22 '14 at 20:11
  • @Stéphane Chazelas You are right. Missing -F option brings a lot of problems, esp. when dir names have + ? { etc chars. – Jianyi CHEN Nov 23 '14 at 12:56

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.