Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Here is a part of my .bashrc

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

and its my shell script

#!/bin/sh
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-1.7.0.9.x86_64/bin/java and NOT as /usr/bin/java –  proofit404 Feb 26 '13 at 12:15
add comment

3 Answers

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
add comment

alias my_command="/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.9.x86_64/bin/java"

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
add comment

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-1.7.0.9.x86_64/bin/java
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.