Here is the command syntax. The format "name:/jsmith/" is literal and cannot be changed.
search -l "name:/jsmith/"
search -l "name:/jsmi.*/"
I am trying to create a bash alias that will take a command line variable ($1) as input like below:
alias new_search='search -l "name:/$1/"'
So the new search command would simply be:
new_search jsmith
new_search jsmi.*
Obviously the alias above is incorrect.
I understand that escape character helps preserving (" and /) and I also searched online and made some progress but the syntax below still fails.
alias new_search='search -l \"name:\/$1\/\"'
Any suggestions? Thanks!
- Here is a reference I found: How to use an environment variable inside a quoted string in Bash
CodePudding user response:
A good rule of thumb is that if your alias requires an argument (and isn't just appending that argument to the alias, but doing something else with it) you should consider using a function. You can put this in your bashrc or whatever and source it the same as you would your alias(es).
new_search() {
search -l "name:/$1/"
}
You're correct about escaping quotes, but:
Your $1
won't expand because the outer quotes are single quotes, which don't allow for variable expansion, and:
Aliases just expand to the string given as the definition, they don't take positional parameters, so either way your $1
wouldn't be doing anything there.