1

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?

2
  • Can you tell us what you have for my_command in your file?
    – Incognito
    Feb 26, 2013 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, 2013 at 12:15

3 Answers 3

2

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 "$@"
}
2
  • 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, 2013 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, 2013 at 13:51
0

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?

2
  • 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, 2013 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, 2013 at 16:57
0

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"
1
  • How I can apply this settings in fork of shell interpreter started this #!/bin/sh pragma?
    – proofit404
    Feb 26, 2013 at 12:46

You must log in to answer this question.

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