![无法在 cshell 中的别名中使用变量](https://linux22.com/image/903605/%E6%97%A0%E6%B3%95%E5%9C%A8%20cshell%20%E4%B8%AD%E7%9A%84%E5%88%AB%E5%90%8D%E4%B8%AD%E4%BD%BF%E7%94%A8%E5%8F%98%E9%87%8F.png)
这是一个示例直接程序。我正在使用 C-shell,想要一个针对此环境本身的解决方案。遵循此代码示例:
set FILENAME = "\!:2"
alias jo 'echo this is my \!:1 file and its name is $FILENAME'
在命令行上,我给出以下内容:
jo first sample.txt
我应该得到如下输出
this is my first file and its name is sample.txt
相反我得到了
this is my first file and its name is !:2
这里的问题是符号 \ 完全被消除了,我不知道该怎么做。如果我想让它接受参数,那就需要这样做。有人能帮忙吗?
答案1
为了获得所需的结果,您应该用双引号将别名的第二个参数括起来,以便正确解释变量文件名:
set FILENAME = "\!:2"
alias jo "echo this is my \!:1 file and its name is $FILENAME"
测试用例:
% jo first sample.txt
this is my first file and its name is sample.txt