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
  • 70,465
Saad
  • 233
  • Do the second alias (ssh-aad) work for you? Because I think it has a flaw. – NotFromBrooklyn Dec 13 '12 at 09:15
  • yes it does work for me can you tell me what is the flaw that you find – Saad Dec 13 '12 at 12:21
  • alias name='commands' something The something part shouldn't work. – NotFromBrooklyn Dec 13 '12 at 13:19
  • but its a simple ssh command all I am doing is ssh saad@102.43.2.1 the ip address is just random – Saad Dec 13 '12 at 14:29
  • I had understood that you can't pass a argument outside the quotes. – NotFromBrooklyn Dec 13 '12 at 19:35
  • no I am not passing any arguments I just didnt want to share my ip address thats why i wrote it in that way. – Saad Dec 14 '12 at 05:54
  • @Zanna. I am not, myself, inclined to use code markup for error messages. I find blockquote markup more readable. However, if you are going to use code markup, might I suggest that you switch off syntax highlighting? <!-- language: lang-none --> – TRiG Mar 22 '18 at 21:48
  • @TRiG good point about the syntax highlighting, oops. I disagree about the error output in general, since it's all ruined if there's more than 1 line and the output is likely to have links and file paths, better to put it all in code (if it's from a terminal). Output from APT commands etc is unreadable when blockquoted – Zanna Mar 23 '18 at 06:13

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
  • 70,465
Saad
  • 233
  • Having ~/.bash_login prevents ~/.profile being sourced, not ~/.bashrc. You definitely did need to uncomment those lines in ~/.bashrc though. Typing bash opens a new Bash shell which sources ~/.bashrc. The original shell (started when you opened a terminal) would have done so too, unless it was not a Bash shell. – Zanna Mar 23 '18 at 06:20
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.

  • The "more general thing" is actually xdg-open(1), which is supposed to work on every XDG-compatible desktop environment. – Andrea Corbellini Dec 13 '12 at 19:20
  • open is not the main problem here I had solved it by using another command which is sensible-browser. My main problem is that ~/.bash_aliases is not getting loaded automatically when I log into the shell. I have to use this command $ source ~/.bash_aliases, even though the script ( which is in ~/.bashrc) for automatically loading this file when the shell starts is uncommented. – Saad Dec 14 '12 at 05:58
  • Andrea Corbellini: I agree, but gnome-open seems to sometimes to a better job of hooking into gnome preferences. But as you might notice I do hook into it if gnome-open isn't available. – quodlibetor Dec 14 '12 at 15:23
  • Saad: have you tried "$HOME/.bash_aliases" instead of ~/.bash_aliases? Also, have you tried just doing if [ -f "$HOME/.bash_aliases" ] ; then echo yes ; else echo no ; fi? – quodlibetor Dec 14 '12 at 15:27