I have a python script to translate text via command line, the usage is script.py "Hola Mundo" and the output will be Hello World.

Now I want to send text to this program via a keyboard shortcut in Xubuntu 18.04.1 XFCE, so I've written a bash script translate.sh and put it together with script.py into /usr/local/bin, and assigned a keyboard shortcut with XFCE keyboard utility to it. However, everything inside the bash script worked, except the line which calls Python.

If I call the script via terminal, it will work flawlessly. I am still very new to bash, and don't know how to get the error message of Python (if any) printed out, when I'm calling the bash script via XFCE keyboard shortcut.

Anyone have an idea what is happening and what the problem is?

My bash script translate.sh:

#!/usr/bin/env bash

TEXT=`xsel -o`
RESULT=`python /usr/local/bin/script.py "$TEXT"`
# or RESULT=`script.py "$TEXT"` (I've put shebang in the python script too) 
# neither worked with keyboard shortcut, both worked in terminal
echo $RESULT # for debugging in terminal

notify-send --icon=info "$TEXT" "$RESULT"

Note that if I run this script in the terminal, everything works - the notification pops up, and I can see the result echoed in the terminal too. When calling this script with keyboard shortcut, the popup appears with the selected text as the notification title, but there's no body. (Proving only the python line didn't work.)

The shebang line in the Python script is #!/usr/bin/env python.

  • 1
    Using backticks is deprecated. Use "$(command)" instead of backticks to get the output of a command stored as a variable. – Thomas Ward Aug 16 '18 at 16:26

I'm not sure of the root cause, but to debug the script, you can use exec to redirect all of the output to a file. E.g. exec 1> ~/output.txt 2>&1

By the way, it's preferable to use the newer $() syntax instead of backticks ``, always use lowercase variable names (to avoid accidentally overwriting an environment/shell variable), and always quote any expansions (like echo "$RESULT").

So in sum:

#!/usr/bin/env bash

exec 1> ~/output.txt 2>&1  # For debugging

text="$(xsel -o)"
result="$(script.py "$text")"

echo "$result"  # For debugging in terminal
notify-send --icon=info "$text" "$result"
  • Thank you for pointing out how to debug and other shell tips! – jexplorer Aug 17 '18 at 6:01
  • @jexplorer Happy to help! – wjandrea Aug 17 '18 at 13:07

I found out the reason why it's not working, thanks to the debugging tips from @wjandrea.

There were Python modules missing, because the Python interpreter/environment being used is /usr/bin/python, instead of the Anaconda version with which I installed the dependencies. It seems that calling the python script with XFCE keyboard shortcuts doesn't respect the $PATH variable set up inside .bashrc file (because we are not running the shell interactively).

The solution is simply to specify the right Python interpreter to be used in the shebang line inside the Python script. Mine looks like #!/home/jexplorer/anaconda3/bin/python.

Or, alternatively and preferably, set up the $PATH variable inside .profile file instead of .bashrc.

  • 1
    Is there a reason you have the PATH set up in your .bashrc instead of in your .profile? If not, then maybe the better solution is to move the PATH over, log out then back in, and use the original shebang. – wjandrea Aug 17 '18 at 13:05
  • I didn't know about .profile and have always used .bashrc! Thanks again for the tips! – jexplorer Aug 17 '18 at 14:09

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.