1

I am trying to execute the following, with no success getting either the 'py3start' or the 'py3test' alias commands recognized - (I introduced the 'py3test' for testing purposes to check if setting the alias explicitly, just prior to using it would make any difference):

command = "echo $SHELL; py3start; alias py3test='source ~/.venv/python3/bin/activate'; py3test"
stdout, stderr, status = connection.exec_command(command=command, timeout=timeout)

Please see the output below: Command stdout as obtained from a debug session.

Please could someone help me figure out why the aliases are not being recognized even though the shell used is /bin/bash, the aliases are defined as below in both the ~/.bashrc and ~/.bash_profile files AND, the output of 'alias -p' includes the said aliases through the same paramiko session (refer last screenshot).

These are the contents of the ~/.bashrc and ~/.bash_profile files on the target VM - where I'm setting the alias py3start.

[root@VM ~]# cat ~/.bashrc 
# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias py3start='source ~/.venv/python3/bin/activate'


# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
[root@VM ~]# cat ~/.bash_profile 
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

alias py3start='source ~/.venv/python3/bin/activate'

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PATH

[root@VM ~]#

Please see output below when 'alias -p' is included in the command - clearly, it finds the aliases here, but still does not find them when I try to use them:

command = "echo $SHELL; py3start; alias py3test='source ~/.venv/python3/bin/activate'; alias -p; py3test"
stdout, stderr, status = connection.exec_command(command=command, timeout=timeout)


stderr = {str} 'bash: py3start: command not found\nbash: py3test: command not found\n'

stdout: Command stdout obtained from debug session.

aeon
  • 83
  • 6
  • I suspect the problem is because the remote shell is not considered “interactive” so .bashrc is not sourced (by default). Much more info here: https://unix.stackexchange.com/questions/257571/why-does-bashrc-check-whether-the-current-shell-is-interactive – Jeff Y May 19 '23 at 18:24
  • @JeffY - the ~/.bashrc file was being correctly sourced in the paramiko session initiation (by default), without me having to explicitly give prepend 'source ~/.bashrc' to the commands list. What was missing was enabling the alias expansion using 'shopt -s expand_aliases' as pointed out in the marked solution below. – aeon May 19 '23 at 19:05
  • See also [Some Unix commands fail with " not found", when executed using Python Paramiko exec_command](https://stackoverflow.com/q/55419330/850848). – Martin Prikryl May 20 '23 at 16:31

1 Answers1

2

Aliases are only expanded in interactive shells by default. Either execute an interactive shell, or more simply in this case, explicitly enable alias expansion using shopt -s expand_aliases.

chepner
  • 497,756
  • 71
  • 530
  • 681
  • Yes, this is what was missing - when added to the end of the ~/.bashrc file, it tooke care of recognizing the alias commands. Thank you! – aeon May 19 '23 at 19:01