0

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!

qfgong
  • 3
  • 2
  • Assuming that `$1` is empty (which is nearly always the case if you use your alias in an interactive shell), `new_search jsmith` will be expanded to `search -l "name:" jsmith`. – user1934428 Nov 02 '22 at 07:12
  • That is true. As @Zac pointed out, it's better to construct a function if the alias needs to consume an argument. – qfgong Nov 02 '22 at 19:35
  • An `alias` is fine if - what you call an "argument" - is in the last position of the expanded command. In your case this does not work. With a function, you are more flexible, and you may consider never using an alias. – user1934428 Nov 03 '22 at 07:31

1 Answers1

0

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.

Zac Anger
  • 6,983
  • 2
  • 15
  • 42
  • +1 Thanks! Didn't think about that. The suggestion works. "new_search jsmith" But there is a slight complication which I forgot to mention in the question above. What if the input variable has a wildcard like "new_search jsmi.*" – qfgong Nov 02 '22 at 04:50
  • I believe you'll need to use `$@` rather than `$1`. I don't know what `search` is in this context, but with `ls` for example you can just use `$@`; or you may need to iterate over it as explained here: https://stackoverflow.com/a/5065321/5774952 – Zac Anger Nov 02 '22 at 04:50
  • Ty! Thought about trying "$@" for the same reason but not working out. I will keep searching. – qfgong Nov 02 '22 at 04:57
  • 1
    @qfgong If the input argument contains a wildcard and you don't want it to get expanded to a list of matching filenames, you need to quote or escape it (e.g. `new_search "jsmi.*"` or `new_search jsmi.\*`). You can sometimes get away with not quoting it if there are no matching filenames (and you're not running zsh, and haven't set nullglob, and...) but it's not safe or a good idea to omit the quotes. – Gordon Davisson Nov 02 '22 at 05:27