I want to set up a bash alias to grep all logs in a directory automatically; however, to make this user-friendly, I need to escape the periods and add a whitespace boundary so grep won’t match too many lines.
First I checked to be sure that I had the right syntax to escape an address…
[mpenning@sasmars daily]$ echo 1.1.1.1 | sed "s/\./\\\\./g"
1\.1\.1\.1
[mpenning@sasmars daily]$
Next I tried to escape a CLI argument… but it’s not quite getting me there…
[mpenning@sasmars daily]$ alias tryme='echo `sed "s/$argv[1]/\\\\./g"`'
[mpenning@sasmars daily]$ tryme 1.1.1.1
-> Indefinite hang until I hit cntlc
I realize that echo isn’t going to search, but this was a simple test.
What is the simplest way to escape periods in arguments to a bash alias?
What you want is a function, and you can use bash’s builtin replacement syntax:
This will avoid you from forking a
sedprocess.