In my daily work I often need to use a ssh connection on a device (which you can consider as read only), and the commands I write are long.

That's why I would like to load some alias just after ssh login. But when I try something as follow, it don't works:

ssh name@ipAdress "bash -l ; alias short='veryLongCommandThatIWriteOften'"

I guess that's because bash stop the processing of the other commands which are just after.

So is it possible to set aliases directly as an argument of bash, or is there another solution to do what I want?

有帮助吗?

解决方案

Instead of an alias, you can use a shell function, which bash allows you to export. This way, you first define the function, then export its name, and finally start a new interactive shell which inherits your function. For example:

ssh -t name@ipAddress "short () { veryLongCommandThatIWriteOften; }; export -f short; bash"

The -t is necessary to set up the pseudo terminal for the interactive bash shell, as ssh won't do it automatically for an apparently non-interactive command.

Note that you many need to be careful about quoting, depending on what the body of short is.

其他提示

Edit the file ~/.bashrc

nano ~/.bashrc

It has examples of how to set aliases also. Log back in to have new alias work.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top