21

So I have tried experimented and created an alias in .bashrc. However, when I test out the command I get:

[rkahil@netmon3 ~]$ menu
-bash: menu: command not found

Here is what I have in the .bashrc file:

# Source global definitions

if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

# User specific aliases and functions
alias menu='./menuScript.sh'
alias vi='vim'

The funny thing is when I created the alias vi, it worked. But menu does not. I have looked up previous posts on UnixStackExchange and attempted to follow other posts, but to no avail. Does anyone else have any suggestions?

ryekayo
  • 4,763
  • 1
    have you tried with alias menu='bash ./menuScript.sh' ? (I am not in a linux machine, can't test it) – Con7e Jul 24 '14 at 19:37
  • Where are you running 'menu' from? If it's not the same directory as 'menuScript.sh' then it will complain about a 'command not found'. Maybe you should use an absolute path? – garethTheRed Jul 24 '14 at 19:37
  • 1
    @garethTheRed It would say ./menuScript.sh: command not found – Michael Mrozek Jul 24 '14 at 19:46
  • @MichaelMrozek - fair one. I've just checked and it said No such file or directory, which is still not the same as the OP's error. The point still stands though - it would be best with an absolute path unless the alias is always only going to be used from the directory where menuScript.sh resides. – garethTheRed Jul 24 '14 at 19:51
  • .bashrc is in my pwd, i ran a locate and came up with: /home/rkahil/.bashrc – ryekayo Jul 24 '14 at 20:04
  • @Con7e I have done that and at first it did not work. This is how much of a clutz I am: I forgot to reset the terminal thinking that that would not have any effect and I was wrong. Please post that as an answer. – ryekayo Jul 24 '14 at 20:19
  • 1
    @ryekayo done :) – Con7e Jul 24 '14 at 20:22

5 Answers5

16

You should try with alias menu='bash ./menuScript.sh'. I am not currently on a Linux machine, so cannot test it myself, but it should work. When you call the alias, it doesn't know what to do with the path, so you must include the bash at the beginning.

And resetting the terminal does help after making the change.

Michael Mrozek
  • 93,103
  • 40
  • 240
  • 233
Con7e
  • 339
  • I don’t understand how the first paragraph is important – unless menuScript.sh has not been chmod’ed to executable, it which case the error message would have been “Permission denied”. I suspect that forcing a reread of the .bashrc file is the answer. … … … … … … … P.S. The shell knows what to do with the path the same way it does if the user types ./menuScript.sh – by trying to execute the file. – Scott - Слава Україні Jul 25 '14 at 02:39
13

When you do

alias menu='./menuScript.sh'

you create am alias that says "that file" but doesn't say what to actually do with it.

However, if you do

alias menu='source ./menuScript.sh'

or

alias menu='. ./menuScript.sh'

You are saying run that file.

7

Did you remember to source your ~/.bashrc file after making the changes? Because the changes take effect in your .bashrc file after restarting your computer or simply sourcing the file.

Paolo
  • 17,355
cbora
  • 81
  • 2
    This type of post appears as though it would be more suited for a comment. – HalosGhost Jul 24 '14 at 19:42
  • 2
    @HalosGhost How do you figure? It's the first thing I would think of too, it would cause exactly this – Michael Mrozek Jul 24 '14 at 19:46
  • @MichaelMrozek, I suggest this only because it seems to me that answer posts should offer a definitive solution, e.g., "You forgot to source your .bashrc, do the following…". On the other hand, clarifying questions (e.g., "Did you forget to… ?") seem more suited for comments. It wouldn't take much rephrasing to make this post seem much more suited to an answer. – HalosGhost Jul 24 '14 at 19:49
  • While restarting your computer would do the trick, you only need to log out of either tty or your session (if using a login manager). If you are logged into a graphical environment, and you don't want to source ~/.bashrc or . ~/.bashrc, then you can just open up another shell also. It's as simple as that. No rebooting required. – Dylan Oct 01 '16 at 16:32
3

There are two issues with the alias

alias menu='./menuScript.sh'
  1. It requires you to be in a particular directory when you invoke the alias. If you're in a directory where menuScript.sh does not exist, the alias will fail to execute.

    It would be better if you specified the full absolute path to the menuScript.sh script when defining the alias, e.g.

    alias menu="$HOME/local/bin/menuScript.sh"
    

    or similar.

  2. As others have already said, another reason why the alias may fail is that the script is not executable, or that is have an invalid #!-line. Make sure that the script is executable with

    chmod +x menuScript.sh
    

    and that the first line in the script is

    #!/bin/bash
    

    or whatever the path is to bash (or whatever shell the script is written for) on your system.

Kusalananda
  • 333,661
0

The reason why it worked with vim is because that is a program already callable without a direct path. You don't have to explicitly say "I want this to be executed as a program" because vim already is one. It is hard-coded into the OS that when it receives the command vim, go and execute the file /usr/bin/vi or wherever the actual program is.

  • Uh, no, it's not hard-coded into the OS. There is a file called vim available in the $PATH that may be a binary executable, an executable script, or even a symlink to another program.. If you remove the entry for vim it ceases to be available to users. – Chris Davies Apr 27 '17 at 21:16
  • @roaima What I meant was that calling vim is slightly different than calling a custom program. I do see what you are getting at, though, and agree – Nathanael Morgan Apr 27 '17 at 21:28
  • I have a script called ifvi that lives in my $HOME/bin (which happens to be in my $PATH). I just use it in exactly the same way I might run a system-installed binary. It really doesn't matter that it's a custom program. – Chris Davies Apr 28 '17 at 10:22