22

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?

7
  • 1
    have you tried with alias menu='bash ./menuScript.sh' ? (I am not in a linux machine, can't test it)
    – Con7e
    Commented Jul 24, 2014 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? Commented Jul 24, 2014 at 19:37
  • 1
    @garethTheRed It would say ./menuScript.sh: command not found Commented Jul 24, 2014 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. Commented Jul 24, 2014 at 19:51
  • 1
    @ryekayo done :)
    – Con7e
    Commented Jul 24, 2014 at 20:22

5 Answers 5

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.

1
  • 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. Commented Jul 25, 2014 at 2: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.

4
  • 2
    This type of post appears as though it would be more suited for a comment.
    – HalosGhost
    Commented Jul 24, 2014 at 19:42
  • 2
    @HalosGhost How do you figure? It's the first thing I would think of too, it would cause exactly this Commented Jul 24, 2014 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
    Commented Jul 24, 2014 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
    Commented Oct 1, 2016 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.

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.

3
  • 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. Commented Apr 27, 2017 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 Commented Apr 27, 2017 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. Commented Apr 28, 2017 at 10:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .