0

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.

1
  • I'm not sure you need alias here. Function may work too. "py3test() {source ~/.venv/python3/bin/activate;}; py3test"
    – yarolig
    Commented May 20, 2023 at 7:41

1 Answer 1

2

There are at least two problems. Quoting man bash:

Aliases are not expanded when the shell is not interactive, unless the expand_aliases shell option is set using shopt

Bash always reads at least one complete line of input, and all lines that make up a compound command, before executing any of the commands on that line or the compound command. Aliases are expanded when a command is read, not when it is executed. Therefore, an alias definition appearing on the same line as another command does not take effect until the next line of input is read.

So you need both

shopt -s expand_aliases

and a newline after the alias py3test=...

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .