Reputation: 3332
I want to write a command opening a a Jupyter Notebook in firefox but I am struggling with bash.
# This is what I want to achieve:
$ firefox --new-tab localhost:8888/tree/notebooks/my_notebook.ipynb
So far I can echo the url to the notebook:
$ cd notebooks
$ var="my_notebook.ipynb"
$ echo localhost:8888/tree/${PWD}/${var} | sed 's/\/home\/johndoe\///g'
localhost:8888/tree/Dropbox/notebooks/my_notebook.ipynb
How to pass the string above to a firefox command? I tried to use command substitution $()
, piping |
, quotes, but without success.
I am assuming that jupyter-notebook is running from my home directory /home/johndoe
. Bonus points if you can pass $HOME
to that sed
command
SOLUTION: I created a function (not an alias) that opens Jupyter Notebook in firefox on a file or a directory as follows:
nb() {
firefox --new-tab "$(echo localhost:8888/tree/$(realpath --relative-to="$HOME" "${PWD}")/$1)" &
}
Upvotes: 0
Views: 439
Reputation: 140990
I tried to use command substitution $(), piping |, quotes, but without success.
Really? It's just:
firefox --new-tab "$(echo localhost:8888/tree/${PWD}/${var} | sed 's/\/home\/johndoe\///g')"
Bonus points if you can pass $HOME to that sed command
Use a different separator - with ~
you should be safe enough. And learn about quoting in shell.
sed "s~$HOME~~g"
You can get relative path with:
realpath --relative-to="$HOME" "$PWD"
Any idea how to place it in an alias using $1 instead of $var?
Use a function
some_function() {
if (($# == 0)); then
echo "Missing argument"
return 1
fi
firefox --new-tab "localhost:8888/tree/$(realpath --relative-to="$HOME")/$1"
}
Upvotes: 2