Search code examples
bashshelltextcsh

How can I make an output of a command in alias to be evaluated when the alias is executed but not when defined?


I made a simple example for this question. I can do

ckim@stph45:~] echo 1 2 3 | awk '{print $2}'
2

I wanted to make an alias for this whole command but it gives em an error (I'm using csh)

ckim@stph45:~] alias tt 'echo 1 2 3 | awk '{print $2}''
Missing }.

How do I escape '? I tried

alias tt 'echo 1 2 3 | awk \'{print $2}\''
alias tt 'echo 1 2 3 | awk "'{print $2}'"'

But didn't work. => solved. Please 'ADD' below.

ADD : as l|L|l's commend below, I can pass it by `alias tt "echo 1 2 3 | awk '{print $2}'", but for real case below

ckim@stph45:~] alias ddcoc "ddd caffe-fast-rcnn/python/caffe/_caffe.so `ps aux | grep python | grep tools | awk '{print $2}'`"
ckim@stph45:~] ddcoc
ddd: No match.
ckim@stph45:~] alias ddcoc
ddd caffe-fast-rcnn/python/caffe/_caffe.so ckim     29216  0.0  0.0  52596  8968 pts/8    Ss+  11:36   0:00 /home/ckim/anaconda2/bin/python -t /usr/local/bin/pydb tools/train_net_e2e.py --gpu 0 --solver models/coco/ZF/faster_rcnn_end2end/solver.prototxt --weights data/imagenet_models/ZF.v2.caffemodel --imdb coco_2014_train --iters 490000 --cfg experiments/cfgs/faster_rcnn_end2end.yml --set RNG_SEED 42 TRAIN.SCALES [400,500,600,700]

The problem is ps ... is substituted at the time alias is being defined. What I want is evaluate ps ... at the time I execute the alias. ddd is an application I can run on csh but in this alias doesn't work. How can I solve it?


Solution

  • Do you have to use an alias? I would simply transform the alias to a script or a shell function.

    This would then look like (at least for bash/zsh)

    function ddcoc()
    {
        ddd <some absolute path>/caffe-fast-rcnn/python/caffe/_caffe.so $(ps aux | grep python | grep tools | awk '{print $2}')
    }