Pablo Jomer
Pablo Jomer

Reputation: 10418

escaping $ in csh

How can I escape $ in csh? inside <double-quotes> (""). I'm trying to do this but can't escape the <dollar> sign ($)

alias e "echo \\!:2-$"

This works but is not enough for my needs

alias e echo \\!:2-$

Upvotes: 3

Views: 12649

Answers (3)

Leslie Krause
Leslie Krause

Reputation: 467

If you really need to use a $ character inside a double quoted string, then try setting a variable (ideally in your ~/.tcshrc file) to the character.

For example, here's an alias that creates sequentially numbered backup files in the format "filename.old24.ext" given the command bak filename ext:

set DOLLAR='$'
alias bak '/bin/ls -1v \!:1.old*.\!:2 | tail -n 1 | awk -F"." "{printf substr(${DOLLAR}2,4)+1}" | xargs -0 -ISEQ cp -ip \!:1.\!:2 \!:1.oldSEQ.\!:2'

Notice, the use of ${DOLLAR} in the string passed to awk. It will be replaced by an actual $ character when the alias is executed by the shell.

Upvotes: 1

Stilez
Stilez

Reputation: 570

The answers suggested to this old question aren't correct.

It can be done, in CSH and other shells, and regardless of the kind of quote, but you need to exit the double quotes first if using them, for some shells.

The following should work in (almost) all shells, so I use it for simplicity. I've used backquote to get the output of a command because it's simpler to follow and avoids needing to add extra "$" symbols (the more modern style). Note the different uses of "$", to actually get a variable and as a literal (inside and outside single quotes for ease).

This should work in most shells.

EXAMPLE COMMAND:

echo "My terminal is $term, first file found in /dev is </dev/`ls /dev | head -n 1`>, the
dollar symbol is '"`echo '$'`"', and a newspaper costs "`echo '$'`"2.50."

OUTPUT:

My terminal is xterm, first file found in /dev is </dev/acpi>, the dollar symbol
is '$', and a newspaper costs $2.50.

Upvotes: 5

D.Shawley
D.Shawley

Reputation: 59583

Trying using single quotes instead - alias e 'echo \!:2-$'. You also need to backslash escape the history substitution as well.

Upvotes: 4

Related Questions