I have a file called "physics". In bash, if I try

x="physics 1b"
grep "string" "$x".sh

grep complains:

grep:  physics 1b: No such file or directory.

However, when I do

grep "string" physics\ 

It works fine. So I guess the problem is something to do with the variable not being expanded to include the backslash that grep needs to recognize the space. How do I get this to work?

Using bash 3.2, mac os 10.6.

Never mind, the problem was that x was set to " physics 1b", but when I did echo $x to check the contents, bash chopped off the spaces in the front so I couldn't tell that it was different. The first way above actually works.

Was it helpful?


Put the entire argument in double-quotes:

grep "string" "$"


use this in your script.

grep "string" "${x}.sh"

another way

grep "string" "${x}.sh"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow