2

I'm working on a system with a lot of tcsh configuration scripts, requiring me to run most programs through tcsh. I've attempted to make this easy for myself by adding this to my ~/.zshrc:

# run command in tcsh
function t() {
    tcsh -c "$@"
}

This works for something like t ls, but fails for t ls -l, which gives the error Unknown option: `-l' Usage: tcsh ..., and is clearly passing -l as an argument to tcsh, not to ls.

How can I quote the string passed in $@?

Eric
  • 95,302
  • 53
  • 242
  • 374

3 Answers3

5

Zsh has a special option for this (not bash): ${(q)}:

tcsh -c "${(j. .)${(q)@}}"

. First (${(q)@}) escapes all characters in the $@ array items that have special meaning, second (${(j. .)…}) joins the array into one string.

ZyX
  • 52,536
  • 7
  • 114
  • 135
2

This seems to work

function t {
  tcsh -c "$*"
}

and is a whole lot shorter than what you found in the other answer ;-)

[edit:]

ok, if you really want to get perverse with quotes... give up the function and just use an alias (which is probably a better idea anyway)

alias t='tcsh -c'

[edit2:] Here is a good and to the point discussion of the different ways to quote parameters in Zsh http://zshwiki.org/home/scripting/args

Francisco
  • 3,980
  • 1
  • 23
  • 27
0

This answer had what I needed:

# run command in tcsh
function t() {
    C=''
    for i in "$@"; do
        C="$C \"${i//\"/\\\"}\""
    done;
    tcsh -c "$C"
}
Community
  • 1
  • 1
Eric
  • 95,302
  • 53
  • 242
  • 374