在第一个命令中忽略$

Moh*_*sen 5 bash terminal

如何$在第一个命令中编写Bash忽略的函数?我想要这个,因为有时当我从网上粘贴一个命令时,它包含一个$第一个字符.

$ ls应该做什么ls$ grep 'foo'应该做什么grep 'foo'.

这不起作用,因为$它不是有效的函数名称.

function $ {
   ...
} 
Run Code Online (Sandbox Code Playgroud)

Tho*_*mas 9

创建一个名为$somewhere 的文件$PATH,例如~/bin:

cd ~/bin
cat <<'EOF' > \$
#!/bin/sh
"$@"
EOF
Run Code Online (Sandbox Code Playgroud)

使其可执行:

chmod 755 \$
Run Code Online (Sandbox Code Playgroud)

并享受疯狂:

$ ls -l
total 92
-rwxr-xr-x 1 thomas thomas    13 Jan  3 19:09 $
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ ls
total 92
-rwxr-xr-x 1 thomas thomas    13 Jan  3 19:09 $
Run Code Online (Sandbox Code Playgroud)

  • +1好主意.你应该使用``$ @"`来正确处理带有空格的参数.我认为函数会比脚本更好,但是bash不允许`$`作为函数名. (2认同)