7

I'm trying to run the following commands:

replace -x "must " A2input.txt
replace -x " a" -f -s ## A2input.txt
replace -x to -s ## -a A2input.txt
replace -x faith -f "unequivocal" A2input.txt

And it'd be nice if I could just alias it to something short and simple like "a", "b", "c", "d", etc...

However, some of those arguments have a quote, which is messing up the alias. Does anyone know how to actually escape the double quotes? I've tried things like '\"' and \" but nothing seems to work.

I'm using tcsh as my shell.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Anton
  • 1,387
  • 2
  • 17
  • 30

6 Answers6

9

The following all work in tcsh to accomplish various results:

alias t echo hello world                # you may not actually need any quotes
alias u 'echo "hello world"'            # nested quotes of different types
alias v echo\ \"hello\ world\"          # escape everything
alias w echo '\;'hello'";"' world       # quote/escape problem areas only
alias x 'echo \"hello world\"'          # single quote and escape for literal "
alias y "echo "\""hello world"\"        # unquote, escaped quote, quote ("\"")
alias z 'echo '\''hello world'\'        # same goes for single quotes ('\'')

To see how these are interpreted by the shell, run alias with no arguments:

% alias
t       (echo hello world)
u       echo "hello world"
v       echo "hello world"
w       (echo \;hello";" world)
x       echo \"hello world\"
y       echo "hello world"
z       echo 'hello world'

Anything in parentheses is run in a subshell. This would be bad if you're trying to set environment variables, but mostly irrelevant otherwise.

Finally, here's what the examples actually do:

% t; u; v; w; x; y; z
hello world
hello world
hello world
;hello; world
"hello world"
hello world
hello world
Gurn
  • 91
  • 1
  • 1
7

I got it to work by storing the string with the double quote in a variable with the string surrounded by single quotes. When I use the variable I up inside single quotes.
Example:

[11:~] phi% 
[11:~] phi% set text = 'a quote "'
[11:~] phi% alias ec echo '$text'
[11:~] phi% ec
a quote "
[11:~] phi% 
[11:~] phi% alias ec echo this has '$text'
[11:~] phi% ec
this has a quote "
[11:~] phi% 

I tested this with tcsh on OSX

phi
  • 1,513
  • 1
  • 15
  • 34
  • Very nice. I now use `set q = '""` in my alias setup scripts. I can usually figure out how to use a combination of `",',\,...` to properly quote, but using `${q}` is so much more readable! – David Ljung Madison Stellar Aug 21 '17 at 18:26
  • @DavidLjungMadisonStellar genius! – Stuart Mar 10 '23 at 18:37
  • @DavidLjungMadisonStellar Can you pls give an example of how you are using it in alias? It will help novices. – BluVio Jun 30 '23 at 04:17
  • 1
    Here's a simple example without aliases that will illustrate. We can do `echo 'Dave said, "Hi there"'` as a basic command. Then if we have `set q='"'` we can also do `echo "Dave said, ${q}Hi there${q}"` Note that we need to switch to double quotes instead of single quotes to evaluate the ${q} – David Ljung Madison Stellar Jul 01 '23 at 18:36
  • Also note that in my first comment I somehow screwed up and typed `set q = '""` which is incorrect, but I can't edit it so many years later. It should be single-quote,double-quote,single-quote. Basically a double quote that is captured inside of single quotes which tell the shell to use it as is without evaluating it as having any meaning to the shell. – David Ljung Madison Stellar Jul 01 '23 at 18:37
4

tcsh has a newer variable backslash_quote. Not sure when it was added but it is supported in 6.18.01 (version on OS X El Capitan) and 6.19 (latest stable release at time of writing). This makes it possible to escape ', ", and ` inside of quotation marks.

set backslash_quote

set sentence = 'I\'m a little teapot.'
set sentence2 = "The man said \"hello\""

If you don't want to use this option, your choices are limited to using a different type of quote around the symbol

"The man said "'"'"hello"'"'

or not using quotes at all and liberally backslashing things.

The\ man\ said\ \"hello\"
Greg Nisbet
  • 6,710
  • 3
  • 25
  • 65
2

It would appear that the reason that \" and \' don't work as you expect is that csh traditionally didn't support such syntax, so, if tcsh were to unconditionally support it, then such older scripts may not work properly anymore.

As mentioned in another answer, tcsh itself has the set backslash_quote feature; however, unlike implied in the mentioned answer, this is hardly a new feature, it's just a tcsh-specific one, and was actually added to tcsh at least some 27 years ago — the feature was first documented in the manual page at tcsh.man,v 3.8 1991/07/25 04:50:55, which hardly qualifies it as "new".

http://mdoc.su/n,f,d/tcsh.1

backslash_quote (+)
       If set, backslashes (`\') always quote `\', `'', and `"'.  This
       may  make complex quoting tasks easier, but it can cause syntax
       errors in csh(1) scripts.
cnst
  • 25,870
  • 6
  • 90
  • 122
0

If you can't get an alias to work, just write a short shell script, chmod +x, and put it somewhere in your $PATH (like $HOME/bin):

#!/bin/tcsh
replace -x "must" ...

I don't have any experience with tcsh, but with bash you do it like any of these:

alias t='echo "hello  world"'     # using single quotes to enclose entire string
alias t=echo\ \"hello\ \ world\"  # escape " and <space>
alias t="echo \"hello  world\""   # double-quote + escape inner double quotes

Maybe something similar will work in tcsh?

derobert
  • 49,731
  • 15
  • 94
  • 124
0

escape double quote in double quote - surround the double quote to escape with double quotes which are surrounded by single quote - '"'"'"'

if you can understand below commands, then you get it:

set var = "`echo '"'"'"'A'"'"'"''"'"'"'B C '"'"'"'`" ; echo $var
Bruce Wen
  • 61
  • 1
  • 4