I want to use the script command to see my output both in the command line and also store it at the same time, as suggested here.

Now, I can do in my command line:

source activate foo
python my_file.py

Quite a hassle if I have to do this often, so I thought I would write a bash script that automatizes this. I tried to literally paste these commands into a command line file, but it would wait for input after script.

Instead, here's how I thought of implementing this in a bash script (such that I would just run the script and it would do it all:

script ../output.txt -c ' /home/foo/anaconda3/condabin/conda activate myenv3 && python myfile.py'

And this gives me a CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.

Why am I not using directly "source activate foo" or ". activate foo" as I would usually do? Well, if I try that, I get bash: activate: No such file or directory

I understand that /bin/bash apparently is not configured to do this in Ubuntu -- what should be my way forward?

  • I'm just guessing here...but in order to use conda, there are variables that have to be set in your ~/.bashrc (should be near the end of the file?). So, you need to source that first, before you run conda. When you start a new bash shell, it runs it for you.
    – Ray
    May 26, 2021 at 9:47
  • Why can't you just the commands you know work each time at the terminal in a script?
    – vanadium
    May 26, 2021 at 11:01
  • @vanadium I tried to copy&paste my commands into the bash script but it would just execute the first command ("script") and wait for further input.
    – FooBar
    May 26, 2021 at 11:28
  • @Ray Indeed, there are lines at the end of ~/.bashrc that include anaconda paths. So youre suggesting adding ". ~/.bashrc &&" at the beginning of the -c ' /home/.... sequence? Because I tried that and it didn't change the outpu
    – FooBar
    May 26, 2021 at 11:31
  • I know you need to source something, but how that fits into the script command that you're running is something that perhaps someone else can fill in for you -- I've never done that myself. Maybe try this...in your installation, there is a file. Mine is at: /opt/miniconda3/etc/profile.d/conda.sh (i.e., fill in the beginning for your system). Try sourcing this instead of .~/bashrc first?
    – Ray
    May 26, 2021 at 11:41

1 Answer 1


When you run commands with script -c, they are run in a noninteractive shell:

$ script ../output.txt -c 'echo "\$0 = $0 ; FLAGS = $-"'
Script started, file is ../output.txt
$0 = bash ; FLAGS = hBc
Script done, file is ../output.txt

The flags are different from the normal interactive shell (note the presence of the i flag):

$ echo "\$0 = $0 ; FLAGS = $-"
$0 = -bash ; FLAGS = himBHs

Although noninteractive, non-login shells read your ~/.bashrc, they likely processes it differently. In particular, the default ~/.bashrc that would have been copied from /etc/skel when your account was created starts with:

# If not running interactively, don't do anything
case $- in
    *i*) ;;
      *) return;;

meaning that any conda related content that you added at the end of your ~/.bashrc will not get executed (and sourcing ~/.bashrc again won't help).

I don't know much about conda, but assuming it is capable of being run in such a shell, then one possible workaround would be the following:

  • remove the conda related portion of your ~/.bashrc to a separate file

  • source that file from your ~/.bashrc to keep the existing behavior for your interactive shell

  • source that file (instead of ~/.bashrc) first in your script -c ' ... ' command

  • Fourth option: move the conda-related part from the end of ~/.bashrc to the beginning, before the part that you quoted which returns for non-interactive shell.
    – raj
    May 26, 2021 at 20:50
  • @raj indeed - although of course that may change the behavior of non-interactive shells in other contexts, which may be undesirable May 26, 2021 at 21:06

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.