I am trying to execute a Bash command from inside of a Jupyter Notebook that writes a random number to some files. The command I'd like to issue is
echo $(( RANDOM )) &> {output_files[i]}
where output_files is a Python list of output filenames. I can't figure out how to put the Python variable output_files[i] into a Bash command successfully, so instead I create the entire command as a string and try to execute that through the Notebook:
# Create a list of 5 filenames
output_files = [os.path.join(os.getcwd(), 'random-%s.txt' % i) for i in range (5)]
# Write random number to each file
for i in range (5):
command = "echo $(( RANDOM )) &> " + output_files[i]
print(command)
!{command}
When I try this, the files ./random-0.txt etc. are created, but no number is written to them - they are empty. When I run the created commands directly in the terminal, though, it works exactly as expected. The files are created and contain a single random number each. The print(command) line produces
echo $(( RANDOM )) &> /filepath/random-0.txt
0
echo $(( RANDOM )) &> /filepath/random-1.txt
echo $(( RANDOM )) &> /filepath/random-2.txt
echo $(( RANDOM )) &> /filepath/random-3.txt
echo $(( RANDOM )) &> /filepath/random-4.txt
Web searches for this problem give hits for several similar questions on Stack Overflow as well as blogs, but the result of all of them is either "Bash commands don't exist on Windows" or "You can use '!' to execute Bash commands in Jupyter Notebooks!" with no further information about how it works so that I could debug an issue like this.
How do I make this work?
&>redirection is not adding anything useful over plain>and in fact, in the unlikely even that you actually get an error message,. you probably don't want it to overwrite your output file.