Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escaping double quotes with tcsh alias

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.

like image 891
Anton Avatar asked Dec 20 '08 01:12

Anton


2 Answers

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

like image 182
phi Avatar answered Oct 09 '22 07:10

phi


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
like image 32
Gurn Avatar answered Oct 09 '22 07:10

Gurn