0

When I run this command in bash it works fine:

$ while sleep 1; do mysqladmin proc | grep tbl1; done

But, when I add it to my .bash_aliases or just run:

$ alias loop='while sleep 1; do $@; done'

It produces this error (using ls as an example):

$ loop ls
-bash: syntax error near unexpected token `ls'

My goal is to incorporate this loop command as an alias into my shell so I can use it more frequently. I've tried escaping it different ways, including double quotes, single quotes, ${$@} and more.

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
ctrlbrk
  • 1,174
  • 2
  • 17
  • 27
  • 1
    Thank you @that other guy -- I did try searching but couldn't find the answer for whatever reason. I tested your answer and it works fine! Hope this helps someone else. – ctrlbrk Apr 07 '20 at 21:47
  • No problem, it's hard to search. Check out [shellcheck](https://www.shellcheck.net) which automatically detects common issues including this one ("Aliases can't use positional parameters. Use a function.") – that other guy Apr 07 '20 at 22:10
  • Note: even with a function, you can't pass a pipeline, just a simple command. For example, `loop mysqladmin proc | grep tbl1` will run the equivalent of `while sleep 1; do mysqladmin proc; done | grep tbl1` (which probably works, at least in this case, but might not be what you expect). – Gordon Davisson Apr 07 '20 at 23:45

0 Answers0