Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Here is a part of my .bashrc

alias my_command="my_command -1 -2 -3"

and its my shell script

my_command blah blah blah

It occurs into error

./aa: line 2: my_command: command not found

So, how to define aliases for the command in the right way?

share|improve this question
Can you tell us what you have for my_command in your file? – Incognito Feb 26 '13 at 12:08
for example, I want to define java as /usr/lib/jvm/java-1.7.0-openjdk- and NOT as /usr/bin/java – proofit404 Feb 26 '13 at 12:15

Two things that happen in interactive shells (expansion of aliases and sourcing of your .bashrc) do not occur when you run a script. You could try adding the following to the top of your script:

source ~/.bashrc         # To get the alias
shopt -s expand_aliases  # To allow alias expansion in your script

You might consider changing mycommand to a shell function; then you wouldn't have to enable alias expansion in your script.

mycommand () {
    my_command -1 -2 -3 "$@"
share|improve this answer
Where I can place shell function definition which will be available in shell script without usage of source command? Is it possible? – proofit404 Feb 27 '13 at 5:35
Not in any way I would recommend. bash doesn't automatically source any files for non-interactive shells, which is what the script runs in. This is a feature, as you want your script to behave deterministically, not at the whim of the environment of whoever happens to run it. You have to put the function in the script itself. – chepner Feb 27 '13 at 13:51

alias my_command="/usr/lib/jvm/java-1.7.0-openjdk-"

After this type

source .bashrc

After typing source .bashrc are you getting any error?

share|improve this answer
but in separate script, with doesn't use source because its written by other people, this fail. Is there any way to set up alias for any fork or shell interpreter? – proofit404 Feb 26 '13 at 12:43
If the script is written by other people, why would it use an alias defined in your personal .bashrc? – chepner Feb 26 '13 at 16:57

If your intention it to use a specific version of Java, then you might want to define JAVA_HOME and set PATH variable accordingly.

export JAVA_HOME=/usr/lib/jvm/java-1.7.0-openjdk-
export PATH=$PATH:export PATH=$PATH:/usr/java/jdk1.5.0_07/bin

If it's about just setting an alias, then the syntax you used is correct (of course, with some valid commands)

alias lm="ls -l"
share|improve this answer
How I can apply this settings in fork of shell interpreter started this #!/bin/sh pragma? – proofit404 Feb 26 '13 at 12:46

You must log in to answer this question.

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