Bash 别名无法通过 ssh 工作

mat*_*tst 3 bash ssh

我的 Bash 别名无法通过 ssh 运行,例如:

$ ssh remote_name ll dir_name
bash: ll: command not found
Run Code Online (Sandbox Code Playgroud)

Bash 手册页显示:

Aliases are not expanded when the shell is not interactive,
unless the expand_aliases shell option is set using shopt...
Run Code Online (Sandbox Code Playgroud)

所以我在本地和远程文件的文件shopt -s expand_aliases顶部添加了(因为我不确定需要哪个 - 远程对吧??)。~/.bashrc.bashrc

我重新启动本地 Bash 并ssh remote_name ll dir_name再次尝试,不幸的是我仍然遇到同样的错误bash: ll: command not found

谁能解释一下我应该做什么才能让它正常工作?

以防万一我的 Bash 版本是:

Local Bash:
$ bash --version 
GNU bash, version 4.3.11(1)-release (x86_64-pc-linux-gnu)

Remote Bash:
$ bash --version 
GNU bash, version 4.3.30(1)-release (x86_64-pc-linux-gnu)
Run Code Online (Sandbox Code Playgroud)

hee*_*ayl 5

~/.bashrc由 的非登录交互式会话读取bash,而不是由非交互式会话读取。

ssh remote some_command正在运行some_command于 的非交互式会话中bash,因此不会读取远程数据~/.bashrc(当然读取本地数据也是不可能的)。

准确地说,非交互式会话可以读取环境变量或(如果设置)bash定义的文件。BASH_ENVENV

如果您想坚持使用别名,也可以以交互模式打开 shell:

ssh remote bash -ic 'll'
Run Code Online (Sandbox Code Playgroud)

另请注意,别名是独立的,它们不接受任何参数,就像您提供目录名称一样。您需要使用函数将参数作为输入。类似的函数定义是:

ll_f () { ls -al --color=auto "$@" ;}
Run Code Online (Sandbox Code Playgroud)

现在你可以这样做:

ll_f /dir_name
Run Code Online (Sandbox Code Playgroud)

  • 注意:`.bashrc`_可能_被非交互式会话读取。`$ head -1 .bashrc echo RUNNING BASHRC $ ssh localhost echo hello RUNNING BASHRC 你好` (3认同)