Mohsen
Mohsen

Reputation: 65785

Ignore $ in first of commands

How can I write a function that Bash ignores $ in first of commands? I want this because sometimes when I paste a command from web it includes a $ in first character.

$ ls should do ls or $ grep 'foo' should do grep 'foo'.

This will not work because $ is not a valid function name.

function $ {
   ...
} 

Upvotes: 5

Views: 93

Answers (1)

Thomas
Thomas

Reputation: 181735

Create a file called $ somewhere on your $PATH, e.g. in ~/bin:

cd ~/bin
cat <<'EOF' > \$
#!/bin/sh
"$@"
EOF

Make it executable:

chmod 755 \$

And enjoy the madness:

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

Upvotes: 9

Related Questions