I'm working in tcsh. How do I create an alias for:
nc stop `nc list | awk '/Running/{print $1}'`
I tried various variations of:
alias ncstop "nc stop `nc list | awk '/Running/{print $1}'`"
but none work
I'm working in tcsh. How do I create an alias for:
nc stop `nc list | awk '/Running/{print $1}'`
I tried various variations of:
alias ncstop "nc stop `nc list | awk '/Running/{print $1}'`"
but none work
You can change the quoting a bit to not allow the backticks to be evaluated until when the alias is used.
alias ncstop 'nc stop `nc list | awk '\''/Running/{print $1}'\''`'
This works because when the '
character is used for quoting, so that the special characters such as |
, $
and the backtick
are not interpreted by the shell. That allows those special characters to make it into the alias definition. But in the process of defining the alias the shell removes that outer layer of '
's. What actually ends up in the alias definition should be:
nc stop `nc list | awk '/Running/{print $1}'`
The '\''
construct can often use a little explaining. But in short it is three apostrophes. The first ends the previous quoted string, the next (which is escaped so as to not be interpreted by the shell) passes through, and the third starts another quoted string. So the '\''
construct allows an apostrophe (ie: '
or single quote
) to appear inside of a single quoted string.
Getting all of the quotes and escapes right in a string that needs to traverse multiple layers of interpretation gets messy and complicated very quickly. There was a comment on the question that recommended (for bash
) looking into implementing aliases as functions instead. I am pretty sure the same could be advised for tcsh
.