How can the terminal in Jupyter automatically run bash instead of sh - jupyter

I love the terminal feature and works very well for our use case where I would like students to do some work directly from a terminal so they experience that environment. The shell that launches automatically is sh and does not pick up all of my bash defaults. I can type "bash" and everything works perfectly. How can I make "bash" the default?

Jupyter uses the environment variable $SHELL to decide which shell to launch. If you are running jupyter using init then this will be set to dash on Ubuntu systems. My solution is to export SHELL=/bin/bash in the script that launches jupyter.

I have tried the ultimate way of switching system-wide SHELL environment variable by adding the following line to the file /etc/environment:
SHELL=/bin/bash
This works on Ubuntu environment. Every now and then, the SHELL variable always points to /bin/bash instead of /bin/sh in Terminal after a reboot.
Also, setting up CRON job to launch jupyter notebook at system startup triggered the same issue on jupyter notebook's Terminal.
It turns out that I need to include variable setting and sourcing statements for Bash init file like ~/.bashrc in CRON job statement as follows via the command $ crontab -e :
#reboot source /home/USERNAME/.bashrc && \
export SHELL=/bin/bash && \
/SOMEWHERE/jupyter notebook --port=8888
In this way, I can log in the Ubuntu server via a remote web browser (http://server-ip-address:8888/) with opening jupyter notebook's Terminal default to Bash as same as local environment.

You can add this to your jupyter_notebook_config.py
c.NotebookApp.terminado_settings = {'shell_command': ['/bin/bash']}

With Jupyter running on Ubuntu 15.10, the Jupyter shell will default into /bin/sh which is a symlink to /bin/dash.
rm /bin/sh
ln -s /bin/bash /bin/sh
That fix got Jupyter terminal booting into bash for me.

Related

kubectl run - How to pass some commands to be executed before reaching the interactive terminal?

When using kubectl run -ti with an interactive terminal, I would like to be able to pass a few commands in the kubectl run command to be run before the interactive terminal comes up, commands like apt install zip for example. In this way, I do not need to wait for the interactive terminal to come up and then run those common commands. Is there a way do so this?
Thanks
You can use the shell's exec to hand control over from your initial "outer" bash, responsible for doing the initialization steps you want, over to a fresh one (fresh in the sense that it does not have -c and can optionally be a login shell) which runs after your pre-steps:
kubectl run sample -it --image=ubuntu:20.04 -- \
bash -c "apt update; apt install -y zip; exec bash -il"

Deleted Shell on WSL and Cannot Access WSL

During an attempt to remove zsh from Ubuntu running on WSL, I ran:
sudo apt-get --purge remove zsh
Foolishly, I did not reset my default shell, so I cannot access WSL. The windows flashes open and then closes.
I can see from powershell that all of my files in WSL are still available, but I do not know how to reset the default shell in WSL from Powershell.
How can I reinstall bash or zsh from Powershell?
So I figured this out. From an 'elevated' powershell I executed the following commands:
cd \wsl$<distro name>
wsl -e bash
sudo vipw
From there, I went to /etc/passwd and changed the shell on my username to bash.

I need to run a script every time I open WSL Ubuntu 18.04 on Windows 10

Every time I open WSL Ubuntu 18.04 on Windows 10 I want to run these settings automatically.
alias desktop='cd /mnt/c/Users/Dot/Desktop/ai_files'
export PYTHONPATH=${PYTHONPATH}:${HOME}/ai-safety-gridworlds
export DISPLAY=localhost:0.0
I tried making .sh script with the following content in /etc/init.d/ but it didn't work.
#!/bin/bash
alias desktop='cd /mnt/c/Users/Dot/Desktop/ai_files'
export PYTHONPATH=${PYTHONPATH}:${HOME}/ai-safety-gridworlds
export DISPLAY=localhost:0.0
To run these commands every time you open WSL, you will want to append the commands to .bashrc.
In bash, run
echo "alias desktop='cd /mnt/c/Users/Dot/Desktop/ai_files'" >> ~/.bashrc
echo "export PYTHONPATH=${PYTHONPATH}:${HOME}/ai-safety-gridworlds" >> ~/.bashrc
echo "export DISPLAY=localhost:0.0" >> ~/.bashrc
To create an environment variable which will be visible for all users on Ubuntu you can create a sh file in /etc/profile.d folder.
In example :
sudo vi /etc/profile.d/my_vars.sh && sudo chmod o+r /etc/profile.d/my_vars.sh
then include there your variables. For example:
export ORACLE_HOME="/opt/oracle/instantclient_11_2"
terminate and start wsl again. Variables should be accessible for all users.

Linux command works on terminal but not in ipython notebook

I am running pg_dump mydb > ~/Desktop/mydb.dump on my mac shell which works perfectly fine.
But when i use the same command on jupyter notebook with ! or %%bash, it doesn't work, saying that /bin/sh: pg_dump: command not found
I do not have any problem with running other commands on notebook.
What could be the issue?
Thanks in advance.
EDIT & ANSWER:
so, even though i am running jupyter notebook via terminal, interestingly the env variables are not the same it seems like.
i do not know why exactly but instead of pg_dump, if i write the whole path for pg_dump, which i added to ~/.bash_profile on terminal, it works on jupyter notebook as well. in my case it is
/Applications/Postgres.app/Contents/Versions/11/bin/pg_dump mydb > ~/Desktop/mydb.dump
You should change your $PATH variable when you use bash in ipython notebook
Like:
export PATH=$PATH:/usr/local/bin/pg_dump && pg_dump mydb > ~/Desktop/mydb.dump

Permanently set PGDATA environment variable Mac

To start my Postgres server I need to run these commands:
pg_ctl -D /usr/local/var/postgres start
export PGDATA='/usr/local/var/postgres'
Isn't there a way to define the PGDATA permanently and from there on only use commands like pg_ctl start to operate in the shell? Thanks.
You're half way there. The only thing you might want to do is to get PGDATA whenever you open the terminal by adding the export line to your shell config file. On my macOS machine i added the following line:
export PGDATA='/Users/john/.postgres' to ~/.bash_profile file. Don't forget to source it if you want to see the effect immediately by executing . ~/.bash_profile command. That's it from now on you can start your posters server by simply typing pg_ctl start. Hope it was worth the wait for the answer. :)
export will set the context for your current session. So we need to hack our terminal so that we will export this PGDATA env variable to every session.
If you are using zsh, you can simply execute the below command.
echo "export PGDATA='/usr/local/var/postgres'" >> ~/.zshrc
In case you are not using zsh replace ~/.zshrc in the above command with ~/.bashrc or ~/.profile
then, do source ~/.zshrc to see the immediate effect. You can instantly check using echo $PGDATA command.
Whenever you open a new terminal, your environment variable will be available by default.