3

I am trying to create an alias and I have added this line in ~/.bash_aliases:

alias server-python='open http://localhost:8000 && python -m SimpleHTTPServer'

alias ssh-saad='ssh saad@<my-server>' <my-server> is replaced by the IP address of my server. So in my ~/.bashrc file these lines are uncommented

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

and in my ~/.profile:

# if running bash
if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
    . "$HOME/.bashrc"
    fi
fi

I believe that whenever I start the terminal my aliases should work. However, unless I run the command source ~/.bash_aliases it’s not working. Also, for the first server-python alias I am getting an error:

Couldn't get a file descriptor referring to the console

I have looked into these solutions here:
How to create a permanent "alias"?
Ubuntu alias not applied in bashrc

but still cannot make it work. I would really appreciate it if someone could point out to me what I am doing wrong. I know that the problem is very trivial, but I must be just missing something.


I have now fixed the error

Couldn't get a file descriptor referring to the console

by using sensible-browser instead of open:

alias server-python='sensible-browser http://localhost:8000 && python -m SimpleHTTPServer'
Zanna
  • 72,312
Saad
  • 233

2 Answers2

2

I finally found one suitable solution for this problem. if there is a ~/.bash_login file and its not empty then ~/.bashrc file is not automatically loaded when we open the shell. If we move that ~/.bash_login

mv ~/.bash_login ~/.bash_login_old

then ~/.bashrc file will be loaded and also it will load the ~/.bash_aliases file if the following lines are uncommented in the ~/.bashrc file.

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

One other solution I can think of is if you dont want to rename or delete your ~/.bash_login file then what you can do is when you are in the shell just type this command bash and it will load the ~/.bashrc file.

Zanna
  • 72,312
Saad
  • 233
0

"open" in ubuntu is /bin/open, described by open -h as "This utility help you to start a program on a new virtual terminal (VT)."

You the more general thing than sensible-browser is gnome-open, which is not installed by default (anymore?), and is provided by the libgnome2-bin:

$ sudo-apt-get install libgnome2-bin
$ gnome-open https://google.com    # opens https://google.com in default browser
$ gnome-open config.txt   # opens config.txt in gedit

I find gnome open so useful that I have the following in my (multi-site) bashrc:

if which gnome-open >/dev/null ; then
    alias o=gnome-open
elif which kde-open >/dev/null ; then
    alias o=kde-open
elif which xdg-open >/dev/null ; then
    alias o=xdg-open
fi

which would let you do:

alias server-python="o http://localhost:8000 && python -m SimpleHTTPServer"

and it would work most places.