.cshrc quoting in complex alias

25 Views Asked by At

The following line works in tcsh shell when entered directly:

df -P . | awk -F: 'NR==2 {print "//"$1}' | awk '{printf "%s/%s\n", $0, ENVIRON["PWD"]}' | awk '{gsub("/home/", ""); gsub("/", "\\"); print}'

But when I try to use in in .cshrc to create an alias:

alias foo " df -P . | awk -F: 'NR==2 {print "//"$1}' | awk '{printf "%s/%s\n", $0, ENVIRON["PWD"]}' | awk '{gsub("/home/", ""); gsub("/", "\\"); print}' "

the outermost double quotes seem to be breaking things:

> foo
awk: cmd. line:1: {printf %s/%sn, /usr/bin/tcsh, ENVIRON[PWD]}
awk: cmd. line:1:         ^ syntax error
awk: cmd. line:1: {gsub(/home/, ); gsub(/, \); print}
awk: cmd. line:1:               ^ syntax error
awk: cmd. line:1: {gsub(/home/, ); gsub(/, \); print}
awk: cmd. line:1:               ^ 0 is invalid as number of arguments for gsub

I have tried multiple combinations of quotes (following info here) with no success. What am I missing?

1

There are 1 best solutions below

0
Nan Nanco On

Figured out this quoting abomination:

alias foo "df -P . | awk -F: 'NR==2 {print "\$"1}'| awk '{printf "\""//%s/%s\n"\"", "\$"0, ENVIRON["\""PWD"\""]}' | awk '{gsub("\""/home/"\"", "\"""\""); gsub("\""/"\"", "\""\\"\""); print}' "

The key was to replace:

$ -> "\$" 
within single-quoted awk: " -> "\""