Eric
Eric

Reputation: 97555

How can I forward bash/zsh arguments to another shell

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 $@?

Upvotes: 2

Views: 1403

Answers (3)

Francisco
Francisco

Reputation: 4110

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

Upvotes: 2

ZyX
ZyX

Reputation: 53604

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.

Upvotes: 5

Eric
Eric

Reputation: 97555

This answer had what I needed:

# run command in tcsh
function t() {
    C=''
    for i in "$@"; do
        C="$C \"${i//\"/\\\"}\""
    done;
    tcsh -c "$C"
}

Upvotes: 0

Related Questions