14

I'm trying to create a bash alias, where the alias itself has a space in it.

The idea is that the alias (i.e. con) stands for sudo openvpn --config /path/to/my/openvpn/configs/. Which results in a readable command, when the con alias is used.

i.e: `con uk.conf` == `sudo openvpn --config /path/to/my/openvpn/configs/uk.conf`

I understand that I can't declare the alias like this: con ="sudo openvpn --config /path/to/my/openvpn/configs/". Would bash functions work in this scenario? I've never heard of that, but when researching a solution for this minor issue.

Mingye Wang
  • 1,181
boolean.is.null
  • 2,553
  • 7
  • 19
  • 24

1 Answers1

17

Yes, you will need to use a function. An alias would work if you wanted to add a parameter, any arguments given to aliases are passed as arguments to the aliased program but as separate parameters, not simply appended to what is there. To illustrate:

$ alias foo='echo bar'
$ foo
bar
$ foo baz
bar baz

As you can see, what was echoed was bar baz and not barbaz. Since you want to concatenate the value you pass to the existing parameter, you'll need something like:

function com(){ sudo openvpn --config /path/to/my/openvpn/configs/"$@"; }

Add the line above to your ~/.bashrc and you're ready to go.

terdon
  • 242,166